-
November 18th, 2010, 09:16 AM
#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
-
November 18th, 2010, 09:43 AM
#2
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.
-
November 20th, 2010, 10:09 AM
#3
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
-
November 20th, 2010, 11:00 AM
#4
Re: C#/SQL Null Exception Error
Hello TG. I had thought about that. But wouldn't the errror mention the label in some way?
-
November 20th, 2010, 06:48 PM
#5
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|