CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2010
    Posts
    1

    C#/SQL Null Exception Error

    Hi All;

    Im having an issue with running and returning values from an SQL stored procedure.
    Actually - the running and the retrieving works fine, its using the values returned that is an issue.
    I can see the variable has the returned values, but i cant add them to any form object.

    I keep getting a 'NULL REFERENCE EXCEPTION" at the code below, even tho i do a null check and i know the value is not (nor ever will be) null.

    Any help would be appreciated!!



    string sessionname = "";

    //Run Stored Procedure
    OleDbConnection conn = new OleDbConnection("CONNSTRING");
    conn.Open();
    OleDbCommand cmd = conn.CreateCommand();

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "msgStartSession";
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@empid", empid);
    OleDbDataReader rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {
    mSESSION = Convert.ToInt32(rdr.GetValue(0).ToString());
    sessionname = rdr.GetValue(1).ToString();
    }

    conn.Close();
    conn.Dispose();

    if (String.IsNullOrEmpty(sessionname) != true)
    lblNAME.Text = sessionname; //ERROR HERE

  2. #2
    Join Date
    Dec 2009
    Posts
    596

    Re: C#/SQL Null Exception Error

    You wrote that you could see that the variable has the returned values. Plural. So the sessionname variable is loaded in a loop. It's what the variable has stored in the final iteration that is going to be used. So if you saw values then I guess you're stepping through it. But does the variable have good data just before it is assigned to the text box? The final iteration might be messing it up. But I don't see how the bad data if any gets past the isnull check though.

  3. #3
    Join Date
    Dec 2007
    Posts
    234

    Re: C#/SQL Null Exception Error

    The assumption is that sessionname is what is failing... but what if it was lblName instead? I'd test EVERYTHING on that line to see what's what. Depending on when/where you're running this code (like perhaps in a constructor somewhere before lblName actually exists...) I could see this as a potential problem.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    Re: C#/SQL Null Exception Error

    Hello TG. I had thought about that. But wouldn't the errror mention the label in some way?

  5. #5
    Join Date
    Dec 2007
    Posts
    234

    Re: C#/SQL Null Exception Error

    It is... in a way... the NULL Reference would be the label... the reference to it exists, but it may not have been instanciated fully yet. I've seen a couple people snagged by this process because they assumed that just because they got intellisense when writing the code, the object actually existed. That's why I;'m trying to determine where/when the code is running... it's been pulled out of context.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured