Solved

Working with DBNulls

Posted on 2002-06-13
8
527 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

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

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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