Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Text
Imports System.Data.SqlClient


Public Class SPThawing

    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader


    Private Sub reset()
        txtpn.Text = ""
        txtdesc.Text = ""
        txtmaker.Text = ""
        txtlot.Text = ""
        txtdateregistered.Text = ""
        txtregisteredby.Text = ""
        txtserial.Text = ""
        txtstatus.Text = ""
        txtserial.Focus()
    End Sub

    Private Sub btncancel_Click(sender As System.Object, e As System.EventArgs) Handles btncancel.Click
        reset()
    End Sub

    Private Sub btnthaw_Click(sender As System.Object, e As System.EventArgs) Handles btnthaw.Click
        If txtpn.Text = "M705PCIS4000" Or txtpn.Text = "GY4CS000P110" Then
            If txtstatus.Text = "WHS" Then

                conn = New SqlConnection("data source=192.168.1.41;initial catalog=Toshiba_kanban;user=cats;password=dogs;Integrated Security=false")
                conn.Open()
                cmd = New SqlCommand("Select * from [Toshiba_kanban].[dbo].[PartsRecords] where SerialNum = '" & txtserial.Text & "' and Status = 'THAW'", conn)
                reader = cmd.ExecuteReader()
                If reader.HasRows Then
                    MsgBox("Solder Paste Already Thawed!")
                    reset()
                Else

                    conn = New SqlConnection("data source=192.168.1.41;initial catalog=Toshiba_kanban;user=cats;password=dogs;Integrated Security=false")
                    conn.Open()
                    cmd = New SqlCommand("UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET [Status] = 'THAW', [ThawDate]='" & Date.Now & "' WHERE SerialNum = '" & txtserial.Text & "'", conn)
                    reader = cmd.ExecuteReader
                    conn.Close()

                    MsgBox("Thaw Successfully!")
                    reset()
                End If

            ElseIf txtstatus.Text <> "WHS" Then
                MsgBox("Please return the solder paste to partsprep!")
                reset()
            End If
        Else
            MsgBox("Solder Paste Part Number is invalid")
        End If

    End Sub

    Private Sub txtserial_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtserial.KeyPress


        If Asc(e.KeyChar) = 13 Then
            conn = New SqlConnection("data source=192.168.1.41;initial catalog=Toshiba_kanban;user=cats;password=dogs;Integrated Security=false")
            conn.Open()
            Dim myQuery As String
            myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & txtserial.Text & "'"
            Dim mycommand As SqlCommand
            mycommand = New SqlCommand(myQuery, conn)

            'Try

            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()
                txtpn.Text = reader("ToshibaPN")
                txtdesc.Text = reader("Description")
                txtmaker.Text = reader("SupplierPN")
                txtlot.Text = reader("LotNo")
                txtdateregistered.Text = reader("PrintedDate")
                txtregisteredby.Text = reader("PrintedBy")
                txtstatus.Text = reader("Status")

            Else
                MessageBox.Show("DID Does not exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            reader.Close()

            'Catch ex As Exception
            '    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'Finally
            conn.Close()
            conn.Dispose()
            'End Try

        End If

    End Sub



End Class