SqlDataReader C# Index (Item Property)

  • Thread starter Thread starter Carlo Razzeto
  • Start date Start date
C

Carlo Razzeto

I have a question about what the SqlDataReader index value returns exactly
(in VB SqlDataReader.Item("Value")). According to the .Net SDK this property
returns:

Overloaded. Gets the value of a column in its native format.

I translated this as this property returns the value as it's type, so an int
type in my sql database would return as an int? If this is true then I'm
guess that the following code *should* run with out error. Assume current is
an sql feild of type int:

if ((int) this.Reader["Current"] == 0)
{
table += "...";
}
else
{
table += "...";
}

Are my assumptions correct? Or am I about to run into some trouble. Thanks
for clarification!

Carlo
 
Hi Carlo,

Thanks for posting your question.

If the "Current" column is defined as an integer, then your If statement
If ((int) this.Reader["Current"] == 0)
will return true if the column contains a zero, otherwise the If statement
will return false.

Hope this helps,
bliz

--
Jim Blizzard | http://weblogs.asp.net/jblizzard
Sr .NET Developer Specialist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.
 
NB if the field contains null, then the code will report an exception. You
can check for Null database fields by using

if (rdr.IsDBNull(rdr.GetOrdinal("Current")))
// null
else
// Not null

or alternatively,

object v = rdr["Current"];
if (Convert.IsDBNull(v))
str += "<null>";
else
str += v.ToString();





Carlo Razzeto said:
Thank you for the clarification!

Carlo

Jim Blizzard said:
Hi Carlo,

Thanks for posting your question.

If the "Current" column is defined as an integer, then your If statement
If ((int) this.Reader["Current"] == 0)
will return true if the column contains a zero, otherwise the If statement
will return false.

Hope this helps,
bliz

--
Jim Blizzard | http://weblogs.asp.net/jblizzard
Sr .NET Developer Specialist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.
 
Back
Top