Imports System.Data
Imports System.Data.SqlClient

Partial Class Faculty
    Inherits System.Web.UI.Page
    Private FacultyTextBox(6) As TextBox        'Faculty table has 7 columns, we used all of them

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Application("sqlConnection").State <> ConnectionState.Open Then
            Application("sqlConnection").Open()
        End If
        If Not IsPostBack Then
            ComboName.Items.Add("Ying Bai")
            ComboName.Items.Add("Satish Bhalla")
            ComboName.Items.Add("Black Anderson")
            ComboName.Items.Add("Steve Johnson")
            ComboName.Items.Add("Jenney King")
            ComboName.Items.Add("Alice Brown")
            ComboName.Items.Add("Debby Angles")
            ComboName.Items.Add("Jeff Henry")
        End If
        If Application("FacultyName") <> String.Empty Then
            ComboName.Items.Add(Application("FacultyName"))
            Application("FacultyName") = String.Empty
        End If
    End Sub

    Protected Sub cmdSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSelect.Click
        Dim cmdString As String = "SELECT faculty_id, name, office, phone, college, title, email FROM Faculty " & _
                                  "WHERE name LIKE @facultyName"
        Dim paramFacultyName As New SqlParameter
        Dim sqlCommand As New SqlCommand
        Dim sqlDataReader As SqlDataReader

        paramFacultyName.ParameterName = "@facultyName"
        paramFacultyName.Value = ComboName.Text
        sqlCommand.Connection = Application("sqlConnection")
        sqlCommand.CommandType = CommandType.Text
        sqlCommand.CommandText = cmdString
        sqlCommand.Parameters.Add(paramFacultyName)
        Call ShowFaculty(ComboName.Text)
        sqlDataReader = sqlCommand.ExecuteReader
        If sqlDataReader.HasRows = True Then
            Call FillFacultyReader(sqlDataReader)
        Else
            Response.Write("<script>alert('No matched faculty found!')</script>")
        End If
        sqlDataReader.Close()
        sqlDataReader = Nothing
        sqlCommand.Dispose()
        sqlCommand = Nothing
    End Sub

    Private Sub FillFacultyReader(ByVal FacultyReader As SqlDataReader)
        Dim intIndex As Integer

        For intIndex = 0 To 6                       'Initialize the object array
            FacultyTextBox(intIndex) = New TextBox()
        Next intIndex
        Call MapFacultyTable(FacultyTextBox)
        While FacultyReader.Read()
            For intIndex = 0 To FacultyReader.FieldCount - 1
                FacultyTextBox(intIndex).Text = FacultyReader.Item(intIndex).ToString
            Next intIndex
        End While
    End Sub

    Private Sub MapFacultyTable(ByRef fText As Object)
        fText(0) = txtID            'The order must be identical
        fText(1) = txtName          'with the real order in the query string cmdString
        fText(2) = txtOffice
        fText(3) = txtPhone
        fText(4) = txtCollege
        fText(5) = txtTitle
        fText(6) = txtEmail
    End Sub

    Private Sub ShowFaculty(ByVal fName As String)
        Dim FacultyImage As String

        Select Case fName
            Case "Ying Bai"
                FacultyImage = "Bai.jpg"
            Case "Satish Bhalla"
                FacultyImage = "Satish.jpg"
            Case "Black Anderson"
                FacultyImage = "Anderson.jpg"
            Case "Steve Johnson"
                FacultyImage = "Johnson.jpg"
            Case "Jenney King"
                FacultyImage = "King.jpg"
            Case "Alice Brown"
                FacultyImage = "Brown.jpg"
            Case "Debby Angles"
                FacultyImage = "Angles.jpg"
            Case "Jeff Henry"
                FacultyImage = "Henry.jpg"
            Case Else
                If Application("FacultyImage") = String.Empty Then
                    FacultyImage = "Default.jpg"
                Else
                    FacultyImage = Application("FacultyImage")
                End If
        End Select
        PhotoBox.ImageUrl = FacultyImage
    End Sub

    Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
        Response.Redirect("Selection.aspx")
    End Sub

    Protected Sub cmdInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Response.Redirect("Insert.aspx")
    End Sub
End Class
