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

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
SQL
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");
Posted

Your Solution Is very simple


C#
void RetriveImage()
{
    OleDbConnection conn = new OleDbConnection(@"Your Connection String");
    conn.Open();
    using (conn)
    {

        Image img ;


        byte[] photo ;

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

        MemoryStream ms = new MemoryStream(photo);

        img = Image.FromStream(ms);

        pictureBox1.Image = img;




    }
    conn.Close();

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

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

                    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();
            }
            RetriveImage();
        }<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");
            DBConnection.Open();
            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;
                }
                

            }
            DBConnection.Close();

        }
<pre lang="cs">
 
Share this answer
 
v2

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