0

我有这个业务层/ Contactentry.ascx 页面,它调用一个存储的过程来将数据插入到 sql 数据库中,然后下面的页面调用这个业务层方法。问题是这种方法

aspdotnet.BusinessLogicLayer.ContactEntry  AddEntry = 
    new ContactEntry(Convert.ToInt32(Session["ContactID"].ToString())
    ,Title,FirstName,MiddleName,LastName,JobTitle,Company,Website,OfficePhone
    ,HomePhone,Mobile,Fax,OEmail,PEmail,OAStreet,OACity,OAState,OACountry
    ,OAZipCode,PAStreet,PACity,PAState,PACountry,PAZipCode);

尽管我已经输入了文本字段的所有值,但会抛出一个错误,指出对象引用未设置为对象的实例。

using System;
using System.Data;
using System.Configuration;
using aspdotnet.DataAccessLayer;

namespace  aspdotnet.BusinessLogicLayer
{
    public class ContactEntry
    {
        private int _ContactID;
        private string _Title;
        private string _FirstName;
        private string _MiddleName;
        private string _LastName;
        private string _JobTitle;
        private string _Company;
        private string _Website; 
        private string _OfficePhone; 
        private string _HomePhone; 
        private string _Mobile; 
        private string _Fax; 
        private string _OEmail;
        private string _PEmail;
        private string _OAStreet;
        private string _OACity; 
        private string _OAState; 
        private string _OACountry;
        private string _OAZipCode;
        private string _PAStreet; 
        private string _PACity; 
        private string _PAState; 
        private string _PACountry;
        private string _PAZipCode;

