Hi,

I'm having " 'System.DBNull' type object not assigned to 'System.Byte[]' type " exception on reading picture data from database into picture object. When there is a picture no problem I get it, but I cannot handle the null case. I tried many alternatives but no result. Here is the code

string sSqlCommand="select pic from per015_ssk where  sicil_no='" + dfSicilNo.Text + " ' ";
                OleDbCommand cmd = new OleDbCommand(sSqlCommand,conn);
                ImageByte = (byte[]) cmd.ExecuteScalar() ; // here I get the exception. I tried to use ?? but couldn't succeed

thanks in advance,
snky

Either the record doesn't exist (the dfSicilNo.Text does not exist) or if the record does exist, the field is null.

You should be able to ask the query how many rows (records) it returned (if it is zero, then the record wasn't found).

If there was a row returned, you can use the IsDBNull test from your previous post to make sure that there is data before you try to extract it.

You need to check for the return value.

string sSqlCommand="select pic from per015_ssk where  sicil_no='" + dfSicilNo.Text + " ' ";
OleDbCommand cmd = new OleDbCommand(sSqlCommand,conn);
object o = cmd.ExecuteScalar();
if (o != DBNull.Value)
{
  ImageByte = (byte[])o;
}
else
{
  //The image value in your database is null. You can't show the image.
}

Some records has null value for pic, I tried to check "if null" on the wrong line. So with DBNull it does it. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.