p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# 2005 (http://p2p.wrox.com/forumdisplay.php?f=138)
-   -   Passing null value to integer (http://p2p.wrox.com/showthread.php?t=67436)

abhishekkashyap27 April 16th, 2008 12:35 AM

Passing null value to integer
Hi All

I just need a little bit of clarification.I m using C# as a language, Sql Server 2005 as database. I am developing a web application where i need to pass integer as a null value.

Here is the piece of code:

Int32 IntLevel; /* declaring IntLevel as an integer */
IntLevel = dbnull.value;

dbnull.value here doesnt work, i have declared it and i do have to pass a value to it but it should be null in order to retreive all the records of Level Column.Level column contains thousands of integer values starting from 0,1,2....., etc.

Please advise, is there any possiblity to pass null value to an integer from C# side.

-- Abhishek

robzyc April 16th, 2008 02:08 AM

Hi Abhishek,

A quick (about 1-2 seconds) google looking for links reveals:


(Links to MSDN, URL wouldnt seem to work with the original address)

The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://robzyc.spaces.live.com

samjudson April 16th, 2008 03:00 AM

Value types can not store a null value.

Int32 IntLevel;


Int32 IntLevel = 0;

both do the same thing, create a new integer value and set its initial value to 0.

DBNull, is not the same as 'null' - it is a class used by the database providers to signify a Null in a database field. So you are never (ever) going to be able to assign DBNull.Value to an integer variable.

There is a generic type called Nullable<> (or Nullable(Of) in VB.Net) which can be used to store null values as well as value types.

Nullable<Int32> IntLevel = null;


Nullable<Int32> IntLevel = 12;

in C# you can abbreviate Nullable<Int32> to int?.

int? IntLevel = null;

/- Sam Judson : Wrox Technical Editor -/

robzyc April 16th, 2008 03:07 AM

Hi Sam,

I recently got into these as well, since the MCAD is based on .NET 1.1 I miss out on all the generic goodness :D its nice to work in 2.0 at work and be able to null ints/bools etc. since like almost every other business, we have data, in databases, with nulls :)

I like the C# shorthand too :)

The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://robzyc.spaces.live.com

planoie April 16th, 2008 01:11 PM

Abhishek -

As Sam has suggested, you can create nullable program side variables. When it comes to passing those values to the DB, you'll need to provide a little logic. Test the var for null, then either provide DBNull.Value or the actual integer. If your sproc supports a default of null for that parameter, then you don't even need to provide it.


joefawcett April 17th, 2008 08:18 AM

To add to the responses slightly a nullable type such as int should be tested by using the HasValue property. If this is true use its Value property or the variable directly, if false then it's null. A few examples on MSDN here.


Joe (Microsoft MVP - XML)

planoie April 17th, 2008 09:00 PM


I'm curious: what is the benefit of HasValue over !=null?


robzyc April 18th, 2008 12:26 AM

Interesting question Peter, I found myself asking the same question! May have to dig on this some more once I in the office! :)


samjudson April 18th, 2008 04:06 AM

Both i.HasValue and i == null compile to exactly the same IL (i.HasValue).

Strangely a nullable type never seems to actually equal null.

i.e. The following throws a null reference exception:

List<int> l = null;
l.Add(1); // exception thrown

Nullable<int> i = null;
i.HasValue; // doesn't throw an exception, but returns false.

i = null actually compiles to a call to "initobj" IL command which initialises all the fields of the instance to null or zero.

/- Sam Judson : Wrox Technical Editor -/

robzyc April 18th, 2008 04:11 AM

Hmm.. Interesting stuff!
Thanks for sharing Sam! :)


All times are GMT -4. The time now is 02:14 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.