Conversion from type 'DBNull' to type 'String' is not valid

Discussion in 'ASP .Net' started by Chris, May 10, 2006.

  1. Chris

    Chris Guest

    Hi again,

    I want to read all the records of a table with 2 fields.
    The problem is that some records have null value in the second field.
    This code below works when all records have both fields fiilled, but gives
    the error:
    "Conversion from type 'DBNull' to type 'String' is not valid" when not.

    .....
    comd = New System.Data.OleDb.OleDbCommand("select name,lok from pc",
    oConnection)
    dtreader = comd.ExecuteReader
    for i=0 to 41
    dtreader.Read()
    for j=0 to 1
    f=dtreader.item(j)
    Response.Write(f)
    next
    Response.Write("<br>")
    next
    dtreader.Close()

    Thanks for hints.
    Chris
     
    Chris, May 10, 2006
    #1
    1. Advertising

  2. Chris

    sloan Guest

    You can either check for

    IsDBNULL before setting it.



    if ( !myDataReader.IsDBNull ( 0 ) )
    {
    string x = myDataReader.GetString(0);
    }


    Or you can find the SafeDataReader .. a class to help with this.
    (google it, but its in vb.net)



    "Chris" <> wrote in message
    news:...
    > Hi again,
    >
    > I want to read all the records of a table with 2 fields.
    > The problem is that some records have null value in the second field.
    > This code below works when all records have both fields fiilled, but gives
    > the error:
    > "Conversion from type 'DBNull' to type 'String' is not valid" when not.
    >
    > ....
    > comd = New System.Data.OleDb.OleDbCommand("select name,lok from pc",
    > oConnection)
    > dtreader = comd.ExecuteReader
    > for i=0 to 41
    > dtreader.Read()
    > for j=0 to 1
    > f=dtreader.item(j)
    > Response.Write(f)
    > next
    > Response.Write("<br>")
    > next
    > dtreader.Close()
    >
    > Thanks for hints.
    > Chris
    >
    >
     
    sloan, May 10, 2006
    #2
    1. Advertising

  3. Chris

    Chris Guest

    Thanks

    "sloan" <> wrote in message
    news:e4Je$...
    >
    > You can either check for
    >
    > IsDBNULL before setting it.
    >
    >
    >
    > if ( !myDataReader.IsDBNull ( 0 ) )
    > {
    > string x = myDataReader.GetString(0);
    > }
    >
    >
    > Or you can find the SafeDataReader .. a class to help with this.
    > (google it, but its in vb.net)
    >
    >
    >
    > "Chris" <> wrote in message
    > news:...
    > > Hi again,
    > >
    > > I want to read all the records of a table with 2 fields.
    > > The problem is that some records have null value in the second field.
    > > This code below works when all records have both fields fiilled, but

    gives
    > > the error:
    > > "Conversion from type 'DBNull' to type 'String' is not valid" when not.
    > >
    > > ....
    > > comd = New System.Data.OleDb.OleDbCommand("select name,lok from pc",
    > > oConnection)
    > > dtreader = comd.ExecuteReader
    > > for i=0 to 41
    > > dtreader.Read()
    > > for j=0 to 1
    > > f=dtreader.item(j)
    > > Response.Write(f)
    > > next
    > > Response.Write("<br>")
    > > next
    > > dtreader.Close()
    > >
    > > Thanks for hints.
    > > Chris
    > >
    > >

    >
    >
     
    Chris, May 11, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elmo Watson
    Replies:
    3
    Views:
    6,049
    Scott M.
    Dec 25, 2003
  2. tshad
    Replies:
    6
    Views:
    3,469
    tshad
    Dec 15, 2004
  3. ibiza
    Replies:
    2
    Views:
    5,354
    ibiza
    Jan 27, 2006
  4. Cirene
    Replies:
    2
    Views:
    904
    nick chan
    Jun 19, 2008
  5. Rob
    Replies:
    0
    Views:
    358
Loading...

Share This Page