Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2008, 12:35 AM
Authorized User
 
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
Default 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


 
Old April 16th, 2008, 02:08 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Abhishek,

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

http://tinyurl.com/6f9t7u

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

Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://robzyc.spaces.live.com
 
Old April 16th, 2008, 03:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Value types can not store a null value.

Int32 IntLevel;

and

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;

or

Nullable<Int32> IntLevel = 12;

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

int? IntLevel = null;

/- Sam Judson : Wrox Technical Editor -/
 
Old April 16th, 2008, 03:07 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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 :)

Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://robzyc.spaces.live.com
 
Old April 16th, 2008, 01:11 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.

-Peter
peterlanoie.blog
 
Old April 17th, 2008, 08:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old April 17th, 2008, 09:00 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Joe,

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

-Peter
peterlanoie.blog
 
Old April 18th, 2008, 12:26 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Rob
http://robzyc.spaces.live.com
 
Old April 18th, 2008, 04:06 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
 
Old April 18th, 2008, 04:11 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

Rob
http://robzyc.spaces.live.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing null values maddy137 ASP.NET 1.0 and 1.1 Basics 1 May 7th, 2006 12:26 PM
passing a null value thru sql parameter polofson .NET Framework 2.0 1 May 3rd, 2006 12:56 AM
passing an integer pointer to a C++ DLL Bill_Thompson Beginning VB 6 1 February 3rd, 2006 06:11 PM
Passing Null in VB kumarpa Pro VB 6 1 March 30th, 2004 04:17 AM
How to make integer to accept null value ganesh15 PHP How-To 3 August 18th, 2003 01:48 PM





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