        public int ContactID
        {
            get { return _ContactID; }
            set { _ContactID = value; }
        }
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }
        public string MiddleName
        {
            get { return _MiddleName; }
            set { _MiddleName = value; }
        }
        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }

        public string JobTitle
        {
            get { return _JobTitle; }
            set { _JobTitle = value; }
        }

        public string Company
        {
            get { return _Company; }
            set { _Company = value; }
        }

        public string Website
        {
            get { return _Website; }
            set { _Website = value; }
        }

        public string OfficePhone
        {
            get { return _OfficePhone; }
            set { _OfficePhone = value; }
        }
        public string HomePhone
        {
            get { return _HomePhone; }
            set { _HomePhone = value; }
        }
        public string Mobile
        {
            get { return _Mobile; }
            set { _Mobile = value; }
        }
        public string Fax
        {
            get { return _Fax; }
            set { _Fax = value; }
        }
        public string OEmail
        {
            get { return _OEmail; }
            set { _OEmail = value; }
        }
        public string PEmail
        {
            get { return _PEmail; }
            set { _PEmail = value; }
        }

        public string OAStreet
        {
            get { return _OAStreet; }
            set { _OAStreet = value; }
        }

        public string OACity
        {
            get { return _OACity; }
            set { _OACity = value; }
        }
        public string OAState
        {
            get { return _OAState; }
            set { _OAState = value; }
        }
        public string OACountry
        {
            get { return _OACountry; }
            set { _OACountry = value; }
        }
        public string OAZipCode
        {
            get { return _OAZipCode; }
            set { _OAZipCode = value; }
        }
        public string PAStreet
        {
            get { return _PAStreet; }
            set { _PAStreet = value; }
        }
        public string PACity
        {
            get { return _PACity; }
            set { _PACity = value; }
        }
        public string PAState
        {
            get { return _PAState; }
            set { _PAState = value; }
        }

        public string PACountry
        {
            get { return _PACountry; }
            set { _PACountry = value; }
        }

        public string PAZipCode
        {
            get { return _PAZipCode; }
            set { _PAZipCode = value; }
        }
        public ContactEntry()
        {
        }
        public ContactEntry(int ContactID, string Title, string FirstName, string MiddleName, string LastName, string JobTitle, string Company, string Website, string OfficePhone, string HomePhone, string Mobile, string Fax, string OEmail, string PEmail, string OAStreet, string OACity, string OAState, string OACountry, string OAZipCode, string PAStreet, string PACity, string PAState, string PACountry, string PAZipCode)
        {

             _ContactID=ContactID; 
             _Title=Title;
             _FirstName = FirstName;
             _MiddleName = MiddleName;
             _LastName = LastName;
             _JobTitle = JobTitle;
             _Company = Company;
             _Website = Website;
             _OfficePhone = OfficePhone;
             _HomePhone = HomePhone;
             _Mobile = Mobile;
             _Fax = Fax;
             _OEmail=OEmail;
             _PEmail=PEmail;
             _OAStreet = OAStreet;
             _OACity = OACity;
             _OAState = OAState;
             _OACountry =OACountry;
             _OAZipCode = OAZipCode;
             _PAStreet = PAStreet;
             _PACity = PACity;
             _PAState = PAState;
             _PACountry = PACountry;
             _PAZipCode = PAZipCode;
        }

        public bool Save()
        {   
            if (_ContactID == 0)
                return Insert();
           else
              return  Update();
        }

        private bool Insert()
        {
            _ContactID = Convert.ToInt32(DBTask.ExecuteScalar(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_Insert", _Title, _FirstName, _MiddleName, _LastName, _JobTitle, _Company, _Website, _OfficePhone, _HomePhone, _Mobile, _Fax, _OEmail, _PEmail, _OAStreet, _OACity, _OAState, _OACountry, _OAZipCode, _PAStreet, _PACity, _PAState, _PACountry, _PAZipCode));
        return (0 < _ContactID);
        }
        public static void Remove(int ContactID)
        {           
        DBTask.ExecuteNonQuery(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_Delete", ContactID);
        }
        private bool Update()
        {
            try
            {
                DBTask.ExecuteNonQuery(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_Update", _ContactID, _Title, _FirstName, _MiddleName, _LastName, _JobTitle, _Company, _Website, _OfficePhone, _HomePhone, _Mobile, _Fax, _OEmail, _PEmail, _OAStreet, _OACity, _OAState, _OACountry, _OAZipCode, _PAStreet, _PACity, _PAState, _PACountry, _PAZipCode);             
                return true;
            }
            catch 
            {
                return false;
            }
        }
        public void  LoadContact(int ContactID)
        {
            DataSet ds = DBTask.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_GetContact", ContactID);
            DataRow row = ds.Tables[0].Rows[0];         
            _ContactID=Convert.ToInt32(row["ContactID"].ToString()); 
            _Title=row["Title"].ToString();  
            _FirstName = row["FirstName"].ToString();  
            _MiddleName = row["MiddleName"].ToString();  
            _LastName = row["LastName"].ToString();  
            _JobTitle = row["JobTitle"].ToString();  
            _Company = row["Company"].ToString();
            _Website = row["Website"].ToString();
            _OfficePhone = row["OfficePhone"].ToString();
            _HomePhone =  row["HomePhone"].ToString();
            _Mobile =  row["Mobile"].ToString();
            _Fax = row["Fax"].ToString();
            _OEmail=row["OfficialEmail"].ToString();
            _PEmail=row["PersonalEmail"].ToString();
            _OAStreet = row["OAStreet"].ToString();
            _OACity = row["OACity"].ToString();
            _OAState = row["OAState"].ToString();
            _OACountry =row["OACountry"].ToString();
            _OAZipCode = row["OAZip"].ToString();
            _PAStreet = row["PAStreet"].ToString();
            _PACity = row["PACity"].ToString();
            _PAState = row["PAState"].ToString();
            _PACountry = row["PACountry"].ToString();
            _PAZipCode = row["PAZip"].ToString();           

        }

    }
}



Insert form calling above function from Business Layer:

private void btnSave_Click(object sender, System.EventArgs e)
        {
            string Title =  drplstTitle.SelectedItem.Text;    
            string FirstName = txtFirstName.Text;
            string MiddleName = txtMiddleName.Text;
            string LastName = txtLastName.Text;
            string JobTitle = this.txtJobTitle.Text;
            string Company = this.txtCompany.Text;
            string Website = this.txtWebSite.Text;
            string OfficePhone = this.txtOfficePhone.Text;
            string HomePhone = this.txtHomePhone.Text;
            string Mobile = this.txtMobile.Text;
            string Fax = this.txtFax.Text;
            string OEmail = this.txtOfficialEmail.Text;
            string PEmail = this.txtPersonalEmail.Text;
            string OAStreet = this.txtOAStreet.Text;
            string OACity = this.txtOACity.Text ;
            string OAState = this.txtOAState.Text;
            string OACountry = this.txtOACountry.Text;
            string OAZipCode = this.txtOAZipCode.Text;
            string PAStreet = this.txtPAStreet.Text;
            string PACity = this.txtPACity.Text;
            string PAState = this.txtPAState.Text;
            string PACountry = this.txtPACountry.Text;
            string PAZipCode = this.txtPAZipCode.Text;
            aspdotnet.BusinessLogicLayer.ContactEntry  AddEntry = 
new ContactEntry(Convert.ToInt32(Session["ContactID"].ToString()),Title,FirstName,MiddleName,LastName,JobTitle,Company,Website,OfficePhone,HomePhone,Mobile,Fax,OEmail,PEmail,OAStreet,OACity,OAState,OACountry,OAZipCode,PAStreet,PACity,PAState,PACountry,PAZipCode);
            //AddEntry.Save();


        }

在上述方法 aspdotnet.BusinessLogicLayer.ContactEntry 之后,我得到对象引用未设置为对象实例。我在调试时看到所有值都在传递。我在输入上述值时输入了所有值,但仍然出错。不知道我错过了什么。请帮助,感谢您的时间。

4

3 回答 3

1

这条线很可疑:

Convert.ToInt32(Session["ContactID"].ToString())

Session["ContactID"]可能会回来null并呼吁炸毁ToString()。不会抛出错误 - 它会返回 0,而不是 NULL。nullConvert.ToInt32(null)

于 2012-07-31T16:38:30.043 回答
0

检查您的会话[“ContactID”]。

考虑将您的代码更改为这样的...

//...
string PAState = this.txtPAState.Text; 
string PACountry = this.txtPACountry.Text; 
string PAZipCode = this.txtPAZipCode.Text;
string contactIDstr = Session["ContactID"] as string;
int contactID = 0;

if(!String.IsNullOrEmpty(conactIDStr))
    Int32.TryParse(contactIDStr, out contactID);

aspdotnet.BusinessLogicLayer.ContactEntry AddEntry = new ContactEntry(
    contentID,
    Title,
    FirstName,MiddleName,LastName,JobTitle,Company,
    Website,OfficePhone,HomePhone,Mobile,Fax,OEmail,PEmail,
    OAStreet,OACity,OAState,OACountry,OAZipCode,PAStreet,PACity,PAState,PACountry,PAZipCode
); 
于 2012-07-31T16:32:36.893 回答
0

你在它Session["ContactID"]之前处理空值和空ConvertingInt32

例子 :

ContactEntry cEntry = null;
    if(Session["ContactID"] != null)
    {
        if(!String.IsNullOrEmpty(Session["ContactID"]))
        {
        cEntry = new ContactEntry(Convert.ToInt32(Session["ContactID"].ToString());
        }
    }
于 2012-07-31T16:37:09.797 回答