Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a headache on how to display an image from the datagridview (dgvMembers) to a picturebox (pbMyImage). I managed to save the details that include and image but not on my click event I want the image i saved previously to be displayed on the picture box by I stalled.

here is the code for saving the data into the access database:

VB
<pre> Try
            OpenConnection()
            Dim ms As New MemoryStream()
            Dim bmpImage As New Bitmap(pbMyImage.Image)
            bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim data As Byte() = ms.GetBuffer()
            Dim p As New OleDbParameter("@photo", OleDbType.VarBinary)
            p.Value = data
            Dim cb As String = "insert into Members(TBSNum,PassNum,Firstname,Lastname,DOB,DOJ,Status,Designation,Gender,ResAddress,Contact1,Contact2,EmailID,City,Photo)" &
                "VALUES(@tbsnum,@passnum,@fname,@lname,@dob,@doj,@status,@design,@gender,@resaddress,@contact1,@contact2,@email,@city,@photo)"
            cmd = New OleDbCommand(cb)
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@tbsnum", txtTBSNum.Text)
            cmd.Parameters.AddWithValue("@passnum", txtPassport.Text)
            cmd.Parameters.AddWithValue("@fname", txtName.Text)
            cmd.Parameters.AddWithValue("@lastname", txtSurname.Text)
            cmd.Parameters.AddWithValue("@dob", dtDOB.MaxDate)
            cmd.Parameters.AddWithValue("@doj", System.DateTime.Now.Date)
            cmd.Parameters.AddWithValue("@status", cbStatus.SelectedItem)
            cmd.Parameters.AddWithValue("@design", cbDesig.SelectedItem)
            cmd.Parameters.AddWithValue("@gender", cbGender.SelectedItem)
            cmd.Parameters.AddWithValue("@resaddress", txtAddress.Text)
            cmd.Parameters.AddWithValue("@contact1", txtPhone1.Text)
            cmd.Parameters.AddWithValue("@contact2", txtPhone2.Text)
            cmd.Parameters.AddWithValue("@email", txtEmail.Text)
            cmd.Parameters.AddWithValue("@city", txtCity.Text)
            cmd.Parameters.AddWithValue("@photo", data)
            cmd.ExecuteNonQuery()
            CloseConnection()
            Reset()
            Getdata()
            MessageBox.Show("Successfully saved", " TKBS Member Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


What I have tried:

And here is the code for the click event that I have tried:

VB
Private Sub dgvMembers_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMembers.CellContentClick
        txtTBSNum.Text = dgvMembers.Rows(e.RowIndex).Cells("TBSNum").Value.ToString
        txtPassport.Text = dgvMembers.Rows(e.RowIndex).Cells("PassNum").Value.ToString
        txtName.Text = dgvMembers.Rows(e.RowIndex).Cells("Fname").Value.ToString
        txtSurname.Text = dgvMembers.Rows(e.RowIndex).Cells("Surname").Value.ToString
        dtDOB.Text = dgvMembers.Rows(e.RowIndex).Cells("DOB").Value.ToString
        dtDOJ.Text = dgvMembers.Rows(e.RowIndex).Cells("DOJ").Value.ToString
        cbStatus.Text = dgvMembers.Rows(e.RowIndex).Cells("Status").Value.ToString
        cbDesig.Text = dgvMembers.Rows(e.RowIndex).Cells("Design").Value.ToString
        cbGender.Text = dgvMembers.Rows(e.RowIndex).Cells("Gender").Value.ToString
        txtAddress.Text = dgvMembers.Rows(e.RowIndex).Cells("ResAdd").Value.ToString
        txtPhone1.Text = dgvMembers.Rows(e.RowIndex).Cells("Phone1").Value.ToString
        txtPhone2.Text = dgvMembers.Rows(e.RowIndex).Cells("Phone2").Value.ToString
        txtCity.Text = dgvMembers.Rows(e.RowIndex).Cells("City").Value.ToString
        txtEmail.Text = dgvMembers.Rows(e.RowIndex).Cells("EmailID").Value.ToString
        txtCity.Text = dgvMembers.Rows(e.RowIndex).Cells("City").Value.ToString
        'pbMyImage.Image = dgvMembers.Rows(e.RowIndex).Cells("Photo").Value
    End Sub


The commented line here is one that I suspect having an problem. I dont get and error but the image is not displayed. That worries me. Please help.
Posted
Updated 21-Nov-17 3:28am
v2
Comments
A_Griffin 21-Nov-17 7:00am    
From your code, I assume the image is stored in the database? Have you tried Googling this? Try this for starters:
https://stackoverflow.com/questions/6350516/how-to-add-image-from-database-to-picturebox
Member 13513923 21-Nov-17 8:22am    
@A_Griffin, My image has already been stored in the database and I can see its loaded on my datagridview from the database on form load. The issue is: getting it from the datagridview in the image column and display it on the picture box in the same form.
Member 13513923 30-Nov-17 5:16am    
@A_Griffin, I have tried:

MemoryStream ms = new MemoryStream(bytes);
Image returnImage = Image.FromStream(ms);

and I get the error:

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid.

1 solution

Just read the value from the DGV cell, cast it to a byte[], and convert the bytes to an Image:
MemoryStream ms = new MemoryStream(bytes);
Image returnImage = Image.FromStream(ms);
You can then assign the Image to the PictureBox.Image property.
 
Share this answer
 
Comments
Member 13513923 30-Nov-17 5:09am    
@OriginalGriff, I have tried this but I am getting an error: I am not sure if I have doe the correct thing. I am still stuck.

Dim pic As Byte()
pic = dgvMembers.Rows(e.RowIndex).Cells("Photo").Value
Dim ms As New MemoryStream(pic)
pbMyImage.Image = Image.FromStream(ms)
OriginalGriff 30-Nov-17 5:40am    
Use the debugger and look at exactly what you have in the data at each step.
At a guess - and give the absence of info you have given me that is all it can be - the image data is not valid, but you need to check it to find out exactly what you have got.

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