0

I have a datagrid (dgvMembers) with all members details on it, my challenge is displaying the picture stored in the Photo column of the datagrid to the form's picture box after CellContentClick event. Initially I had saved the details successifully (but I am not so sure if the picture was successifully saved together with other form details as well since i am not getting the error - and the the column shows an icon-like image with a red x). Below is the code for btnSave to save details and the CellContectClick event. Please help as I am now stuck.

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles 
btnSave.Click
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", pbMyImage.Image)
        ' 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
End Sub

And the code for the CellContentClick event:

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
Keith
  • 1
  • 1
  • If your data is coming from a database then you should populate a `DataTable`, bind that to a `BindingSource` and then bind that to your grid and the `TextBoxes` and other controls. There's then no code needed to populate those controls. You would handle the `CurrentChanged` event of the `BindingSource`, access the current record via the `Current` property and populate the `PictureBox`. Simples. – jmcilhinney Nov 22 '17 at 06:42
  • If this image is in a compatible format: `pbMyImage.Image = CType(dgvMembers.Rows(e.RowIndex).Cells("Photo").Value, Image)` – Jimi Nov 22 '17 at 06:55
  • @Jmcilhinney thanks for your suggestion but how do I do that in this code. Which lines of code do I add and where? – Keith Nov 22 '17 at 07:13
  • I'm not here to write your code for you. I've given you some general guidance and now it's up to you to look into what I've told you about. When someone gives you a new keyword, e.g. "BindingSource", if searching for information on that is not the first thing you do then you're doing it wrong. – jmcilhinney Nov 22 '17 at 09:20

0 Answers0