How I can test if I have an image from my sql database or not using C#

  • Thread starter Thread starter gaggs1
  • Start date Start date
G

gaggs1

Guest
I have a winforms Desktop application, That store product from clients, those Product can have an image to descripe them. My problem that when I want to charge my product information, my program show me an execption: System.InvalidCastException : 'Impossible d'effectuer un cast d'un objet de type 'System.DBNull' en type 'System.Byte[]'.' I know that exception causes of I don't have an image into my database. then I want to know how to test if my database has an image first.

private void chargeImage()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlCommand com1 = new SqlCommand("Select * FROM [dbo].[Product] where RFID = '" + dataview.SelectedRows[0].Cells[0].Value.ToString() + "'", con);//Charge image
con.Open();
SqlDataReader dr = com1.ExecuteReader();
if (dr.Read())
{
byte[] picArr = (byte[])dr["image"];
ms = new MemoryStream(picArr);
ms.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = Image.FromStream(ms);

}
con.Close();
}




Continue reading...
 
Back
Top