Nullreference error after deploying another machine

1k views Asked by At

I created a asp.net application using C# its working fine in developed machine both iis and visual studio. no error is there, but when i tried to add it to another machine it throwing error. In login page which checks weather the username and password are correct so i made separate function for it. when i clicks login button it throws error

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] DAL.DataAccess..ctor() in C:\Users\Converge\Desktop\woms latest 11-3-2014 12-6 pm\New folder\WebApplication1\DAL\DataAccess.cs:36 WebApplication1.Login.Btnlogin_Click(Object sender, EventArgs e) in C:\Users\Converge\Desktop\woms latest 11-3-2014 12-6 pm\New folder\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

codes

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;
                }
             }


    }

anybody help me please

2

There are 2 answers

1
Bertrand Deroanne On

Could you secure your code for DBNull :

For example :

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

You can make this for each DB Value return by the datarow.

5
meda On

Check your web.config file. If it worked in your local machine.

You will need to adjust your connection string for production server.

It is crashing because your connection object is null


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

Then read it like that:

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