Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Below is the code I was written in DAL
C#
public DataSet LoginDisplay()
      {
          SqlCommand cmd = new SqlCommand("GetUser",con);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@UserID", objBOL.User);
          DataSet ds = new DataSet();
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          try
          {  da.Fill(ds);
              return ds;
          }
          catch
          {throw;
          }

          finally
          { da.Dispose();
              con.Close();
              con.Dispose();
          }
      }

================================================
Below is the code I was written in BLL
C#
public DataSet RetrieveRecords()
      {
         LoginDAL _LoginDAL = new LoginDAL();

          try
          {return _LoginDAL.LoginDisplay();
          }
          catch
          {throw;
          }
          finally
          {_LoginDAL = null;
          }
      }

=========================================
Below is the code - UI
C#
protected void Button1_Click(object sender, EventArgs e)
        {
           LoginBOL objBOL = new LoginBOL();
            objBOL.User = txtTest.Text;
            LoginBLL objBLL = new LoginBLL();
            DataSet ds = new DataSet();
            ds = objBLL.RetrieveRecords();
            txtRoles.Text = Convert.ToString(ds.Tables[0].Rows[0]["ROLE"]);
            txtUserID.Text= Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
            txtName.Text= Convert.ToString(ds.Tables[0].Rows[0]["EMPLOYEE_NAME"]);
}


While running the code shows the below error

Server Error in/ Application.

Object reference not set to an instance of an object.


Please help!.
Posted
Updated 25-Nov-14 21:35pm
v2
Comments
[no name] 26-Nov-14 3:36am    
Too generic. Apply a debugger and check where do u encounter this error
Sinisa Hajnal 26-Nov-14 3:42am    
This is the error that is easiest to debug. Use your debugger, find the exact line where the object is not instantiated and create it.

NOTE: if you have try catch, you should do something with them, not just rethrow, otherwise, why have them at all?

Also, consider creating your LoginBOL object in BLL class instead of just passing dataset from the database (in which case, why having the middle tier?). Create the object from the dataset and return that to UI.
[no name] 26-Nov-14 4:00am    
Hi,

And also check your SP fetch any rows?
If yes, Then check the field name like 'ROLE','USERID' and 'EMPLOYEE_NAME' are present in the datatable.

1 solution

First of all you need to check whether the DataSet contains any tables returned from database then if the table contains any row or not.

Then we need to check if for the individual columns having values existing or not.
These checks can be added as follows :-

C#
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if(ds.Tables[0].Rows[0]["ROLE"] != null && !ds.Tables[0].Rows[0].IsNull("ROLE"))
                    txtRoles.Text = Convert.ToString(ds.Tables[0].Rows[0]["ROLE"]);
                if (ds.Tables[0].Rows[0]["UserID"] != null && !ds.Tables[0].Rows[0].IsNull("UserID"))
                    txtUserID.Text = Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
                if (ds.Tables[0].Rows[0]["EMPLOYEE_NAME"] != null && !ds.Tables[0].Rows[0].IsNull("EMPLOYEE_NAME"))
                    txtName.Text = Convert.ToString(ds.Tables[0].Rows[0]["EMPLOYEE_NAME"]);
            }


Hope this example will be useful to avoid the error you are getting here. Then debugging will help you to check where it is failing to satisfying the conditions to solve it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900