Option Explicit On 
Option Strict On
Imports System.Data
Imports System.Data.SqlClient

Public Class Teacher_Info
	Inherits System.Web.UI.Page
	Protected WithEvents imgTeacher As System.Web.UI.WebControls.Image
	Protected WithEvents btnSaveChanges As System.Web.UI.WebControls.Button
	Protected WithEvents txtMiddleName As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtRoom As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtTelephone As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtFax As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtWebsite As System.Web.UI.WebControls.TextBox
	Protected WithEvents eweOtherDetails As EasyWebEditorControl.EasyWebEdit
	Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtPassConf As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtLoginName As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
	Protected WithEvents daTeacherInfo As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents DsTeachers1 As AllSaints.dsTeachers
	Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtAddress As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtAddress2 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCellPhone As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCity As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtState As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtZipCode As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCountry As System.Web.UI.WebControls.TextBox
	Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
	Protected WithEvents TeacherImageFile As System.Web.UI.HtmlControls.HtmlInputFile
	Protected WithEvents lblError As System.Web.UI.WebControls.Label
	Protected WithEvents lnkRemovePicture As System.Web.UI.WebControls.LinkButton
	Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
	Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents lblTID As System.Web.UI.WebControls.Label
	Protected WithEvents pnlPassword As System.Web.UI.WebControls.Panel
    Protected WithEvents ddlTitle As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlGradeLevel As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtEducation As System.Web.UI.WebControls.TextBox
    Protected WithEvents ddlExperience As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlCatechesis As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtPersonalRef As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblReqlog As System.Web.UI.WebControls.Label
    Protected WithEvents ddlCategories As System.Web.UI.WebControls.DropDownList
    Dim tid As String

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.daTeacherInfo = New System.Data.SqlClient.SqlDataAdapter
        Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlConn = New System.Data.SqlClient.SqlConnection
        Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
        Me.DsTeachers1 = New AllSaints.dsTeachers
        CType(Me.DsTeachers1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'daTeacherInfo
        '
        Me.daTeacherInfo.InsertCommand = Me.SqlInsertCommand1
        Me.daTeacherInfo.SelectCommand = Me.SqlSelectCommand1
        Me.daTeacherInfo.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetTeacherInfo", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("LoginName", "LoginName"), New System.Data.Common.DataColumnMapping("Password", "Password"), New System.Data.Common.DataColumnMapping("Title", "Title"), New System.Data.Common.DataColumnMapping("FirstName", "FirstName"), New System.Data.Common.DataColumnMapping("MiddleName", "MiddleName"), New System.Data.Common.DataColumnMapping("LastName", "LastName"), New System.Data.Common.DataColumnMapping("Room", "Room"), New System.Data.Common.DataColumnMapping("Address1", "Address1"), New System.Data.Common.DataColumnMapping("Address2", "Address2"), New System.Data.Common.DataColumnMapping("Telephone", "Telephone"), New System.Data.Common.DataColumnMapping("CellPhone", "CellPhone"), New System.Data.Common.DataColumnMapping("Fax", "Fax"), New System.Data.Common.DataColumnMapping("Website", "Website"), New System.Data.Common.DataColumnMapping("ZipCode", "ZipCode"), New System.Data.Common.DataColumnMapping("City", "City"), New System.Data.Common.DataColumnMapping("State", "State"), New System.Data.Common.DataColumnMapping("Country", "Country"), New System.Data.Common.DataColumnMapping("PicturePath", "PicturePath"), New System.Data.Common.DataColumnMapping("Description", "Description"), New System.Data.Common.DataColumnMapping("GradeLevel", "GradeLevel"), New System.Data.Common.DataColumnMapping("Education", "Education"), New System.Data.Common.DataColumnMapping("Experience", "Experience"), New System.Data.Common.DataColumnMapping("Catechesis", "Catechesis"), New System.Data.Common.DataColumnMapping("PersonalReflection", "PersonalReflection")}), New System.Data.Common.DataTableMapping("Table1", "Table1", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("LoginName", "LoginName"), New System.Data.Common.DataColumnMapping("Password", "Password"), New System.Data.Common.DataColumnMapping("Title", "Title"), New System.Data.Common.DataColumnMapping("FirstName", "FirstName"), New System.Data.Common.DataColumnMapping("MiddleName", "MiddleName"), New System.Data.Common.DataColumnMapping("LastName", "LastName"), New System.Data.Common.DataColumnMapping("Room", "Room"), New System.Data.Common.DataColumnMapping("Address1", "Address1"), New System.Data.Common.DataColumnMapping("Address2", "Address2"), New System.Data.Common.DataColumnMapping("Telephone", "Telephone"), New System.Data.Common.DataColumnMapping("CellPhone", "CellPhone"), New System.Data.Common.DataColumnMapping("Fax", "Fax"), New System.Data.Common.DataColumnMapping("Website", "Website"), New System.Data.Common.DataColumnMapping("ZipCode", "ZipCode"), New System.Data.Common.DataColumnMapping("City", "City"), New System.Data.Common.DataColumnMapping("State", "State"), New System.Data.Common.DataColumnMapping("Country", "Country"), New System.Data.Common.DataColumnMapping("PicturePath", "PicturePath"), New System.Data.Common.DataColumnMapping("Description", "Description"), New System.Data.Common.DataColumnMapping("GradeLevel", "GradeLevel"), New System.Data.Common.DataColumnMapping("Categories", "Categories"), New System.Data.Common.DataColumnMapping("Education", "Education"), New System.Data.Common.DataColumnMapping("Experience", "Experience"), New System.Data.Common.DataColumnMapping("Catechesis", "Catechesis"), New System.Data.Common.DataColumnMapping("PersonalReflection", "PersonalReflection")})})
        Me.daTeacherInfo.UpdateCommand = Me.SqlUpdateCommand1
        '
        'SqlInsertCommand1
        '
        Me.SqlInsertCommand1.CommandText = "[InsTeacherInfo]"
        Me.SqlInsertCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlInsertCommand1.Connection = Me.SqlConn
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LoginName", System.Data.SqlDbType.VarChar, 100, "LoginName"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 100, "Password"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Title", System.Data.SqlDbType.VarChar, 10, "Title"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 255, "FirstName"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MiddleName", System.Data.SqlDbType.VarChar, 255, "MiddleName"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 255, "LastName"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Room", System.Data.SqlDbType.VarChar, 255, "Room"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address1", System.Data.SqlDbType.VarChar, 255, "Address1"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address2", System.Data.SqlDbType.VarChar, 255, "Address2"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Telephone", System.Data.SqlDbType.VarChar, 50, "Telephone"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CellPhone", System.Data.SqlDbType.VarChar, 50, "CellPhone"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.VarChar, 50, "Fax"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Website", System.Data.SqlDbType.VarChar, 255, "Website"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ZipCode", System.Data.SqlDbType.VarChar, 10, "ZipCode"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.VarChar, 50, "City"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@State", System.Data.SqlDbType.VarChar, 50, "State"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.VarChar, 50, "Country"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PicturePath", System.Data.SqlDbType.VarChar, 255, "PicturePath"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.Text, 0, "Description"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@GradeLevel", System.Data.SqlDbType.VarChar, 50, "GradeLevel"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Categories", System.Data.SqlDbType.VarChar, 50, "Categories"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Education", System.Data.SqlDbType.Text, 0, "Education"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Experience", System.Data.SqlDbType.VarChar, 100, "Experience"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Catechesis", System.Data.SqlDbType.VarChar, 100, "Catechesis"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PersonalReflection", System.Data.SqlDbType.Text, 0, "PersonalReflection"))
        '
        'SqlConn
        '
        Me.SqlConn.ConnectionString = "data source=localhost;initial catalog=Allsaints;persist security info=False;packe" & _
        "t size=4096"
        '
        'SqlSelectCommand1
        '
        Me.SqlSelectCommand1.CommandText = "[GetTeacherInfo]"
        Me.SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlSelectCommand1.Connection = Me.SqlConn
        Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ID", System.Data.SqlDbType.VarChar, 100))
        '
        'SqlUpdateCommand1
        '
        Me.SqlUpdateCommand1.CommandText = "[SetTeacherInfo]"
        Me.SqlUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlUpdateCommand1.Connection = Me.SqlConn
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Title", System.Data.SqlDbType.VarChar, 10, "Title"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 255, "FirstName"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MiddleName", System.Data.SqlDbType.VarChar, 255, "MiddleName"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 255, "LastName"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Room", System.Data.SqlDbType.VarChar, 255, "Room"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address1", System.Data.SqlDbType.VarChar, 255, "Address1"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address2", System.Data.SqlDbType.VarChar, 255, "Address2"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Telephone", System.Data.SqlDbType.VarChar, 50, "Telephone"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CellPhone", System.Data.SqlDbType.VarChar, 50, "CellPhone"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.VarChar, 50, "Fax"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Website", System.Data.SqlDbType.VarChar, 255, "Website"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ZipCode", System.Data.SqlDbType.VarChar, 10, "ZipCode"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.VarChar, 50, "City"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@State", System.Data.SqlDbType.VarChar, 50, "State"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.VarChar, 50, "Country"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PicturePath", System.Data.SqlDbType.VarChar, 255, "PicturePath"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.Text, 0, "Description"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@GradeLevel", System.Data.SqlDbType.VarChar, 50, "GradeLevel"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Categories", System.Data.SqlDbType.VarChar, 50, "Categories"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Education", System.Data.SqlDbType.Text, 0, "Education"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Experience", System.Data.SqlDbType.VarChar, 100, "Experience"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Catechesis", System.Data.SqlDbType.VarChar, 100, "Catechesis"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PersonalReflection", System.Data.SqlDbType.Text, 0, "PersonalReflection"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_LoginName", System.Data.SqlDbType.VarChar, 100, "LoginName"))
        '
        'DsTeachers1
        '
        Me.DsTeachers1.DataSetName = "dsTeachers"
        Me.DsTeachers1.Locale = New System.Globalization.CultureInfo("en-US")
        CType(Me.DsTeachers1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
		If SqlConn.State = ConnectionState.Closed Then
			SqlConn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
		End If
	End Sub

#End Region

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		If SqlConn.State = ConnectionState.Closed Then SqlConn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
        lnkRemovePicture.Attributes.Add("onclick", "return confirm('Are you sure?');")
        Dim cmd As New SqlCommand
        With cmd
            .CommandText = "select distinct ClassName from SMSClass"
            .CommandType = CommandType.Text
            .Connection = SqlConn
            If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
            Dim rdr As SqlDataReader = .ExecuteReader()
            Dim itemValue As String
            While rdr.Read
                'Response.Write(rdr.Item("ClassName"))
                itemValue = CType(rdr.Item("ClassName"), String)
                ddlGradeLevel.Items.Add(New ListItem(itemValue, itemValue))
            End While
            rdr.Close()
        End With
        If Not Page.IsPostBack Then
            tid = Request("TID")
            If tid = "" Then
                lblMessage.Text = "<Strong>Create New Teacher Profile</Strong>"
            Else
                lblMessage.Text = "<Strong>Edit Teacher Profile</Strong>"
                daTeacherInfo.SelectCommand.Parameters("@ID").Value = tid
                daTeacherInfo.Fill(DsTeachers1)
            End If

            If DsTeachers1.Tables.Count > 0 AndAlso DsTeachers1.Tables(0).Rows.Count > 0 Then
                'Response.Write(DsTeachers1.Tables(0).Rows(0).Item(0).ToString)
                eweOtherDetails.HTMLValue = DsTeachers1.Tables(0).Rows(0).Item("Description").ToString

                If DsTeachers1.Tables(0).Rows(0).Item("PicturePath").ToString <> "" Then
                    imgTeacher.ImageUrl = "../contents/teachers/" & DsTeachers1.Tables(0).Rows(0).Item("PicturePath").ToString
                    imgTeacher.Visible = True
                    lnkRemovePicture.Visible = True
                End If
                pnlPassword.Visible = False
                txtLoginName.Visible = False
                lblReqlog.Visible = False
                lblTID.Text = tid
                lblTID.Visible = True
                If DsTeachers1.Tables(0).Rows(0).Item("Title").ToString = "" Or DsTeachers1.Tables(0).Rows(0).Item("Title").ToString = "Mr" Or DsTeachers1.Tables(0).Rows(0).Item("Title").ToString = "Ms" Or DsTeachers1.Tables(0).Rows(0).Item("Title").ToString = "Mrs" Then
                    ddlTitle.Items.FindByValue(DsTeachers1.Tables(0).Rows(0).Item("Title").ToString).Selected = True
                Else
                    ddlTitle.Items.FindByValue("").Selected = True
                End If
                ddlGradeLevel.Items.FindByValue(DsTeachers1.Tables(0).Rows(0).Item("GradeLevel").ToString).Selected = True
                ddlCategories.Items.FindByValue(DsTeachers1.Tables(0).Rows(0).Item("Categories").ToString).Selected = True
                ddlExperience.Items.FindByValue(DsTeachers1.Tables(0).Rows(0).Item("Experience").ToString).Selected = True
                ddlCatechesis.Items.FindByValue(DsTeachers1.Tables(0).Rows(0).Item("Catechesis").ToString).Selected = True

                Page.DataBind()
            End If
        End If

        '<SAQIB ADDED LINES>
        If SqlConn.State = ConnectionState.Open Then SqlConn.Close()
        '<SAQIB ADDED LINES/>
	End Sub

	Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
		If SqlConn.State = ConnectionState.Open Then SqlConn.Close()
    End Sub
    Private Sub btnSaveChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveChanges.Click
        Dim sFilename As String = ""

        'TID lable will not be visible if new teacher creation
        If lblTID.Visible = False Then
            If txtLoginName.Text.Trim.Length <= 0 Then
                lblError.Text = "<p>Teacher Login Name or Email address was not entered!</p>"
                lblError.Visible = True
                Exit Sub
            End If

            If txtPassword.Text.Trim.Length <= 0 Or txtPassConf.Text.Trim.Length <= 0 Then
                lblError.Text = "<p>Password was not entered!</p>"
                lblError.Visible = True
                Exit Sub
            End If

            If txtPassword.Text <> txtPassConf.Text Then
                lblError.Text = "<p>Password and Confirm Password did not match!</p>"
                lblError.Visible = True
                Exit Sub
            End If

            'now upload the NEW posted file from client
            With TeacherImageFile.PostedFile
                If .FileName.Trim <> "" And .ContentLength > 0 Then
                    Select Case .ContentType.Trim.ToLower
                        Case "image/gif", "image/pjpg", "image/jpeg", "image/pjpeg", "image/jpg", "image/bmp", "image/x-png", "image/png"
                            Try
                                sFilename = System.IO.Path.GetExtension(.FileName)
                                sFilename = txtLoginName.Text & sFilename
                                .SaveAs(Server.MapPath("..") & "\contents\teachers\" & sFilename)
                            Catch ex As Exception
                                lblError.Text = "<p>Error Occured, Unable to upload image file.</p>"       '& vbCrLf & ex.ToString
                                lblError.Visible = True
                                Exit Sub
                            End Try
                    End Select
                End If
            End With

            With daTeacherInfo.InsertCommand
                .Parameters("@LoginName").Value = txtLoginName.Text
                .Parameters("@Password").Value = txtPassword.Text
                .Parameters("@Title").Value = ddlTitle.SelectedValue
                .Parameters("@FirstName").Value = txtFirstName.Text
                .Parameters("@MiddleName").Value = txtMiddleName.Text
                .Parameters("@LastName").Value = txtLastName.Text
                .Parameters("@Room").Value = txtRoom.Text
                .Parameters("@Address1").Value = txtAddress.Text
                .Parameters("@Address2").Value = txtAddress2.Text
                .Parameters("@Telephone").Value = txtTelephone.Text
                .Parameters("@CellPhone").Value = txtCellPhone.Text
                .Parameters("@Fax").Value = txtFax.Text
                .Parameters("@Website").Value = txtWebsite.Text
                .Parameters("@ZipCode").Value = txtZipCode.Text
                .Parameters("@City").Value = txtCity.Text
                .Parameters("@State").Value = txtState.Text
                .Parameters("@Country").Value = txtCountry.Text
                .Parameters("@PicturePath").Value = sFilename
                .Parameters("@Description").Value = eweOtherDetails.HTMLValue
                .Parameters("@GradeLevel").Value = ddlGradeLevel.SelectedValue
                .Parameters("@Categories").Value = ddlCategories.SelectedValue
                .Parameters("@Education").Value = txtEducation.Text
                .Parameters("@Experience").Value = ddlExperience.SelectedValue
                .Parameters("@Catechesis").Value = ddlCatechesis.SelectedValue
                .Parameters("@PersonalReflection").Value = txtPersonalRef.Text
                If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
                .Connection = SqlConn
                Try
                    .ExecuteNonQuery()
                Catch ex As Exception
                    lblError.Text = "<p>Teacher account already exists. Please provide unique login name or teacher email address!</p>"    '& vbCrLf & ex.ToString
                    lblError.Visible = True
                    Exit Sub
                End Try
            End With
            '********************************************************************
            'else it is the case with update teacher info
        Else
            'get the old image file name to be used in the bottom update query to persist image name
            Dim cmd As New SqlCommand
            With cmd
                .CommandText = "Select PicturePath from as_Teachers where LoginName='" & lblTID.Text & "'"
                .CommandType = CommandType.Text
                .Connection = SqlConn
                If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
                Dim objName As Object = .ExecuteScalar
                If IsDBNull(objName) Then
                    sFilename = ""
                Else
                    sFilename = CType(objName, String)
                End If

            End With

            With TeacherImageFile.PostedFile
                If .FileName.Trim.Length > 0 And .ContentLength > 0 Then
                    Select Case .ContentType.Trim.ToLower
                        'check for proper image format else show error
                    Case "image/gif", "image/pjpg", "image/jpeg", "image/pjpeg", "image/jpg", "image/bmp", "image/x-png", "image/png"

                            'delete/unlink the old file if in the editing mode if any proper new image is given
                            Try
                                System.IO.File.Delete(Server.MapPath("..") & "\contents\teachers\" & sFilename)
                            Catch ex As Exception
                                'do nothing if cannot delete old image file
                            End Try

                            'now upload the NEW posted file from client
                            Try
                                sFilename = System.IO.Path.GetExtension(.FileName)
                                sFilename = txtLoginName.Text & sFilename
                                .SaveAs(Server.MapPath("..") & "\contents\teachers\" & sFilename)
                            Catch ex As Exception
                                lblError.Text = "<p>Error Occured, Unable to upload image file.</p>"       '& vbCrLf & ex.ToString
                                lblError.Visible = True
                                Exit Sub
                            End Try
                        Case Else
                            lblError.Text = "<p>The format of newly provided image is not supported.</p>"
                            lblError.Visible = True
                            Exit Sub
                    End Select
                End If
            End With
            With daTeacherInfo.UpdateCommand
                .Parameters("@Original_LoginName").Value = lblTID.Text
                .Parameters("@Title").Value = ddlTitle.SelectedValue
                .Parameters("@FirstName").Value = txtFirstName.Text
                .Parameters("@MiddleName").Value = txtMiddleName.Text
                .Parameters("@LastName").Value = txtLastName.Text
                .Parameters("@Room").Value = txtRoom.Text
                .Parameters("@Address1").Value = txtAddress.Text
                .Parameters("@Address2").Value = txtAddress2.Text
                .Parameters("@Telephone").Value = txtTelephone.Text
                .Parameters("@CellPhone").Value = txtCellPhone.Text
                .Parameters("@Fax").Value = txtFax.Text
                .Parameters("@Website").Value = txtWebsite.Text
                .Parameters("@ZipCode").Value = txtZipCode.Text
                .Parameters("@City").Value = txtCity.Text
                .Parameters("@State").Value = txtState.Text
                .Parameters("@Country").Value = txtCountry.Text
                .Parameters("@PicturePath").Value = sFilename
                .Parameters("@Description").Value = eweOtherDetails.HTMLValue
                .Parameters("@GradeLevel").Value = ddlGradeLevel.SelectedValue
                .Parameters("@Categories").Value = ddlCategories.SelectedValue
                .Parameters("@Education").Value = txtEducation.Text
                .Parameters("@Experience").Value = ddlExperience.SelectedValue
                .Parameters("@Catechesis").Value = ddlCatechesis.SelectedValue
                .Parameters("@PersonalReflection").Value = txtPersonalRef.Text
                If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
                Try
                    .ExecuteNonQuery()
                Catch ex As Exception
                    lblError.Text = "<p>Error Occured, Unable to update the teacher information!</p>"    '& vbCrLf & ex.ToString
                    lblError.Visible = True
                    Exit Sub
                End Try
            End With
        End If

        '<SAQIB ADDED LINES>
        If SqlConn.State = ConnectionState.Open Then SqlConn.Close()
        '<SAQIB ADDED LINES/>

        Response.Redirect("Teacher_Info.aspx?TID=" & txtLoginName.Text, True)
    End Sub


    'Remove the teachers picture
    Private Sub lnkRemovePicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkRemovePicture.Click
        Dim cmd As New SqlCommand
        With cmd
            .CommandText = "Select PicturePath from as_Teachers where LoginName='" & lblTID.Text & "'"
            .CommandType = CommandType.Text
            .Connection = SqlConn
            If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
            Dim sFilename As String = CType(.ExecuteScalar, String)

            If sFilename.Length > 0 Then
                Try
                    System.IO.File.Delete(Server.MapPath("..") & "\contents\teachers\" & sFilename)
                Catch ex As Exception
                    'do nothing
                End Try
                .CommandText = "Update as_Teachers Set PicturePath='' where LoginName='" & lblTID.Text & "'"
                .CommandType = CommandType.Text
                .Connection = SqlConn
                If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
                .ExecuteNonQuery()
                imgTeacher.ImageUrl = ""
                imgTeacher.Visible = False
                lnkRemovePicture.Visible = False
                'referesh the page data
                'Call GetContactData()
                'Call GetData()
            End If
        End With

        '<SAQIB ADDED LINES>
        If SqlConn.State = ConnectionState.Open Then SqlConn.Close()
        '<SAQIB ADDED LINES/>
    End Sub

End Class