Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 16th, 2008, 12:35 AM
Authorized User
 
Join Date: May 2007
Location: Hyderabad, Andhra Pradesh, India.
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


  #2 (permalink)  
Old April 16th, 2008, 02:08 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
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
  #3 (permalink)  
Old April 16th, 2008, 03:00 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
  #4 (permalink)  
Old April 16th, 2008, 03:07 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
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
  #5 (permalink)  
Old April 16th, 2008, 01:11 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
  #6 (permalink)  
Old April 17th, 2008, 08:18 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
  #7 (permalink)  
Old April 17th, 2008, 09:00 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
  #8 (permalink)  
Old April 18th, 2008, 12:26 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
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
  #9 (permalink)  
Old April 18th, 2008, 04:06 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
  #10 (permalink)  
Old April 18th, 2008, 04:11 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:41 AM.


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