Imports System.Data
Imports System.Data.OracleClient

Partial Class _LogIn
    Inherits System.Web.UI.Page
    Public oraConnection As OracleConnection

    Protected Sub Page_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)
        Application("oraConnection") = oraConnection
        If oraConnection.State = ConnectionState.Open Then
            oraConnection.Close()
        End If
        oraConnection.Open()
        If oraConnection.State <> ConnectionState.Open Then
            Response.Write("<script>alert('Database connection is Failed')</script>")
            Exit Sub
        End If
    End Sub

    Protected Sub cmdLogIn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLogIn.Click
        Dim cmdString1 As String = "SELECT user_name, pass_word, faculty_id, student_id FROM LogIn "
        Dim cmdString2 As String = "WHERE user_name=:name AND pass_word=:word"
        Dim cmdString As String = cmdString1 & cmdString2
        Dim paramUserName As New OracleParameter
        Dim paramPassWord As New OracleParameter
        Dim oraCommand As New OracleCommand
        Dim oraDataReader As OracleDataReader

        paramUserName.ParameterName = "name"
        paramUserName.Value = txtUserName.Text
        paramPassWord.ParameterName = "word"
        paramPassWord.Value = txtPassWord.Text
        oraCommand.Connection = oraConnection
        oraCommand.CommandType = CommandType.Text
        oraCommand.CommandText = cmdString
        oraCommand.Parameters.Add(paramUserName)
        oraCommand.Parameters.Add(paramPassWord)
        oraDataReader = oraCommand.ExecuteReader
        If oraDataReader.HasRows = True Then
            Response.Redirect("Selection.aspx")
        Else
            Response.Write("<script>alert('No matched username/password found!')</script>")
        End If
        oraCommand.Dispose()
        oraCommand = Nothing
        oraDataReader.Close()
        oraDataReader = Nothing
    End Sub

    Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        If oraConnection.State = ConnectionState.Open Then
            oraConnection.Close()
        End If
        Response.Write("<script>window.close()</script>")
    End Sub
End Class
