Faisal Ansari

Faisal Ansari

  • NA
  • 451
  • 833.6k

how to retrieve image from data base ?

Jul 27 2012 2:53 AM
hello guys how r u ?

i want to retrieve image in picture box from database on datagridview cell click event .
but exception raising like "parameter is not valid"
code below here 

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Image image;
            int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["ID"].Value);
            SqlConnection con = new SqlConnection(@"Data Source=ITOFASL254\SQLEXPRESS; Initial Catalog=TestDB; Integrated Security=SSPI; user id=sa; password=passpass; Trusted_Connection=False; database=TestDB;");
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand("select Image from ImageStore where ID=" + dataGridView1.Rows[e.RowIndex].Cells["ID"].Value, con);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                byte[] imageData = (byte[])ds.Tables[0].Rows[0][0];
                using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                {
                    ms.Write(imageData, 0, imageData.Length);
                    image = Image.FromStream(ms);
                }
                pictureBox1.Image = image;
            }

            con.Close();
}

Reply soon 


Answers (5)