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


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

        Call connectDatabase()
        Call getLastID()
        Call loadPublishers()

        DateAddedLabel1.Text = Format(Date.Today, "Short Date").ToString
        PriceTextBox.Text = Format(PriceTextBox.Text, "Standard")
    End Sub

    Private Sub DateAddedLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateAddedLabel1.Click

    End Sub
    Private Sub frmAddProducts_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DragLeave
        MsgBox("ee")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        CategoryComboBox.Text = ""
        ItemNameTextBox.Text = ""
        ItemDescriptionTextBox.Text = ""
        'TotalQtyTextBox.Text = ""
        QtyonHandTextBox.Text = ""
        'QtyTextBox.Text = ""
        PriceTextBox.Text = ""
        DateAddedLabel1.Text = ""
        AmountTextBox.Text = ""

        Call DisconnectDatabase()
        Me.Hide()
    End Sub
    Private Sub getLastID()
        Dim oleDBDR As OleDbDataReader
        Dim oleDBCommand As New OleDbCommand

        With oleDBCommand
            .Connection = conn
            .CommandText = "SELECT * FROM tblStock ORDER BY ItemID DESC"
        End With
        oleDBDR = oleDBCommand.ExecuteReader
        If oleDBDR.Read Then
            ItemIDLabel1.Text = Val(oleDBDR.Item(0)) + 1
        End If
    End Sub
    Private Sub cleartextbox()
        CategoryComboBox.Text = ""
        ItemNameTextBox.Text = ""
        ItemDescriptionTextBox.Text = ""
        ' TotalQtyTextBox.Text = ""
        QtyonHandTextBox.Text = ""
        'QtyTextBox.Text = ""
        PriceTextBox.Text = ""
        AmountTextBox.Text = ""
    End Sub

    Private Sub AmountTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AmountTextBox.TextChanged
        If IsValidTextbox(AmountTextBox) = False Then Exit Sub
    End Sub

    Private Sub QtyonHandTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QtyonHandTextBox.TextChanged
        If IsValidTextboxForNumbers(QtyonHandTextBox) = False Then Exit Sub
    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      
        If MsgBox("Are you sure you want to Add Data?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then
            Dim OleDBC As New OleDbCommand
            With OleDBC

                .Connection = conn
                .CommandText = "Insert Into tblStock (ItemID, Barcode, Category, ItemName, ItemDescription, QtyOnHand, Price, Amount, DateAdded) VALUES ('" & ItemIDLabel1.Text & _
                "','" & TextBox1.Text & _
                "','" & CategoryComboBox.Text & _
                "','" & ItemNameTextBox.Text & _
                "','" & ItemDescriptionTextBox.Text & _
                "','" & QtyonHandTextBox.Text & _
                "','" & PriceTextBox.Text & _
                 "','" & AmountTextBox.Text & _
                "','" & DateAddedLabel1.Text & "')"
                .ExecuteNonQuery()
            End With
            MsgBox("Data Added!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "SUCCESS")
            Me.Hide()
            Call getLastID()
            Call cleartextbox()
        End If
    End Sub
    Private Sub loadPublishers()
        Dim OleDBC As New OleDbCommand
        Dim OleDBDR As OleDbDataReader
        With OleDBC
            .Connection = conn
            .CommandText = "SELECT CategoryName FROM tblCategory"
        End With
        OleDBDR = OleDBC.ExecuteReader
        CategoryComboBox.Items.Clear()
        If OleDBDR.HasRows Then
            While OleDBDR.Read
                CategoryComboBox.Items.Add(OleDBDR.Item(0))
            End While
        End If
    End Sub
End Class