部署另一台计算机后出现Nullreference错误

时间:2014-03-18 18:04:59

标签: c# asp.net

我使用C#创建了一个asp.net应用程序,它在开发的机器iis和visual studio中都运行良好。没有错误,但当我试图将它添加到另一台机器时,它会抛出错误。在检查天气的登录页面中,用户名和密码是正确的,所以我为它做了单独的功能。 当我点击登录按钮时,它会抛出错误

  

对象引用未设置为对象的实例。   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

     

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

     

来源错误:

     

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。

     

堆栈追踪:

     

[NullReferenceException:对象引用未设置为对象的实例。]      DAL.DataAccess..ctor()在C:\ Users \ Converge \ Desktop \ woms最新11-3-2014 12-6 pm \新文件夹\ WebApplication1 \ DAL \ DataAccess.cs:36      WebApplication1.Login.Btnlogin_Click(Object sender,EventArgs e)位于C:\ Users \ Converge \ Desktop \ woms最新11-3-2014 12-6 pm \新文件夹\ WebApplication1 \ WebApplication1 \ Login.aspx.cs:46      System.Web.UI.WebControls.Button.OnClick(EventArgs e)+9541114      System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+103      System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10      System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13      System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+35      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1625

login.aspx.cs

//Variable bin is a class which contains all variables for this application
//variablecollection is the collection of variablebin
//LoginDAL is a class which contains all the sqlrelated functions


   protected void Btnlogin_Click(object sender, EventArgs e)
        {
            _BinObj = new VariableBIN();
            _CollObj = new VariableCollection();
            _newColll = new VariableCollection();
            _BinObj.LoginId = TxtLoginid.Text.Trim();
            _BinObj.Password = TxtPassword.Text.Trim();
            _newColll.Add(_BinObj);
            _LogDal =new LoginDAL ();
            _CollObj = _LogDal.LoginCheck(_newColll);
            foreach (VariableBIN _bin in _CollObj)
            {
                if (_bin.Isexist == 1)
                {
                    Session["login"] = _CollObj;
                    if (_bin.LoginTypeId == 1)
                    {
                        Response.Redirect("AdminDashboard.aspx");
                    }
                    else
                    {
                        if (_bin.LoginTypeId == 2)
                        {
                            Response.Redirect("EngineerHeadDashboard.aspx");
                        }

                        else
                        {
                            if (_bin.LoginTypeId == 3)
                            {
                                Response.Redirect("Employee_Task.aspx");
                            }
                            else
                            {
                                if (_bin.LoginTypeId == 4)
                                {
                                    Response.Redirect("Task.aspx");
                                }
                            }


                        }
                    }

                }
                else
                {
                    Lblmsg.Text = "Invalid username or password";
                    Lblmsg.ForeColor = Color.Red;
                }
            }

LoginDAL

public   class LoginDAL:DataAccess
    {

DataSet ds = null;
        SqlDataAdapter da = null;
        SqlDataReader sdr = null;
        string Result = null;
        DataTable dt=null ;



public VariableCollection  LoginCheck(VariableCollection  _collObj)
            {
                try
                {
                    InitializeDbObjects();
                    DB_Open();
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = SP_Login;
                    foreach (VariableBIN _Binobj in _collObj)
                    {
                        cmd.Parameters.Add("@Action", SqlDbType.Int).Value = 1;
                        cmd.Parameters.Add("@Loginid", SqlDbType.NVarChar).Value = _Binobj.LoginId;
                        cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = _Binobj.Password;
                    }

                    da = new SqlDataAdapter();
                    dt = new DataTable();
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        _VarColl = new VariableCollection();
                        _VarBin = new VariableBIN();
                        _VarBin.Isexist = 0;
                        _VarColl.Add(_VarBin);
                        return _VarColl;

                    }
                    else
                    {
                        _VarColl = new VariableCollection();
                        foreach (DataRow dr in dt.Rows)
                        {
                            _VarBin = new VariableBIN();
                            _VarBin.LoginId = dr["loginid"].ToString();
                            _VarBin.LoginTypeId = Convert.ToInt32(dr["logintypeid"].ToString());
                            _VarBin.EmployeeName = dr["employeename"].ToString();
                            _VarBin.EmployeeId = Convert.ToInt32(dr["employeeid"].ToString());
                            _VarBin.Isexist = Convert.ToInt32(dr["isexist"].ToString());
                            _VarColl.Add(_VarBin);
                        }
                        return _VarColl;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
             }


    }

任何人都请帮助我

2 个答案:

答案 0 :(得分:0)

你能保护你的DBNull代码:

例如:

_VarBin.LoginId = (dr["loginid"] != DBNull.Value) ? dr["loginid"].ToString() : string.Empty;

您可以通过数据行为每个DB Value返回。

答案 1 :(得分:0)

检查您的web.config文件。如果它在你的本地机器上工作。

您需要调整生产服务器的连接字符串。

崩溃是因为您的connection对象为空


<connectionStrings>     
    <add name="DefaultConnection" connectionString="Data Source=xxxxxxx"
         providerName="System.Data.SqlClient" /> 
</connectionStrings>

然后这样读:

ConnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].Connecti‌​onString