Object reference not set to an instance of an object

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.

    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)

Because you set it to null…
OdbcDataReader dr = null;

You really want to use your ds variable (as that is the one you are filling).

thank you for suggestion, but:

Compiler Error Message: CS1061: 'System.Data.DataSet' does not contain a definition for 'Read' and no extension method 'Read' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 46:         if (ds.Read())
Line 47:         {
Line 48:             if (ds.Tables[0].Rows.Count > 0)


that would be correct, ds does not contain read. A dataset is already populated, so you simply start with your inner IF statement.

Ok, thank you.

But I need check that the value of the cookie is stored.

If value of the cookie is valid I show the dataset else warning user not enabled for dataset, for this use read.
Can you help me?

You will get an empty dataset if the value isn’t found.

think

if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)