Imports System.Data.SqlClient
Public Class Frmroom

    Private Sub Frmroom_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If txt1.Text = "" And txt2.Text = "" And txt3.Text = "" And txt4.Text = "" And txt5.Text = "" And txt6.Text = "" Then
            MsgBox("please Complete data before leave this Form", MsgBoxStyle.Critical)
            Dim frm As New Frmroom
            frm.Show()
        Else
            Me.Hide()
        End If
    End Sub

    Private Sub Frmroom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label8.Text = "0912"
        showsearch()

    End Sub
    Private Sub showsearch()
        Call main()
        Con.Open()
        Dim UserSelect As SqlCommand
        Dim myreader As SqlDataReader
        Dim sql As String

        sql = "select * from roominventory where roomno='" & Label8.Text & "' "
        UserSelect = New SqlCommand(sql, Con)
        myreader = UserSelect.ExecuteReader
        If (myreader.Read = True) Then
            Label8.Text = myreader(0)
            txt1.Text = myreader(1)
            txt2.Text = myreader(2)
            txt3.Text = myreader(3)
            txt4.Text = myreader(4)
            txt5.Text = myreader(5)
            txt6.Text = myreader(6)
            bsave.Enabled = False
            bupdate.Visible = True
            bcancel.Visible = True
        Else
            MsgBox("No Record save", MsgBoxStyle.Exclamation)
            bsave.Enabled = True
            bupdate.Visible = False
            bcancel.Visible = True
        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 roominventory(roomno,Ward,semi_private,private,operating_room,emergency_room,icu) values('" & Label8.Text & "','" & txt1.Text & "','" & txt2.Text & "', '" & txt3.Text & "', '" & txt4.Text & "', '" & txt5.Text & "', '" & txt6.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 deleterecord()
        Call main()
        Con.Open()


        Dim adapter As New SqlDataAdapter
        Dim sql As String

        sql = "delete roominventory where roomno ='" & Label8.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



    Private Sub bsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsave.Click

        save()
        showsearch()
        Label8.Text = "0912"

    End Sub

    Private Sub bupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bupdate.Click

        deleterecord()
        showsearch()
        txt1.Text = ""
        txt2.Text = ""
        txt3.Text = ""
        txt4.Text = ""
        txt5.Text = ""
        txt6.Text = ""
    End Sub

    Private Sub bcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bcancel.Click
        showsearch()
    End Sub


    Private Sub txt1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt1.TextChanged
        txtno()
    End Sub
    Private Sub txtno()


        If IsNumeric(txt1.Text) Then
            GoTo ex
        ElseIf txt1.Text = "." Then
            GoTo ex
        ElseIf IsNumeric(txt2.Text) Then
            GoTo ex
        ElseIf txt2.Text = "." Then
            GoTo ex
        ElseIf IsNumeric(txt3.Text) Then
            GoTo ex
        ElseIf txt3.Text = "." Then
            GoTo ex
        ElseIf IsNumeric(txt4.Text) Then
            GoTo ex
        ElseIf txt4.Text = "." Then
            GoTo ex
        ElseIf IsNumeric(txt5.Text) Then
            GoTo ex
        ElseIf txt5.Text = "." Then
            GoTo ex
        ElseIf IsNumeric(txt6.Text) Then
            GoTo ex
        ElseIf txt6.Text = "." Then
            GoTo ex
        Else
            MsgBox("You cannot add alpha characters, please input the Integer or numbers !", vbCritical)
            txt1.Text = ""

        End If

ex:
        Exit Sub
    End Sub

    Private Sub txt2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt2.TextChanged
        txtno()
    End Sub

    Private Sub txt3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt3.TextChanged
        txtno()
    End Sub

    Private Sub txt4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt4.TextChanged
        txtno()
    End Sub

    Private Sub txt5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt5.TextChanged
        txtno()
    End Sub

    Private Sub txt6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt6.TextChanged
        txtno()
    End Sub
End Class