Forums - C# Corner

Forum guidelines
Sakthis

Sakthis

  • 1.3k
  • 50
  • 11.9k

Problem retrieve image from sql table in c#?

Oct 14 2013 5:46 PM
Hi,

I have problem while retrieve image from sql table to picturebox in c#, can anyone? 

This is my code :

 private void btnSearch_Click(object sender, EventArgs e)
        {          
            SqlConnection CN = new SqlConnection("Data Source=sakthi\\sqlexpress; Initial Catalog=LoginDetails; Integrated Security=True");
            SqlCommand cmd = new SqlCommand("select * from emp where empcode ='" + txtEmpCode.Text + "'", CN);            
            SqlDataReader myreader;
            try
            {
                CN.Open();
                myreader = cmd.ExecuteReader();
                if (myreader.Read())
                {
                    txtEmpCode.Text = myreader[0].ToString();
                    txtName.Text = myreader[1].ToString();
                    txtGender.Text = myreader[3].ToString(); 
                    byte[] img = (byte[])(myreader[2]);
                    if (img == null)
                        pictureBox1.Image = null;
                    else
                    {
                        MemoryStream ms = new MemoryStream(img);
                        pictureBox1.Image = Image.FromStream(ms);
                    }                                      
                }
                else
                {
                    MessageBox.Show("do not found");
                }


                CN.Close();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

Answers (2)

Unlimited Flat-Free License

SPONSORED BY

File APIs for .NET
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!