Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   How to retrieve nullable data from a MS SQL server database by using a DataReader (http://www.programmingforums.org/showthread.php?t=12976)

jonyzz Apr 11th, 2007 7:40 AM

How to retrieve nullable data from a MS SQL server database by using a DataReader
 
Hey guys,
I have a question about selecting data from a MS Sql server database by using a SqlDataReader. The problem is that some of the columns in my table can be null. I want to retrieve a bool value or a null depending on what is in the column. I tried using the bool? type like this:

:


bool? b = (bool?)reader["ConfirmColumn"];


it compiles, but throws an Invalid Cast exception at runtime if the value in the "ConfirmColumn" is NULL.
Has any of you had the same problem to solve? Any ideas?
Thanks in advance.

pegasus001 Apr 11th, 2007 8:22 AM

What is spam? And what is edit?

melbolt Apr 12th, 2007 11:13 AM

Hi,

I believe this is what you need

:

  1. if (!(reader["ConfirmColumn"] is System.DBNull.Value))
  2.             {
  3.                 bool? b = (bool?)reader["ConfirmColumn"];
  4.             }


I am better with VB .NET so i tried converting it over to C#, one thing to watch out for, I wasn't sure whether or not to use "is" or "==", in VB .NET you use "is" when working with potential null conditionals, wasn't sure if C# was the same or not. so you may need to change it if it doesn't work. try this first though.

good luck

jonyzz Apr 17th, 2007 9:15 AM

thank you melbolt - it is a good idea


All times are GMT -5. The time now is 2:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC