Marvin kakuru

Marvin kakuru

  • 1.4k
  • 359
  • 160.8k

Image

Mar 10 2012 4:24 PM
hi everyone,
i have been trying to display an image selected from my sql database in a picturebox.below is the code im using

 
  SqlConnection cn = new SqlConnection("Data Source=KATOTO-PC;Initial Catalog =AssetRegister;User ID=sa;Password=Kyozi");
  string sql = ("select serialnumber,supplier,assetimage from AssetInformation where serialnumber like '" + textBox1.Text + "'");
 
  SqlCommand cmd = new SqlCommand(sql, cn);
  cn.Open();
 
  SqlDataReader sdr = cmd.ExecuteReader();
  sdr.Read(); // read first row
  textBox2.Text = sdr[2].ToString(); // read first column
 
  if (sdr.HasRows)
  {
  while (sdr.Read())
  {
 
  byte[] getimage = (byte[])sdr[2];
  MemoryStream ms = new MemoryStream(getimage);
  Image myimage = Image.FromStream(ms);
  pictureBox2.Image = myimage;
 
 
 
 
  }
  sdr.Close();
 
  }
however nothing is displayed in my picturebox,but if i change line "textBox2.Text = sdr[2].ToString(); // read first column" to sdr[2] which is the column of my image, it brings "System.Byte[]"
any help.
best regards.

Answers (5)