Forum guidelines
AuthorQuestion
Problem retrieve image from sql table in c#?
Posted on: 14 Oct 2013
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); }
        }

AuthorReply
Re: Problem retrieve image from sql table in c#?
Posted on: 14 Oct 2013  
Re: Problem retrieve image from sql table in c#?
Posted on: 15 Oct 2013  
yea i try this i got error like there is no position row 2:

But my schema in sql like : 

CREATE TABLE [dbo].[emp](
[empcode] [varchar](50) NULL,
[name] [varchar](50) NULL,
[image] [image] NULL,
[gender] [varchar](50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

My forms like: 

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;          
            DataTable dt = new DataTable();            
            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[])dt.Rows[2]["image"]; 
                    MemoryStream ms = new MemoryStream();                    
                    ms.Write(img, 0, img.Length);
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.Close();
                    pictureBox1.Image = Image.FromStream(ms);
                }
                else
                {
                    MessageBox.Show("do not found");
                }

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

SPONSORED BY

Offshore Software and Database 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.