SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    408
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    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.
    PHP 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(SQLmyConnectionString);
            
    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 DetailsSystem.NullReferenceExceptionObject 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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,237
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    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).

  3. #3
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    408
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    thank you for suggestion, but:
    PHP Code:
    Compiler Error MessageCS1061'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

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,237
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    that would be correct, ds does not contain read. A dataset is already populated, so you simply start with your inner IF statement.

  5. #5
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    408
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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?

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,237
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    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)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •