Home > Software engineering >  How do I read a null value in the IDataReader?
How do I read a null value in the IDataReader?

Time:10-27

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?

CodePudding user response:

You can use IsDBNull:

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