Click here to Skip to main content
15,944,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have Access Database. I inserted Employee Data and images in access Database. When i clicked on Datagridview Click Event data is updating to textboxes. With the same click Photo is to update to picturebox. But i am not able to display image in picture box. Please help me.

I converted image into bytes and stored in Database. like
MessageBox.Show("Saving Data at Index :" + rowPosition.ToString());

              OleDbCommand oledbinsert = new OleDbCommand("Insert INTO EmployeeData(EmpID,EmployeeName,PassportNo,PassportExpiry,AddressMalaysia,

OleDbParameter imageParameter = oledbinsert.Parameters.AddWithValue("@Img", SqlDbType.Binary);
imageParameter.Value = ImageAsBytes;
imageParameter.Size = ImageAsBytes.Length;

int rowsAffected = oledbinsert.ExecuteNonQuery();
MessageBox.Show("Data Stored Successfully in" + rowsAffected.ToString() + "Row");
MessageBox.Show("Data Successfully Added to Employee Data");

Your Solution Is very simple

void RetriveImage()
    OleDbConnection conn = new OleDbConnection(@"Your Connection String");
    using (conn)

        Image img ;

        byte[] photo ;

        OleDbCommand cmd = new OleDbCommand("SELECT Pic From ATEST where ID=4", conn);
        OleDbDataAdapter reader;
        reader = cmd.ExecuteReader();
        photo =(byte []) reader.GetValue(0);

        MemoryStream ms = new MemoryStream(photo);

        img = Image.FromStream(ms);

        pictureBox1.Image = img;


Share this answer
Thank You Bro. But its not working.
Just Look at my code in gridview click event :

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            if (e.RowIndex >= 0)

                    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                    Txt_EmpID.Text = row.Cells[0].Value.ToString();
                    Txt_EmpName.Text = row.Cells[1].Value.ToString();
                    Txt_Passport.Text = row.Cells[2].Value.ToString();
                    Dtp_PpExpiry.Text = row.Cells[3].Value.ToString();
                    txt_Address1.Text = row.Cells[4].Value.ToString();
                    Txt_Address2.Text = row.Cells[5].Value.ToString();
                    dtpDOB.Text = row.Cells[6].Value.ToString();
                    Txt_Emergency.Text = row.Cells[7].Value.ToString();
                    Txt_Emerg2.Text = row.Cells[8].Value.ToString();
                    Txt_Bank.Text = row.Cells[9].Value.ToString();
                    Txt_Dl.Text = row.Cells[10].Value.ToString()
    MessageBox.Show("Loading Data");
                catch (Exception ex)
                    MessageBox.Show("Error" + ex);
               // pictureBox1.Image = row.Cells[18].Value as Image;
                //pictureBox1.Image = dataGridView1.CurrentRow.Cells[18].Value as Image;

               // LoadImage();
        }<pre lang="cs">

 <pre lang="cs">void RetriveImage()
            OleDbConnection DBConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Desktop\EmployeeData.accdb");
            using (DBConnection)

                Image img;

                byte[] photo;

                OleDbCommand cmd = new OleDbCommand("SELECT img From EmployeeData where EmpID=4", DBConnection);
                //OleDbDataAdapter reader = new OleDbDataAdapter();
              //  OleDbDataReader reader;
                OleDbDataReader reader = cmd.ExecuteReader();
               // reader = cmd.ExecuteReader();
                while (reader.Read())
                    photo = (byte[])reader.GetValue(0);

                    MemoryStream ms = new MemoryStream(photo);

                    img = Image.FromStream(ms);

                    pictureBox1.Image = img;


<pre lang="cs">
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900