Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to solve this error Object cannot be cast from DBNull to other type iam using orcle sqldeveloper as backend.


com_id=Convert.ToInt32(ds.Tables[0].Rows[0]["COMPANYID"]);
hear in my COMPANYID coloumn i have blank data. so i got this error. how to resolve it.
help me please
Posted
Comments
Thanks7872 27-Oct-15 1:39am    
If you have blank data and getting error,what help do you need from us? Make such that there is something when you convert it. Another way is to use ternary operator to check if it is blank.

There's at least few possible ways:
1) Using Nullable Types (C# Programming Guide)[^]
C#
//declare nullable integer value
int? myValue = ds.Tables[0].Rows[0]["COMPANYID"];
if (myValue!=null)
{
   //proceed
}


2) Using lambda expressions[^] with DbNull.Value[^]:
C#
int result = ds.Tables[0].Rows[0]["COMPANYID"] == DbNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["COMPANYID"]);


3) Using Int32.TryParse method[^]:
C#
int result = 0;
if(Int32.TryParse(ds.Tables[0].Rows[0]["COMPANYID"], result))
{
    //result has been succesfully converted into integer value
}


Try!
 
Share this answer
 
Check this out-
C#
com_id = Convert.IsDBNull(ds.Tables[0].Rows[0]["COMPANYID"]) ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["COMPANYID"]);



here if DBNull, com_id can be assigned with some default value which could be handled later.
 
Share this answer
 
v2
Comments
Rams@music 27-Oct-15 2:46am    
thank u this is working
and iam try with this. i got result but yours is more preferable
if(ds.Tables[0].Rows[0]["COMPANYID"].ToString()=="")
{
com_id = 0;
}
else
{
com_id=Convert.ToInt32(ds.Tables[0].Rows[0]["COMPANYID"]);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900