Imports System.Data
Imports System.Data.OracleClient

Partial Class Insert
    Inherits System.Web.UI.Page
    Public FacultyName As String        'used for validation later by Faculty page.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Application("oraConnection").State <> ConnectionState.Open Then
            Application("oraConnection").Open()
        End If
    End Sub

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

    Protected Sub cmdInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Dim cmdString As String = "INSERT INTO Faculty (faculty_id, name, office, phone, college, title, email) " & _
                                  "VALUES (:faculty_id, :name, :office, :phone, :college, :title, :email)"
        Dim oraCommand As New OracleCommand
        Dim FacultyImage As String
        Dim intInsert As Integer

        FacultyImage = txtPhoto.Text                'display the new inserted faculty photo
        If FacultyImage = String.Empty Then
            FacultyImage = "Default.jpg"
        End If
        Application("FacultyImage") = FacultyImage  'reserve faculty image for validation
        PhotoBox.ImageUrl = FacultyImage
        FacultyName = txtName.Text                  'reserve faculty name for validation
        Application("FacultyName") = FacultyName
        oraCommand.Connection = Application("oraConnection")
        oraCommand.CommandType = CommandType.Text
        oraCommand.CommandText = cmdString
        InsertParameters(oraCommand)
        intInsert = oraCommand.ExecuteNonQuery()
        oraCommand.Dispose()
        oraCommand = Nothing
        If intInsert = 0 Then
            Response.Write("<script>alert('The data insertion is failed')</script>")
            Exit Sub
        End If
        cmdInsert.Enabled = False           'disable the Insert button
    End Sub

    Private Sub InsertParameters(ByRef cmd As OracleCommand)
        cmd.Parameters.Add("faculty_id", OracleType.Char).Value = txtID.Text
        cmd.Parameters.Add("name", OracleType.Char).Value = txtName.Text
        cmd.Parameters.Add("office", OracleType.Char).Value = txtOffice.Text
        cmd.Parameters.Add("phone", OracleType.Char).Value = txtPhone.Text
        cmd.Parameters.Add("college", OracleType.Char).Value = txtCollege.Text
        cmd.Parameters.Add("title", OracleType.Char).Value = txtTitle.Text
        cmd.Parameters.Add("email", OracleType.Char).Value = txtEmail.Text
    End Sub
End Class
