获取对象引用未设置为附加的业务层代码的对象实例

时间:2012-07-31 16:29:20

标签: c# asp.net .net

我有这个Business Layer / Contactentry.ascx页面调用存储的prcoedure将数据插入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之后,我没有将对象引用设置为对象的insance。我在调试时看到所有值都被传递了。我输入上面的值时输入了所有值,但仍然出错。不确定我错过了什么。请帮助,感谢您的时间。

3 个答案:

答案 0 :(得分:1)

这一行很可疑:

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

Session["ContactID"]可能会返回null并在ToString()上致电nullConvert.ToInt32(null)不会抛出错误 - 它会返回0,而不是NULL。

答案 1 :(得分: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
); 

答案 2 :(得分:0)

您要处理Session["ContactID"]空值并在Converting之前清空Int32

示例:

ContactEntry cEntry = null;
    if(Session["ContactID"] != null)
    {
        if(!String.IsNullOrEmpty(Session["ContactID"]))
        {
        cEntry = new ContactEntry(Convert.ToInt32(Session["ContactID"].ToString());
        }
    }