Saturday, January 11, 2014

Errror casting System.DBNulll to System.Byte[]

Consider this line of code
imgSign.ImageUrl = "data:image/png;base64," + Convert.ToBase64String((byte[])dr["Signature"]);  
now if dr["Signature"] returns a null value compiler will raise the error
Error casting System.DBNulll to System.Byte[]

to handler this error we can either use try catch or if statement to see if dr["Signature"] is not null

if (dr["Signature"] != DBNull.Value)
imgSign.ImageUrl = "data:image/png;base64," + Convert.ToBase64String((byte[])dr["Signature"]);