Imports System.Data.OleDb

Public Class AddnewProduct
    Private dbc As New OleDbConnection(conStr)

    Private Sub search()
        Dim dba As New OleDbDataAdapter("SELECT * FROM tbProduct WHERE ProdName LIKE '%" & Me.TextBox1.Text & "%';", dbc)
        Dim dbset As New DataSet
        dba.fill(dbset)
        Me.DataGridView2.DataSource = dbset.Tables(0).DefaultView
    End Sub
    Private Sub sgo()
        Dim dba As New OleDbDataAdapter("SELECT * FROM tbProduct;", dbc)
        Dim dbset As New DataSet
        dba.Fill(dbset)
        Me.DataGridView2.DataSource = dbset.Tables(0).DefaultView
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim count As Integer = 0
        For i As Integer = 0 To (Me.DataGridView2.Rows.Count - 1)
            If Not IsDBNull(DataGridView2.Rows(i).Cells("ProdName").Value) AndAlso DataGridView2.Rows(i).Cells("ProdName").Value = TextBox1.Text Then
                count = 1
            End If
        Next
        'MessageBox.Show(count)
        If count > 0 Then
            MessageBox.Show("The Product you want to Add is already on the Database", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub

        End If
        If count = 0 Then
            If MessageBox.Show("Are you Sure you want to add New Product?", "ADD PRODUCT", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.No Then
                Exit Sub

            End If
            Dim incmd As New OleDbCommand("INSERT INTO tbProduct(ProdName, ProdMan) VALUES('" & Me.TextBox1.Text & "', '" & Me.TextBox2.Text & "')", dbc)
            dbc.Open()
            incmd.ExecuteNonQuery()
            dbc.Close()

            MessageBox.Show("The Product has been Successfully Added!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
            search()
            AdminForm.showStocks()
            Exit Sub
        End If
    End Sub

    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        showprod()

        
    End Sub
    Public Sub showprod()
        Dim dba As New OleDbDataAdapter("SELECT * FROM tbProduct;", dbc)
        Dim dbset As New DataSet
        dba.Fill(dbset)
        Me.DataGridView2.DataSource = dbset.Tables(0).DefaultView
    End Sub
   

    'Private Sub Button2_Click(sender As Object, e As EventArgs)
    '   
    'End Sub

    'Private Sub DataGridView2_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView2.SelectionChanged
    '    Dim dba As New OleDbDataAdapter("SELECT * FROM tbProduct WHERE ProdID=" & Me.DataGridView2.CurrentRow.Cells(0).Value & ";", dbc)
    '    Dim dbset As New DataSet
    '    dba.Fill(dbset)
    '    Me.TextBox1.Text = dbset.Tables(0).DefaultView.Item(0).Item(1)
    '    Me.TextBox2.Text = dbset.Tables(0).DefaultView.Item(0).Item(2)

    'End Sub

    Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If Me.Button2.Text = "Edit" Then
            Me.Button2.Text = "SAVE"

            Me.TextBox1.ReadOnly = False
            Me.TextBox2.ReadOnly = False


        ElseIf Me.Button2.Text = "SAVE" Then

            If Me.TextBox1.Text = "" Or Me.TextBox2.Text = "" Then
                MessageBox.Show("EMPTY", "EMPTY", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub

            End If

            Me.Button2.Text = "Edit"



            'Dim incmd As New OleDbCommand("INSERT INTO tbStockOut(StockID,ProdName,Unit,Quantity,Price,OutDate, ExpirationDate) VALUES(" & Me.DataGridView1.CurrentRow.Cells(0).Value & ",'" & Me.TextBox1.Text & "','" & Me.TextBox2.Text & "'," & Me.TextBox3.Text & "," & Me.DataGridView1.CurrentRow.Cells(2).Value * Me.TextBox3.Text & ",#" & Now() & "#, '" & Me.TextBox4.Text & "')", dbc)
            'dbc.Open()
            'incmd.ExecuteNonQuery()
            'dbc.Close()

            Dim edit As New OleDbCommand("UPDATE tbProduct SET ProdName='" & Me.TextBox1.Text & "', ProdMan='" & Me.TextBox2.Text & "' WHERE ProdID=" & Me.DataGridView2.CurrentRow.Cells(0).Value & ";", dbc)
            dbc.Open()
            edit.ExecuteNonQuery()
            dbc.Close()

            showprod()


            MessageBox.Show("Successfully Updated!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.TextBox1.ReadOnly = True
            Me.TextBox2.ReadOnly = True

        End If

       
    End Sub

  

    Private Sub DataGridView2_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        Dim i As Integer = DataGridView2.CurrentRow.Index
        Me.TextBox1.Text = DataGridView2.Item(1, i).Value
        Me.TextBox2.Text = DataGridView2.Item(2, i).Value

    End Sub
End Class