Imports System.Data.SqlClient
Public Class PharmacyProduct

    Private Sub PharmacyProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt1.Enabled = True

        lock()
        searchshow()
        txt1.Enabled = False

        isearch.Enabled = True
        inew.Enabled = True
        isave.Enabled = False
        icancel.Enabled = False
        iupdate.Enabled = False
        idel.Enabled = False

        loddatagrid()
        With grid1


            .ReadOnly = True
            .Columns(0).Width = 180
            .Columns(1).Width = 180
            .Columns(2).Width = 153

            .Columns(0).HeaderText = "Product Code"
            .Columns(1).HeaderText = "Product Name"
            .Columns(2).HeaderText = "Product Price"

        End With
    End Sub
    Private Sub clear()
        txt1.Text = ""
        txt2.Text = ""
        txt3.Text = ""
    End Sub
    Private Sub unlock()
        txt2.Enabled = True
        txt3.Enabled = True
    End Sub
    Private Sub lock()
        txt2.Enabled = False
        txt3.Enabled = False
    End Sub
    Private Sub autonum()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String
        Dim sum As Integer
        Dim no As Integer

        sql = "select max(productcode) from PharmacyProduct"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            no = 1
            txt1.Text = myreader(0)
            sum = txt1.Text + no
            txt1.Text = sum
            'clear()
        Else
            txt1.Text = "1200901"

        End If


        Con.Close()
    End Sub
    Private Sub save()
        Call main()
        Con.Open()
        Dim adapter As New SqlDataAdapter
        Dim sql As String
        sql = "insert into PharmacyProduct (productcode,productnameproductprice) values('" & txt1.Text & "','" & txt2.Text & "', '" & txt3.Text & "')"
        Try

            adapter.InsertCommand = New SqlCommand(sql, Con)
            adapter.InsertCommand.ExecuteNonQuery()
            MsgBox("Successful To save this record on database", MsgBoxStyle.Information, "Information On Database")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Con.Close()
    End Sub


    Private Sub updatesave()
        Call main()
        Con.Open()
        Dim adapter As New SqlDataAdapter
        Dim sql As String

        sql = "update PharmacyProduct set productcode='" & txt1.Text & "',productname='" & txt2.Text & "',productprice='" & txt3.Text & "'  where productcode='" & txt1.Text & "'"
        Try
            adapter.UpdateCommand = Con.CreateCommand
            adapter.UpdateCommand.CommandText = sql
            adapter.UpdateCommand.ExecuteNonQuery()
            MsgBox("Successful To Update this record on database", MsgBoxStyle.Information, "Information On Database")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Con.Close()
    End Sub


    Private Sub deleterecord()
        Call main()
        Con.Open()


        Dim adapter As New SqlDataAdapter
        Dim sql As String

        sql = "delete PharmacyProduct where productcode ='" & txtsearch.Text & "'"
        Try
            adapter.DeleteCommand = Con.CreateCommand
            adapter.DeleteCommand.CommandText = sql
            adapter.DeleteCommand.ExecuteNonQuery()
            MsgBox("Successful To Delete this record on database", MsgBoxStyle.Information, "Information On Database")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Con.Close()
    End Sub

    '********auto complete Search*********************
    Private Sub searchshow()
        Call main()
        Con.Open()

        Dim cmd As New SqlCommand("SELECT productcode FROM PharmacyProduct", Con)
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "My List") 'list can be any name u want

        Dim col As New AutoCompleteStringCollection
        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1
            col.Add(ds.Tables(0).Rows(i)("productcode").ToString())
        Next
        txtsearch.AutoCompleteSource = AutoCompleteSource.CustomSource
        txtsearch.AutoCompleteCustomSource = col
        txtsearch.AutoCompleteMode = AutoCompleteMode.Suggest
        Con.Close()
    End Sub
    Private Sub showsearch()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from PharmacyProduct where productcode like '" & (txtsearch.Text) & "%'"
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            If txtsearch.Text = myreader(0) Then
                unlock()
                inew.Enabled = False
                icancel.Enabled = True
                iupdate.Enabled = True
                idel.Enabled = True

                txt1.Text = myreader(0)
                txt2.Text = myreader(1)
                txt3.Text = myreader(2)
            

            End If
        Else
            lock()
            MsgBox("No record Found on Database", MsgBoxStyle.Critical, "Error")
            inew.Enabled = True
            icancel.Enabled = False
            iupdate.Enabled = False
            idel.Enabled = False

            clear()
        End If

        Con.Close()
    End Sub
    Private Sub loddatagrid()
        Call main()
        Con.Open()
        Dim DataSet1 As New DataSet
        Dim DataAdapter1 As SqlDataAdapter

        DataAdapter1 = New SqlDataAdapter("SELECT [productcode],[productname],[productprice] FROM [dbo].[PharmacyProduct]", Con)
        DataSet1.Clear()
        DataAdapter1.Fill(DataSet1, "Table1")

        grid1.DataSource = DataSet1
        grid1.DataMember = "Table1"
        grid1.Refresh()
        Con.Close()
    End Sub

    Private Sub inew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inew.Click
        isearch.Enabled = False
        inew.Enabled = False
        isave.Enabled = True
        icancel.Enabled = True
        iupdate.Enabled = False
        idel.Enabled = False
        txtsearch.Enabled = False


        clear()
        unlock()

        autonum()
        txt2.Focus()
      
    End Sub

    Private Sub isave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles isave.Click
        save()
        loddatagrid()

        clear()
        lock()
        searchshow()

        isearch.Enabled = True
        inew.Enabled = True
        isave.Enabled = False
        icancel.Enabled = False
        iupdate.Enabled = False
        idel.Enabled = False
        txtsearch.Enabled = True
    End Sub

    Private Sub icancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles icancel.Click
        lock()
        clear()
        txtsearch.Text = ""

        isearch.Enabled = True
        inew.Enabled = True
        isave.Enabled = False
        icancel.Enabled = False
        iupdate.Enabled = False
        idel.Enabled = False
        txtsearch.Enabled = True
    End Sub

    Private Sub iupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iupdate.Click
        On Error GoTo err


        If txtsearch.Text = "" Then
            MsgBox("Sorry you have no record to update!", MsgBoxStyle.Critical, "Error Update")

        Else
            Select Case MsgBox("Are you sure, you want to Update this Record?", MsgBoxStyle.YesNo)
                Case MsgBoxResult.Yes
                    updatesave()
                    loddatagrid()

                    clear()
                    txtsearch.Text = ""
                    lock()

                    searchshow()


                    isearch.Enabled = True
                    inew.Enabled = True
                    isave.Enabled = False
                    icancel.Enabled = False
                    iupdate.Enabled = False
                    idel.Enabled = False
                Case MsgBoxResult.No
                    GoTo err
            End Select


        End If
