'Option Explicit On
'Option Strict On

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

Public Class frmAdd
    Dim con As New OleDbConnection(cnSettings)
    Dim cmd As New OleDbCommand
    'Dim selectcmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim ds As New DataSet
    Dim Ctrl As Control

    Public picStr As String

    Private Sub frmAdd_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        blnadd = False
        blnupdate = False

    End Sub

    Private Sub frmAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        Try
            
            con.Open()
            cmd.Connection = con
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'DataSet1.tbMember(1).MEMBERID = txtid.Text
        'DataSet1.tbMember(0).FULLNAME = txtfullname.Text

        'Me.TbMemberTableAdapter.Update(Me.DataSet1.tbMember)
        'MsgBox("Record saved successfully")
        'If blnadd = True Then
        addMember()
        'End If



    End Sub

    Private Sub addMember()
        Try
            Dim dt_of_birth As String = CStr(CType(mskDOB.Text, Date))
            Dim dtconvert As String = CStr(CType(mskDateConv.Text, Date))
            Dim dtbaptise As String = CStr(CType(mskDateBapt.Text, Date))

            ''''''
            'If picStr <> "" Then
            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 ms As New MemoryStream
            'picImage.Image.Save(ms, picImage.Image.RawFormat)
            'Dim arrimage As Byte() = ms.GetBuffer

            'ms.Close()

            '''''


            da = New OleDbDataAdapter("Select * from tbMEMBER", con)
            da.Fill(ds, "tbMEMBER")
            cmd.CommandText = "INSERT INTO tbMEMBER(MEMBERID,FULLNAME, GENDER, DATEOFBIRTH, HOMETOWN, REGION1, HNO, PHONE, OCCUPATION, MARRITAL, DATECONV, DATEBAPT, WHOBAPT, GROUPNUM, TITHEGROUP, TITHENUM, HOMECELLGROUP, HOMECELLMEETING,PICTURE) values ('" & txtid.Text & "','" & txtfullname.Text & "','" & listgender.Text & "','" & dt_of_birth & "','" & txthtown.Text & "','" & listregion.Text & "','" & txtHno.Text & "','" & mskPhon.Text & "','" & txtoccupation.Text & "','" & listmarrital.Text & "','" & dtconvert & "','" & dtbaptise & "','" & txtWhoBapt.Text & "','" & txtGroupNum.Text & "','" & listtithegroup.Text & "','" & txtTitheC_Num.Text & "','" & txtHomeC_Group.Text & "','" & txtCell_Plc_Meet.Text & "',@pic)"
            cmd.Parameters.Add("@pic", OleDbType.Binary, 50, "PICTURE").Value = picByte

            cmd.Connection = con

            'con.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Dispose()

        End Try
        'End If
        MsgBox("Record saved successfully", MsgBoxStyle.OkOnly)
        ClearAll()

        'con.Dispose()
        'mems = Nothing

    End Sub


   
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()


    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
    Sub ClearAll()
        For Each Me.Ctrl In Me.Controls
            If TypeOf Ctrl Is TextBox Then
                Ctrl.Text = ("")
            End If
        Next
        'tHE DEFAULT (No Photo) Image
        picImage.ImageLocation = Application.StartupPath & ("\untitled.JPG")
    End Sub

   
End Class
