Search code examples
c#sqliteunity-game-engineidatareader

How do I read a null value in the IDataReader?


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?


Solution

  • You can use IsDBNull:

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