Chevy Mark Sunderland

Chevy Mark Sunderland

  • NA
  • 188
  • 166.5k

Object reference not set to an instance of an object

Apr 17 2013 2:07 PM
Hello guys and good day, I need your help.

This is my net code, I've this error in object dr, why?
Can you help me? Thank you in advance.

[code]
protected void BindEmployeeDetails()
{
myConnectionString.Open();

httpCookie CustInfo = Request.Cookies["id"];

SQL = "SELECT * FROM ";
SQL = SQL + " tbl ";
SQL = SQL + " WHERE 1 ";

if (CustInfo != null)
{
SQL = SQL + " AND id = " + Request.Cookies["id"].ToString() + " ;";
}

OdbcCommand cmd = new OdbcCommand(SQL, myConnectionString);
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
OdbcDataReader dr = null;


if (dr.Read())
{
if (ds.Tables[0].Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvDetails.DataSource = ds;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "No record found";
}
}
else
{
Response.Write("No user");
}

myConnectionString.Close();
}


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

Source Error:


Line 43: OdbcDataReader dr = null;
Line 44:
Line 45: if (dr.Read())
Line 46: {
Line 47: if (ds.Tables[0].Rows.Count > 0)
[/code]

Answers (3)