Imports System.Data
Imports System.Data.OleDb
Imports System.IO

Public Class frmedit
    Inherits System.Windows.Forms.Form

    Dim picStr As String

    Private Sub Updatefrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        filllv()
    End Sub
    Sub filllv()
        lv.Items.Clear()
        lv.Columns.Clear()

        Dim lv_con As New OleDbConnection(cnSettings)
        Dim lv_comstr As String = "select * from tbMember"

        Dim NewQuery As New OleDbCommand(lv_comstr, lv_con)

        Try
            lv_con.Open()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dim NReader As OleDbDataReader = NewQuery.ExecuteReader
        Dim i As Integer
        For i = 0 To NReader.FieldCount - 1
            Dim header As New ColumnHeader
            header.Text = NReader.GetName(i)
            lv.Columns.Add(header)

        Next

        While NReader.Read
            Dim lvitem As New ListViewItem
            lvitem.Text = NReader.GetValue(0).ToString
            For i = 0 To NReader.FieldCount - 1
                lvitem.SubItems.Add(NReader.GetValue(i).ToString)
            Next
            lv.Items.Add(lvitem)

        End While

        NReader.Close()
        lv_con.Close()

    End Sub

   
    Private Sub lv_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv.SelectedIndexChanged
        Dim Selecteditem As ListView.SelectedListViewItemCollection = CType(sender, ListView).SelectedItems
        If (Selecteditem.Count > 0) Then
            Txtid.Text = Selecteditem(0).SubItems(0).Text
            txtfullname.Text = Selecteditem(0).SubItems(2).Text
            listgender.Text = Selecteditem(0).SubItems(3).Text
            mskDOB.Text = Selecteditem(0).SubItems(4).Text
            txthtown.Text = Selecteditem(0).SubItems(5).Text
            listregion.Text = Selecteditem(0).SubItems(6).Text
            txtHno.Text = Selecteditem(0).SubItems(7).Text
            mskPhon.Text = Selecteditem(0).SubItems(8).Text
            txtoccupation.Text = Selecteditem(0).SubItems(9).Text
            listmarrital.Text = Selecteditem(0).SubItems(10).Text
            mskDateConv.Text = Selecteditem(0).SubItems(11).Text
            mskDateBapt.Text = Selecteditem(0).SubItems(12).Text
            txtWhoBapt.Text = Selecteditem(0).SubItems(13).Text
            txtGroupNum.Text = Selecteditem(0).SubItems(14).Text
            listtithegroup.Text = Selecteditem(0).SubItems(15).Text
            txtTitheC_Num.Text = Selecteditem(0).SubItems(16).Text
            txtHomeC_Group.Text = Selecteditem(0).SubItems(17).Text
            txtCell_Plc_Meet.Text = Selecteditem(0).SubItems(18).Text
            picImage.ImageLocation = Selecteditem(0).SubItems(19).Text

        End If
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New OleDbConnection(cnSettings)
        Dim cmdupd As New OleDbCommand
        Dim updstr As String

        Dim fs As FileStream
        fs = New FileStream(picStr, FileMode.Open, FileAccess.Read)
        Dim picByte As Byte() = New Byte(CInt(fs.Length - 1)) {}
        fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
        fs.Close()

        Dim ID As Integer

        ID = Txtid.Text

        updstr = "UPDATE tbMember SET FULLNAME=" & "'" & txtfullname.Text & "'" & "," & _
            "GENDER=" & "'" & listgender.Text & "'" & "," & _
            "DATEOFBIRTH=" & "'" & mskDOB.Text & "'" & "," & _
            "HOMETOWN=" & "'" & txthtown.Text & "'" & "," & _
            "REGION1=" & "'" & listregion.Text & "'" & "," & _
            "HNO=" & "'" & txtHno.Text & "'" & "," & _
            "PHONE=" & "'" & mskPhon.Text & "'" & "," & _
            "OCCUPATION=" & "'" & txtoccupation.Text & "'" & "," & _
            "MARRITAL=" & "'" & listmarrital.Text & "'" & "," & _
            "DATECONV=" & "'" & mskDateConv.Text & "'" & "," & _
            "DATEBAPT=" & "'" & mskDateBapt.Text & "'" & "," & _
            "WHOBAPT=" & "'" & txtWhoBapt.Text & "'" & "," & _
            "GROUPNUM=" & "'" & txtGroupNum.Text & "'" & "," & _
            "TITHEGROUP=" & "'" & listtithegroup.Text & "'" & "," & _
            "TITHENUM=" & "'" & txtTitheC_Num.Text & "'" & "," & _
            "HOMECELLGROUP=" & "'" & txtHomeC_Group.Text & "'" & "," & _
            "HOMECELLMEETING=" & "'" & txtCell_Plc_Meet.Text & "'" & _
            "PICTURE= @PIC" & _
            "WHERE MEMBERID=" & ID & ";"

        cmdupd = New OleDbCommand(updstr, con)
        cmdupd.Parameters.AddWithValue("@PIC", OleDbType.Binary).Value = picByte
        con.Open()

        Try
            cmdupd.ExecuteNonQuery()

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Dispose()
            filllv()
        End Try
        MsgBox("Update successful", MsgBoxStyle.DefaultButton1)


    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Try
            Dim imgdlg As FileDialog = New OpenFileDialog()
            imgdlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"


            If imgdlg.ShowDialog() = DialogResult.OK Then
                picStr = imgdlg.FileName

                Dim newimg As New Bitmap(picStr)

                picImage.SizeMode = PictureBoxSizeMode.CenterImage
                picImage.Image = DirectCast(newimg, Image)
            End If
            txtpicture.Text = imgdlg.FileName

            imgdlg = Nothing
            'Catch ae As System.ArgumentException
            '    imgName = " "
            '    MessageBox.Show(ae.Message.ToString())
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try
    End Sub

   
End Class