0

I am trying to retrieve image from access database by using listbox select event

I used the following code to save data to the database :

command.CommandText = "insert into EmployeeInfo (FirstName,LastName,Pay,Pic) values ('" + txt_fname.Text + "' , '" + txt_lname.Text + "' , '" + txt_pay.Text + "' , @productpic)";
            MemoryStream stream = new MemoryStream();
            pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] pic = stream.ToArray();
            command.Parameters.AddWithValue("@productpic", pic);

And I am retrieving data using the following code :

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            connection.Open();

            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select * from EmployeeInfo where FirstName = '" + listBox1.Text +"'";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read()) {
                list_fname.Text = reader["FirstName"].ToString();
                list_lname.Text = reader["LastName"].ToString();
                list_dob.Text = reader["DoB"].ToString();
            }
            connection.Close();
        }

Now what I am trying to do is that to retrieve the picture associated with every row , field name is Pic into a picturebox , and show it in the while loop with other data . I have seen several questions asked by others but everyone is using datagridview to retrieve data where in my case i am trying to do it inside listbox select event .

Any kind of help would be highly appreciated . Thanks in advance .

1 Answers1

0

Called this function in the while loop in listbox select event :

void loadpicture()
            {


                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText = "select pic from EmployeeInfo where FirstName = '" + listBox1.Text + "'";
                OleDbDataAdapter da = new OleDbDataAdapter(command);
                DataSet ds = new DataSet();
                da.Fill(ds);
                byte[] content = (byte[])ds.Tables[0].Rows[0].ItemArray[0];
                MemoryStream stream = new MemoryStream(content);
                pb1.Image = Image.FromStream(stream);

            }