0

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

crthompson
  • 15,653
  • 6
  • 58
  • 80
  • So what is the code of the DataAccess Constructor? – nvoigt Mar 18 '14 at 18:07
  • 1
    Looks like a job for Trace.Assert. ;) – Crono Mar 18 '14 at 18:10
  • please post the code for the class DataAccess. it's constructor code is probably getting a null value in that particular machine. – Raja Nadar Mar 18 '14 at 18:11
  • have you tried putting a breakpoint and debugging through the code to see where exactly it's failing. – Rahul Mar 18 '14 at 18:22
  • @raja public class DataAccess { public SqlCommand cmd = new SqlCommand(); public SqlConnection con = new SqlConnection(); string _connString; public string ConnString public DataAccess() { ConnString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); } public void InitializeDbObjects() { con = new SqlConnection(ConnString); cmd = new SqlCommand(); cmd.Connection = con; } – user3434389 Mar 18 '14 at 18:39
  • @Rahul There is no error or related warnings in this projects, error throwing after publishing – user3434389 Mar 18 '14 at 18:40
  • try doing this in your DataAccess constructor.. this will check if you have an issue with your web.config key.. `string connectionString = ConfigurationManager.AppSettings["ConnectionString"]; if (String.IsNullOrEmpty(connectionString)){throw new Exception("Check connection string.");} ConnString = connectionString; – Raja Nadar Mar 18 '14 at 23:09

2 Answers2

0

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.

  • thanks for your answer, but this is not dbnull value, it is something related to constructor thanks for your answer, but this is not dbnull value, it is something related to constructor this is my dataacces class file string _connString; public string ConnString { get { return _connString; } set { _connString = value; } } public DataAccess() { ConnString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); } – user3434389 Mar 18 '14 at 18:20
0

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
meda
  • 45,103
  • 14
  • 92
  • 122