How do I read a null value in the IDataReader?

131 views Asked by At

I'm using sqlite as a database. One of the value is null in my database. It's datatype is bytes. Following is the code that I use to retrieve the value,

        byte[] blobValue = new byte[iData.GetBytes(4,0,null,0,int.MaxValue)-1];

Unfortunately it throws "Invalid cast exception" as the data is empty. How do I return null if the data is empty in the database?

1

There are 1 answers

0
Tim Schmelter On BEST ANSWER

You can use IsDBNull:

byte[] blobValue = null;
if(!iData.IsDBNull(4))
{
    blobValue = new byte[iData.GetBytes(4,0,null,0,int.MaxValue)-1];
}