Question : Check null integer

Hi Experts,
I am calling a stored procedure which retuns me integer value or null sometimes.

int? BId = (int?)DBDataHandler.ExecuteScalar(connection, "SPID", UID);
            if (Convert.IsDBNull(BuId))
                BId = -1;
           
            return (int)BId;

when the returned value is not null then it is fine but when I get null value I am getting exception, if (Convert.IsDBNull(BuId)) ==>> Specified cast is not valid.

What is going wrong?
Your any help is appreciated.Thanks

Answer : Check null integer

what about this:
1:
2:
3:
object r = DBDataHandler.ExecuteScalar(connection, "SPID", UID);
int BId = Convert.IsDBNull(r) ? -1 : (int)r
return BId;
Random Solutions  
 
programming4us programming4us