urgent asp.net invalid cast error

Discussion in 'ASP .Net' started by Guest, Jun 7, 2006.

  1. Guest

    Guest 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
     
    Guest, Jun 7, 2006
    #1
    1. Advertisements

  2. Guest

    Winista Guest

    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 ...
    }
     
    Winista, Jun 7, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.