?
Solved

Working with DBNulls

Posted on 2002-06-13
8
Medium Priority
?
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 450 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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