I've got this query:
SELECT PICTURE FROM LOGO WHERE LOGONO = ?
("PICTURE" is an Image column in SQL Server)
And this code to read the data:
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
if (!reader.IsDBNull(0))
{
byte[] buffer = new byte[8000];
while (reader.GetBytes(0, 0, buffer, 0, 8000) > 0)
picture.AddRange(buffer);
}
}
}
The problem is an InvalidOperationException ("No data exists for the row/column") always gets thrown on the reader.IsDBNull(0) statement when the column contains null.
MSDN says:
Call this method to see if there are null column values before calling the typed get methods (for example, GetByte, GetChar, and so on) to avoid raising an error.
How can I check that the column doesn't contain null without triggering the Exception? Am I going about this in the wrong way?