Solved

Working with DBNulls

Posted on 2002-06-13
8
501 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

12 Experts available now in Live!

Get 1:1 Help Now