err:
        Exit Sub
    End Sub

    Private Sub idel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles idel.Click
        On Error GoTo err


        If txtsearch.Text = "" Then
            MsgBox("Sorry you have no record to Delete!", MsgBoxStyle.Critical, "Error Delete")


        Else
            Select Case MsgBox("Are you sure, you want to Delete this Record?", MsgBoxStyle.YesNo)
                Case MsgBoxResult.Yes
                    deleterecord()
                    loddatagrid()

                    clear()
                    txtsearch.Text = ""
                    lock()

                    searchshow()


                    isearch.Enabled = True
                    inew.Enabled = True
                    isave.Enabled = False
                    icancel.Enabled = False
                    iupdate.Enabled = False
                    idel.Enabled = False

                Case MsgBoxResult.No
                    GoTo err
            End Select
        End If


err:
        Exit Sub

    End Sub

    Private Sub txtsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.Click
        If txtsearch.Text = "" Then
            clear()
        End If
    End Sub

    Private Sub txtsearch_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtsearch.KeyUp
        If e.KeyCode = Keys.Enter Then
            showsearch()
        End If
    End Sub

    Private Sub isearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles isearch.Click
        If txtsearch.Text = "" Then
            MsgBox("Search Product Code is Null ", MsgBoxStyle.Critical, "Error Search")
        Else
            showsearch()
        End If
    End Sub
End Class