Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 11th, 2007, 6:40 AM   #1
jonyzz
Programmer
 
jonyzz's Avatar
 
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0 jonyzz is on a distinguished road
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.
jonyzz is offline   Reply With Quote
Old Apr 11th, 2007, 7:22 AM   #2
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
What is spam? And what is edit?
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Apr 12th, 2007, 10:13 AM   #3
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 237
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
Hi,

I believe this is what you need

csharp Syntax (Toggle Plain Text)
  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
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Old Apr 17th, 2007, 8:15 AM   #4
jonyzz
Programmer
 
jonyzz's Avatar
 
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0 jonyzz is on a distinguished road
thank you melbolt - it is a good idea
jonyzz is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i automate text data and input into database? lionel84 C# 0 Jun 26th, 2005 9:35 PM
Help in QBASIC (I think it's similar to VB) phoenix987 Visual Basic 3 May 9th, 2005 12:33 PM
Help with a QBASIC program phoenix987 Other Programming Languages 4 May 5th, 2005 12:27 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:25 AM.

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