I have code similar to the following:
SqlDataReader sdr = null;
.
.
sdr = cmd.ExecuteReader();
while(sdr.Read()){
string x = sdr["x"].ToString();
Console.WriteLine(x);
long? y = Int64.Parse(sdr["y"].ToString());
}
and I am currently debugging a problem when parsing the value of y. In the database, for the value written to the console, x, it says y is a numeric value (and if it wasn’t- it would be null).
When the exception is thrown I hover over sdr and navigate to the actual data being stored. The values in element [0] do the correspond to the value x which has just been written to screen. In other words, x is definitely accessed, because I can see the correct value for x being displayed. However, in Visual Studio 2008, when debugging, looking at the object’s memory the first element does not have an x-column value the same as what was just displayed on console!
-Why can I not see the element which has been accessed?
-Is this problem related to the fact my numeric value isn’t getting parsed correctly?
I did notice another strange thing: The first time I look at the sdr object in Visual Studio I can see the elements. If i move the mouse to the other side of the screen and then hover back over the object, try to iterate through the elements of the sdr reader object, there are no elements- as if it is empty?!?!
Really that’s not the way to work with SqlDataReader…
Putting apart the compilation problem stated by @TimSchmelter you should write code that checks for DBNull.Value before trying to access and convert the values of a SqlDataReader