urgent asp.net invalid cast error

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

Guest

System.InvalidCastException: Specified cast is not valid.
hi i am received that error below is my code

result = new CustID(Convert.ToString(reader["CustID"]),
Line 57:
(DateTime)reader["BirthDate"],Convert.ToString(reader["Name"]),
Convert.ToString(reader["Sex"]), (Byte)reader["NameFormatCodeOri"],
Convert.ToString(reader["Address"]),
Convert.ToString(reader["PostCode"]));
First error was can not convert dbnull to string then i used
Convert.tostring and now i am getting that error not with all record but with
some records i assume dbnull value created some problem
thanks
 
If some of your fields can have null values then you need to check for
DbNull values before accessing those values. You can convert a null value.

If (!Convert.IsDBNull(dr[......])
{
... do your stuff ...
}
 
Back
Top