Problem retrieve image from sql table in c#?
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); }
}