Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • 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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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