Solved

Working with DBNulls

Posted on 2002-06-13
8
511 Views
Last Modified: 2008-02-01
When you use a SQLDataReader, there is a method, GetSqlInt32, where you get a value from the data reader and it returns a SqlInt32object. If the field you get is Null, it simply initializes the SQLInt32 Object to hold the value 0. BUT. If you try to make a NEW SqlInt32 based on the same field but from a DataRow instead, it returns an error. Anybody know a way around this?
0
Comment
Question by:drokliss
  • 6
  • 2
8 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7076724
.Net framework is strongly typed environment. IF you will try to create an object with wrong type of arguments it will throw an exception at you. What you will need to do is check if the value in DataField is DBNull or not. If its DBNull, then don't try to set it in SqlInt32.
0
 

Author Comment

by:drokliss
ID: 7076728
Then why would the Datareader initialize a null value when you call the GetSqlInt32 method?
0
 

Author Comment

by:drokliss
ID: 7076730
Then why would the Datareader initialize a null value when you call the GetSqlInt32 method?
0
 

Author Comment

by:drokliss
ID: 7076744
Then why would the Datareader initialize a null value when you call the GetSqlInt32 method?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:drokliss
ID: 7076746
ack! Stinking refresh. Sorry about triple posting
0
 
LVL 23

Accepted Solution

by:
naveenkohli earned 150 total points
ID: 7076845
Since SqlInt32 is a structure so its is a value type. And the default value for this unsigned interger type is 0. You can experiment yourself. Just defaine a SqlInt32 variable and don't assign any value to it. By default it will pick up {0}.
Now try to assign "null" to this variable. It will not compile.

Therefore if you will try to assign a DBNull value to it from DataRow it will throw run timne exception at you.

GetSqlInt32 is not supposed to do any conversions. Check if you NULL value for that field in column in database.
This could very well be bug in the call.

To make sure, call GetValue on DataReader for that field. And check if that "object" is of type DBNull or not.

Naveen

0
 

Author Comment

by:drokliss
ID: 7077209
I know for a fact that the value in the database is null. For whatever reason, the GetSqlInt32 method on the datareader was set up to initialize to its default value when the column passed into it is null. Thanks for your help anyway.
0
 

Author Comment

by:drokliss
ID: 7077212
Helpful knowledge.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now