ahmed sa

ahmed sa

  • NA
  • 289
  • 128.8k

error Object cannot be cast from DBNull to other types.

Apr 20 2015 3:50 AM
Hi guys i have problem 
Firstly this is all my code 
Function MaxTotalQuantity
public int MaxTotalQuantity(string ConnectionString)
{


SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select max(TotalQuantity) from dbo.EndWork";
con.Open();
int commit = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
return commit;
}
Calling function 
Sales.SalesClass SalesClass4 = new Sales.SalesClass();
int TotalQuantity = SalesClass4.MaxTotalQuantity("Data Source=192.168.1.3;Initial Catalog=yamo;User ID=admin;Password=2233;Connection Lifetime=3;Max Pool Size=3;Connection Timeout=30");
label10.Text = TotalQuantity.ToString();

End work table 
ID intUnchecked
QunatityEndintChecked
Positionnvarchar(50)Checked
IssentintChecked
TotalQuantityintChecked
Unchecked
But error show to me when run code it give me
error Object cannot be cast from DBNull to other types.
Some times value found in TotalQuantity is null
What i need is to convert null to 0 when is null is found show as 0 and exception not show
How to solve this problem



Answers (6)