• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Null handling in C#

Friends,

I have a problem. My stored procedure produces an integer values which I am assigning to an Nullable integer variable.

Dataclass where I call my Stored proc
------------
//Typecasting error happens here . Unable to cast from object to integer datatype
userProfile.NS_ID = Convert.ToInt32(dr.ItemArray[41]);

userprofile class
------------

public int? NMLS_ID
            {
                  get;
                  set;
            }

NS_ID is should get an integer..based on which I do my own business logic. but it is expected that 99% of time ..my stored proc would be producing a null because my db is fresh and users have not started using it.

How do i handle this.

REgards
Subbu
0
SubbuUSA
Asked:
SubbuUSA
2 Solutions
 
petr_hlucinCommented:
I suppose you are talkinig about MSSQL stored procedure. Then use comparison with DBNull.Value:

if (o == DBNull.Value)
{
  // handle null value
}
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
In your code that references the class:

if (ClassInstance.NMLS_ID == null)
{
   //do nothing or report to user that this is null
}
else
{
   // do something
}

If you were only interested in it when there was not a null you could flip the if:

if (ClassInstance.NMLS_ID != null)
{
   //  do something
}
0
 
SubbuUSAAuthor Commented:
Thanks Guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now