Imports System.Data.OleDb
Imports System.Object
Public Class frmsearchProduct

    Private Sub frmsearchProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call connectDatabase()
        Call loadProducts()

        Button2.Enabled = False
    End Sub
    Private Sub loadProducts()
        Dim oleDBC As New OleDbCommand
        Dim oleDBDR As OleDbDataReader
        Dim c As Integer
        c = 0
        With oleDBC
            .Connection = conn
            .CommandText = "SELECT * FROM tblStock "
        End With
        oleDBDR = oleDBC.ExecuteReader
        DataGridView1.Rows.Clear()
        If oleDBDR.HasRows Then
            While oleDBDR.Read
                DataGridView1.Rows.Add()
                DataGridView1.Item(0, c).Value = oleDBDR.Item(0)
                DataGridView1.Item(1, c).Value = oleDBDR.Item(1)
                DataGridView1.Item(2, c).Value = oleDBDR.Item(2)
                DataGridView1.Item(3, c).Value = oleDBDR.Item(3)
                DataGridView1.Item(4, c).Value = oleDBDR.Item(4)
                DataGridView1.Item(5, c).Value = oleDBDR.Item(5)
                DataGridView1.Item(6, c).Value = oleDBDR.Item(6)
                DataGridView1.Item(7, c).Value = oleDBDR.Item(7)
                DataGridView1.Item(8, c).Value = oleDBDR.Item(8)
                
                c = c + 1
            End While
        Else
            MsgBox("No Record to Display!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "WARNING")
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call DisconnectDatabase()

        frmEdetproducts.Show()
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Call DisconnectDatabase()
        Me.Close()
    End Sub
    Private Sub loadsearchresult()
        Dim oleDBC As New OleDbCommand
        Dim oleDBDR As OleDbDataReader
        Dim c As Integer
        c = 0
        If ComboBox1.Text = "Barcode" Then
            With oleDBC
                .Connection = conn
                .CommandText = "SELECT * FROM tblStock WHERE Barcode='" & TextBox2.Text & "'"
                'like '%" & txtType.Text & "%'"
            End With
        ElseIf ComboBox1.Text = "Category" Then
            With oleDBC
                .Connection = conn
                .CommandText = "SELECT * FROM tblStock WHERE Category='" & TextBox2.Text & "'"
            End With
        ElseIf ComboBox1.Text = "Name" Then
            With oleDBC
                .Connection = conn
                .CommandText = "SELECT * FROM tblStock WHERE ItemName='" & TextBox2.Text & "'"
            End With
        Else
            MsgBox("RECORD DOES NOT EXIST")
        End If
        OleDBDR = OleDBC.ExecuteReader
        DataGridView1.Rows.Clear()
        If OleDBDR.HasRows Then
            While OleDBDR.Read
                DataGridView1.Rows.Add()
                DataGridView1.Item(0, c).Value = OleDBDR.Item(0)
                DataGridView1.Item(1, c).Value = OleDBDR.Item(1)
                DataGridView1.Item(2, c).Value = OleDBDR.Item(2)
                DataGridView1.Item(3, c).Value = OleDBDR.Item(3)
                DataGridView1.Item(4, c).Value = OleDBDR.Item(4)
                DataGridView1.Item(5, c).Value = OleDBDR.Item(5)
                DataGridView1.Item(6, c).Value = OleDBDR.Item(6)
                DataGridView1.Item(7, c).Value = OleDBDR.Item(7)
                DataGridView1.Item(8, c).Value = OleDBDR.Item(8)
               
                c = c + 1
            End While
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call loadsearchresult()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call DisconnectDatabase()
        Me.Hide()
        frmAddProducts.Show()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Call loadProducts()
    End Sub
    Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
        Button2.Enabled = True
        If e.RowIndex < 0 Then Exit Sub
        frmEdetproducts.ItemIDLabel1.Text = DataGridView1.Item(0, e.RowIndex).Value
        frmEdetproducts.TextBox1.Text = DataGridView1.Item(1, e.RowIndex).Value
        frmEdetproducts.CategoryComboBox.Text = DataGridView1.Item(2, e.RowIndex).Value
        frmEdetproducts.ItemNameTextBox.Text = DataGridView1.Item(3, e.RowIndex).Value
        frmEdetproducts.ItemDescriptionTextBox.Text = DataGridView1.Item(4, e.RowIndex).Value
        'frmEdetproducts.TotalQtyTextBox.Text = DataGridView1.Item(5, e.RowIndex).Value
        'frmEdetproducts.QtyTextBox.Text = DataGridView1.Item(6, e.RowIndex).Value
        frmEdetproducts.QtyonHandTextBox.Text = DataGridView1.Item(5, e.RowIndex).Value
        frmEdetproducts.PriceTextBox.Text = DataGridView1.Item(6, e.RowIndex).Value
        frmEdetproducts.AmountTextBox.Text = DataGridView1.Item(7, e.RowIndex).Value
        frmEdetproducts.DateAddedLabel1.Text = DataGridView1.Item(8, e.RowIndex).Value


    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub



    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Call loadsearchresult()
    End Sub
    Private Sub callme()

        If TextBox2.Text = "" Then
            MsgBox("magtype ka muna ng isesearch mo")
            Me.Focus()
        Else
            Call loadsearchresult()
        End If
    End Sub
    Private Sub calls()
        Dim oleDBC As New OleDbCommand
        Dim oleDBDR As OleDbDataReader
        Dim c As Integer
        c = 0
        If ComboBox1.Text = "Barcode" Then
            With oleDBC
                .Connection = conn
                .CommandText = "SELECT * FROM tblStock WHERE Barcode='" & TextBox2.Text & "'"
                'like '%" & txtType.Text & "%'"
            End With
        ElseIf ComboBox1.Text = "Name" Then
            With oleDBC
                .Connection = conn
                .CommandText = "SELECT * FROM tblStock WHERE ItemName='" & TextBox2.Text & "'"
            End With
        Else
            MsgBox("RECORD DOESENET EXIST")
        End If

        oleDBDR = oleDBC.ExecuteReader
        If oleDBDR.HasRows Then
            While oleDBDR.Read
                DataGridView1.Rows.Clear()
                DataGridView1.Rows.Add()
                DataGridView1.Item(0, c).Value = oleDBDR.Item(0)
                DataGridView1.Item(1, c).Value = oleDBDR.Item(1)
                DataGridView1.Item(2, c).Value = oleDBDR.Item(2)
                DataGridView1.Item(3, c).Value = oleDBDR.Item(3)
                DataGridView1.Item(4, c).Value = oleDBDR.Item(4)
                DataGridView1.Item(5, c).Value = oleDBDR.Item(5)
                DataGridView1.Item(6, c).Value = oleDBDR.Item(6)
                DataGridView1.Item(7, c).Value = oleDBDR.Item(7)
                DataGridView1.Item(8, c).Value = oleDBDR.Item(8)
               
                c = c + 1

            End While
        Else
            MsgBox("Record Does Not Exist", MsgBoxStyle.Exclamation, "WARNING")
        End If

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Call callme()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub
End Class