473,809 Members | 2,469 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.ToDoubl e(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 3348
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.isDBNul l(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
1828
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 the update method of the dataadapter. Since the dataset field datatype is INT32 it gets set to the empty string equivalent which is 0. Zero is not a valid value so the update fails. Can this be done without changing the SQL of the udpdate...
0
2157
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, and dynamically generate an html table. The problem is when the DataSet contains a null value, the node for that record is not created. Then when the xml parses it, there are too few cells in some of the table rows. I can think of a couple...
8
4214
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 string ? please help - thx in advance Line 757: get { Line 758: try { Line 759: return ((string)(this));
2
5108
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 any application that uses the data access application building block from Microsoft's Enterprise Library (patterns and practices site). I want to allow the users to select the database connection. To do this, I thought I could read the...
4
12812
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 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
9
22515
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 namespaces and classes should I use and how? -- dba123
9
3279
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 check for a NULL and if its NULL insert DBNull into the db? Currently I'm getting error message System.DBNull.System.IConvertible.ToBoolean() now i need to get this field and insert a NULL into the db is the values
2
1940
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 read and load the dataset. It doesn't seem to be the reading of the files as so much as the loading of the data into the dataset. Any help would be appreciated. cenOcr = new DataSet(); // Create the XmlSchemaSet class. XmlSchemaSet sc = new...
2
3883
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 stated. Also please note that this is unrelated to another of my previous threads, “dataAdapter.Update problem”, which incidentally has been resolved. I’m learning SQL. I’m accessing database SQL2008 AdventureWorks, table Person.Contact, which has a...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.