473,507 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading null value in a dataset

2 New Member
I have to read a database using dataset. My database has some null values.
My code is working fine when there is a value in the database. But when there is a null value in the database, dataset throws an exception.

I have to read null value in a variable. I have tried this....

if (dr["value"] != DBNull.Value)
{
abc = Convert.ToDouble(dr["value"]);
MessageBox.Show("value is:" + abc.ToString());
}
else
{
abc=null;
MessageBox.Show("value is:" + abc.ToString());
}

But it gives an error that null can not be assigned to a double variable(here abc is a double variable). How can I read a null value in a variable.
Can anybody guide me....
Nov 27 '08 #1
7 3328
anijos
52 New Member
Try declaring abc as a nullable variable

Expand|Select|Wrap|Line Numbers
  1. double? abc =null;
Nov 27 '08 #2
Siyodia
3 New Member
try Convert.isDBNull(database-value-to-check)==true
Nov 29 '08 #3
kenobewan
4,871 Recognized Expert Specialist
I would have expected your null value to be read as a string by the datareader, hence only needing to test for an empty string.
Nov 29 '08 #4
rpicilli
77 New Member
@monika varshney
There some way to do that.

The easiest is to put your code inside a Try Cath constrution

Try
your code
Catch Ex
verify if was throwed a dbnull exception
if this is the case make abc = 0 or other value
End


I hope this help you

Rpicilli
Nov 30 '08 #5
mldisibio
190 Recognized Expert New Member
Just to clarify:
Your check (dr["value"]!= DbNull.Value) is a correct way of checking for a null value from the database, and is an excellent practice.
[The other way is "dr[0].IsDbNull" if you know the column index.]

The error that null cannot be assigned has nothing to do with your database values. It is only because the double "abc" is a value type. Value types have 0 or false as their default values and cannot be assigned to null. Only reference types (objects such as a DataReader or any other class) can be assigned null directly.

So your option is, as anijos said, declare abc as a Nullable double, which was a new Type added to the Framework in 2.0 for the most part to accomodate null database values. It basically says: I have a double which either has some legitimate value, or is undefined. Beware that using this option brings some extra work. Read best practices for using Nullable types: Nullable Types

The other option is to leave abc as a normal double, but assign it a value that your code knows equates to a null in the database, such as 0, -1 or Double.MinValue. However, if all those are legitimate values from your data field, then you must find a work around (such as nullable) that signifies the source column is null.
Dec 1 '08 #6
mldisibio
190 Recognized Expert New Member
P.S. kenobewan:
@kenobewan
Using the "dataReader["columnName"] syntax to pull a value returns the value boxed as an object. So if the source query pulls the field as a numeric type, it will be a boxed numeric.

For any datatype, even if varchar or such, if the column value is null, it is pulled as DbNull.Value, not as a string.

:) Mike
Dec 1 '08 #7
kenobewan
4,871 Recognized Expert Specialist
@mldisibio
They say that you learn something new everyday, good response :).
Dec 1 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1798
by: WhiskyRomeo | last post by:
I have a bound textbox (bindings are set at design time) to a dataset column. Sometimes the value of this textbox must be set so that the underlying database field gets updated to null when using...
0
2138
by: gilly3 | last post by:
Reposting to a more relevant group. I am passing a generic dataset into a new XMLDataDocument and then parsing the XML with XSLT. The idea being that I can parse any dataset with the same xslt,...
8
4202
by: nabil m | last post by:
please help - i have a field that is null in my sql database - i am using C# i getthis error everytimethe fild is null - how do i check to see if the field in my DB is null then output an empty...
2
5081
by: news.microsoft.com | last post by:
Hi After conducting a several searches, I wasn't sure where to post this question. So my apologiesbefore hand for double posting this and possibly posting to the wrong forum(s). Okay, I have...
4
12767
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
9
22476
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
9
3265
by: GotDotNet? | last post by:
I have a dataset and I have to loop through it and some of the values for an insertition into the db. Some of the fields are integers and booleans but contain a NULL in the field. how can I...
2
1923
by: pkenderdine | last post by:
Can someone please advise the best approach for reading in many 5000+ xml files from disk into a data set. Here is the code so far. On my computer (may not be best spec) is takes about 3 minutes to...
2
3863
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.