Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check for null integers in codeBehind

Status
Not open for further replies.

raghu3

Programmer
Dec 19, 2005
96
US
The problem being, the integer is not a user input.
I need to say:
if (id != null) {
}

It does not compile, I get the following:
Operator '!=' cannot be applied to operands of type 'int' and '<null>'

If I try
Convert.ToInt32(id)
It compiles but I get a runtime error:
System.NullReferenceException: Object reference not set to an instance of an object.

Any work around ?





 
Does checking if it is null work? e.g.
Code:
if(id == null)


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Isn't there a IsDBNull or something like that?

"...your mom goes to college..."
 
You could use an object instead then and check if that object is null - if it isn't convert it to an int otherwise do what ever validation you are trying to do..


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I don't think that an integer can be null. Once you declare it then it is set to "0." You could check to see if it is zero.
 
Nullable types are new to .NET 2.0. You won't be able to check if an int is null in any version of .NET, and you'll have to use an int? (nullable int) in 2.0.

Otherwise you'll have to do what others suggest which is to check a database value against System.DbNull.Value or assign a default value that indicates "null" (like int.MinValue).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top