Forums - C# Corner

Forum guidelines
Sakthis

Sakthis

  • 722
  • 31
  • 11.4k

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)

C# Corner Hyderabad Developer Conference

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.