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

C

Chris

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
 
S

sloan

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)
 
C

Chris

Thanks

sloan said:
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)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top