Imports System.Data
Imports System.Data.OracleClient
Public Class LogInForm
    Public oraConnection As OracleConnection

    Private Sub LogInForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oraString As String = "Data Source=XE;" + _
                      "User ID=system;" + _
                      "Password=reback"
        oraConnection = New OracleConnection(oraString)

        Try
            oraConnection.Open()
        Catch oraExceptionErr As OracleException
            MessageBox.Show(oraExceptionErr.Message, "Oracle Error")
        Catch InvalidOperationExceptionErr As InvalidOperationException
            MessageBox.Show(InvalidOperationExceptionErr.Message, "Oracle Error")
        End Try

        If oraConnection.State <> ConnectionState.Open Then
            MessageBox.Show("Database connection is Failed")
            Exit Sub
        End If
    End Sub

    Private Sub TabLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabLogIn.Click
        Dim cmdString1 As String = "SELECT user_name, pass_word, faculty_id, student_id FROM LogIn "
        Dim cmdString2 As String = "WHERE user_name=:UserName AND pass_word=:PassWord"
        Dim cmdString As String = cmdString1 & cmdString2
        Dim oraUserName As New OracleParameter
        Dim oraPassWord As New OracleParameter
        Dim LogInTableAdapter As New OracleDataAdapter
        Dim oraDataTable As New DataTable
        Dim oraCommand As New OracleCommand
        Dim selForm As New SelectionForm

        oraUserName.ParameterName = "UserName"
        oraUserName.OracleType = OracleType.Char
        oraUserName.Value = txtUserName.Text
        oraPassWord.ParameterName = "PassWord"
        oraPassWord.OracleType = OracleType.Char
        oraPassWord.Value = txtPassWord.Text
        oraCommand.Connection = oraConnection
        oraCommand.CommandType = CommandType.Text
        oraCommand.CommandText = cmdString
        oraCommand.Parameters.Add(oraUserName)
        oraCommand.Parameters.Add(oraPassWord)
        LogInTableAdapter.SelectCommand = oraCommand
        LogInTableAdapter.Fill(oraDataTable)
        If oraDataTable.Rows.Count > 0 Then
            selForm.Show()
            Me.Hide()
        Else
            MessageBox.Show("No matched username/password found!")
            txtUserName.Text = String.Empty
            txtPassWord.Text = String.Empty
            txtUserName.Focus()
        End If
        oraDataTable.Dispose()
        oraDataTable = Nothing
        oraCommand.Dispose()
        oraCommand = Nothing
        LogInTableAdapter.Dispose()
        LogInTableAdapter = Nothing
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        ' clean up the objects used
        oraConnection.Close()
        oraConnection.Dispose()
        oraConnection = Nothing
        Me.Close()
    End Sub

    Private Sub ReadLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadLogIn.Click
        Dim cmdString1 As String = "SELECT user_name, pass_word, faculty_id, student_id FROM LogIn "
        Dim cmdString2 As String = "WHERE user_name=:UserName AND pass_word=:PassWord"
        Dim cmdString As String = cmdString1 & cmdString2
        Dim oraUserName As New OracleParameter
        Dim oraPassWord As New OracleParameter
        Dim oraCommand As New OracleCommand
        Dim oraDataReader As OracleDataReader
        Dim selForm As New SelectionForm

        oraUserName.ParameterName = "UserName"
        oraUserName.OracleType = OracleType.Char
        oraUserName.Value = txtUserName.Text
        oraPassWord.ParameterName = "PassWord"
        oraPassWord.OracleType = OracleType.Char
        oraPassWord.Value = txtPassWord.Text
        oraCommand.Connection = oraConnection
        oraCommand.CommandType = CommandType.Text
        oraCommand.CommandText = cmdString
        oraCommand.Parameters.Add(oraUserName)
        oraCommand.Parameters.Add(oraPassWord)
        oraDataReader = oraCommand.ExecuteReader
        If oraDataReader.HasRows = True Then
            selForm.Show()
            Me.Hide()
        Else
            MessageBox.Show("No matched username/password found!")
        End If
        oraCommand.Dispose()
        oraCommand = Nothing
        oraDataReader.Close()
        oraDataReader = Nothing
    End Sub
End Class
