passing method parameters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a method which takes two parameters.. and they both come from database.

I get error message when one of the parameter from the database contain null
value.

What do i need to check to prevent getting this error? how do i check for
null value?? how can i replace the null value with (0) zero to stop this
error?

Many thanks

Eerror message: Specified cast is not valid.

int DBBrandID =(int)((DbDataRecord)e.Item.DataItem)["BrandID"];
int DBModelID =(int)((DbDataRecord)e.Item.DataItem)["ModelID"];

lblMakeModel.Text = cs.getBrandModel(DBBrandID,DBModelID);
 
huzz said:
I have a method which takes two parameters.. and they both come from database.

I get error message when one of the parameter from the database contain null
value.

What do i need to check to prevent getting this error? how do i check for
null value?? how can i replace the null value with (0) zero to stop this
error?

Many thanks

Eerror message: Specified cast is not valid.

int DBBrandID =(int)((DbDataRecord)e.Item.DataItem)["BrandID"];
int DBModelID =(int)((DbDataRecord)e.Item.DataItem)["ModelID"];

lblMakeModel.Text = cs.getBrandModel(DBBrandID,DBModelID);

You can use DbDataRecord.IsDBNull to check for nullity. You might want
to have a helper method to return 0, eg:

int GetInt (DbDataRecord record, string columnName)
{
if (record.IsDBNull (columnName))
{
return 0;
}
else
{
return (int)record[columnName];
}
}
 
Back
Top