Option Explicit On 
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Public Class MgrTeachers
	Inherits System.Web.UI.Page
	Protected WithEvents daTeachers As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
	Protected WithEvents cmdSelect As System.Data.SqlClient.SqlCommand
	Protected WithEvents cmdInsert As System.Data.SqlClient.SqlCommand
	Protected WithEvents cndDelete As System.Data.SqlClient.SqlCommand
	Protected WithEvents cmdUpdate As System.Data.SqlClient.SqlCommand
	Protected WithEvents DsTeachers1 As AllSaints.dsTeachers
	Protected WithEvents lnkExport As System.Web.UI.WebControls.LinkButton
	Protected WithEvents DG_Teachers As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.daTeachers = New System.Data.SqlClient.SqlDataAdapter()
		Me.cndDelete = New System.Data.SqlClient.SqlCommand()
		Me.SqlConn = New System.Data.SqlClient.SqlConnection()
		Me.cmdInsert = New System.Data.SqlClient.SqlCommand()
		Me.cmdSelect = New System.Data.SqlClient.SqlCommand()
		Me.cmdUpdate = New System.Data.SqlClient.SqlCommand()
		Me.DsTeachers1 = New AllSaints.dsTeachers()
		CType(Me.DsTeachers1, System.ComponentModel.ISupportInitialize).BeginInit()
		'
		'daTeachers
		'
		Me.daTeachers.DeleteCommand = Me.cndDelete
		Me.daTeachers.InsertCommand = Me.cmdInsert
		Me.daTeachers.SelectCommand = Me.cmdSelect
		Me.daTeachers.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("Country", "Country"), New System.Data.Common.DataColumnMapping("PicturePath", "PicturePath"), New System.Data.Common.DataColumnMapping("Description", "Description")})})
		Me.daTeachers.UpdateCommand = Me.cmdUpdate
		'
		'cndDelete
		'
		Me.cndDelete.CommandText = "[DelTeacherInfo]"
		Me.cndDelete.CommandType = System.Data.CommandType.StoredProcedure
		Me.cndDelete.Connection = Me.SqlConn
		Me.cndDelete.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.cndDelete.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_LoginName", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "LoginName", System.Data.DataRowVersion.Original, Nothing))
		'
		'SqlConn
		'
		Me.SqlConn.ConnectionString = "data source=Localhost;initial catalog=Allsaints;persist security info=False;user " & _
		"id=sa;packet size=4096"
		'
		'cmdInsert
		'
		Me.cmdInsert.CommandText = "[InsTeacherInfo]"
		Me.cmdInsert.CommandType = System.Data.CommandType.StoredProcedure
		Me.cmdInsert.Connection = Me.SqlConn
		Me.cmdInsert.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.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LoginName", System.Data.SqlDbType.VarChar, 50, "LoginName"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 50, "Password"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Title", System.Data.SqlDbType.VarChar, 10, "Title"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 255, "FirstName"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MiddleName", System.Data.SqlDbType.VarChar, 255, "MiddleName"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 255, "LastName"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Room", System.Data.SqlDbType.VarChar, 255, "Room"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address1", System.Data.SqlDbType.VarChar, 255, "Address1"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address2", System.Data.SqlDbType.VarChar, 255, "Address2"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Telephone", System.Data.SqlDbType.VarChar, 50, "Telephone"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CellPhone", System.Data.SqlDbType.VarChar, 50, "CellPhone"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.VarChar, 50, "Fax"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Website", System.Data.SqlDbType.VarChar, 255, "Website"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ZipCode", System.Data.SqlDbType.VarChar, 10, "ZipCode"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.VarChar, 50, "City"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.VarChar, 50, "Country"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PicturePath", System.Data.SqlDbType.VarChar, 255, "PicturePath"))
		Me.cmdInsert.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.VarChar, 2147483647, "Description"))
		'
		'cmdSelect
		'
		Me.cmdSelect.CommandText = "[GetTeacherInfo]"
		Me.cmdSelect.CommandType = System.Data.CommandType.StoredProcedure
		Me.cmdSelect.Connection = Me.SqlConn
		Me.cmdSelect.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.cmdSelect.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ID", System.Data.SqlDbType.VarChar, 10))
		'
		'cmdUpdate
		'
		Me.cmdUpdate.CommandText = "[SetTeacherInfo]"
		Me.cmdUpdate.CommandType = System.Data.CommandType.StoredProcedure
		Me.cmdUpdate.Connection = Me.SqlConn
		Me.cmdUpdate.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.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 50, "Password"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Title", System.Data.SqlDbType.VarChar, 10, "Title"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.VarChar, 255, "FirstName"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@MiddleName", System.Data.SqlDbType.VarChar, 255, "MiddleName"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.VarChar, 255, "LastName"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Room", System.Data.SqlDbType.VarChar, 255, "Room"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address1", System.Data.SqlDbType.VarChar, 255, "Address1"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Address2", System.Data.SqlDbType.VarChar, 255, "Address2"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Telephone", System.Data.SqlDbType.VarChar, 50, "Telephone"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CellPhone", System.Data.SqlDbType.VarChar, 50, "CellPhone"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.VarChar, 50, "Fax"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Website", System.Data.SqlDbType.VarChar, 255, "Website"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ZipCode", System.Data.SqlDbType.VarChar, 10, "ZipCode"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.VarChar, 50, "City"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.VarChar, 50, "Country"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PicturePath", System.Data.SqlDbType.VarChar, 255, "PicturePath"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.VarChar, 2147483647, "Description"))
		Me.cmdUpdate.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_LoginName", System.Data.SqlDbType.VarChar, 50, "LoginName"))
		'
		'DsTeachers1
		'
		Me.DsTeachers1.DataSetName = "dsTeachers"
		Me.DsTeachers1.Locale = New System.Globalization.CultureInfo("en-US")
		Me.DsTeachers1.Namespace = "http://www.tempuri.org/dsTeachers.xsd"
		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")
		If Not Page.IsPostBack Then
			daTeachers.SelectCommand.Parameters("@ID").Value = ""
			daTeachers.Fill(DsTeachers1)
			DG_Teachers.DataSource = New DataView(DsTeachers1.Tables(0))
			DG_Teachers.DataBind()
		End If

	End Sub

	'**************************************************************************
	'	Get this page Data to display is now 
	'**************************************************************************
	Private Sub DG_Teachers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DG_Teachers.SelectedIndexChanged

	End Sub

	Private Sub DG_Teachers_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DG_Teachers.PageIndexChanged
		DG_Teachers.CurrentPageIndex = e.NewPageIndex
		daTeachers.SelectCommand.Parameters("@ID").Value = ""
		daTeachers.Fill(DsTeachers1)
		DG_Teachers.DataSource = New DataView(DsTeachers1.Tables(0), "", CType(ViewState("SortBy"), String) & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
		DG_Teachers.DataBind()
	End Sub

	'**************************************************************************
	'	Data gird has a new item created now
	'**************************************************************************
	Private Sub DG_Teachers_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_Teachers.ItemCreated
		If e.Item.ItemType = ListItemType.Footer Then
			Dim btnDel As New Button()
			With btnDel
				.ID = "DeleteSelected"
				.Attributes.Add("onclick", "return confirm('Are you sure ?');")
				.CommandName = "DeleteAllSelected"
				.Text = "Delete Selected"
				.Font.Name = "Verdana"
				.ToolTip = "Click this to delete all selected records"
			End With
			e.Item.Cells(1).Controls.Add(btnDel)
		End If

		If e.Item.ItemType = ListItemType.Pager Then
			If DsTeachers1.Tables(0).Rows.Count < DG_Teachers.PageSize Then
				e.Item.Visible = False
			End If
		End If
	End Sub

	'**************************************************************************
	'	A child command has been fired up by a control in the Data gird
	'**************************************************************************
	Private Sub DG_Teachers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_Teachers.ItemCommand
		If e.CommandName = "DeleteAllSelected" Then
			Dim dgi As DataGridItem
			For Each dgi In DG_Teachers.Items
				If dgi.ItemType = ListItemType.Item Or dgi.ItemType = ListItemType.AlternatingItem Then
					Dim cb As CheckBox = CType(dgi.Cells(0).Controls(1), CheckBox)
					If cb.Checked Then
						Dim cmd As New SqlCommand()
						With cmd
							.CommandText = "Select PicturePath from as_Teachers where LoginName='" & cb.ToolTip.ToString & "'"
							.CommandType = CommandType.Text
							.Connection = SqlConn
							If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()

							Dim oValue As Object = .ExecuteScalar
							If Not IsDBNull(oValue) Then
								If oValue.ToString.Trim.Length > 0 Then
									Try
										System.IO.File.Delete(Server.MapPath("..") & "/contents/teachers/" & oValue.ToString)
									Catch ex As Exception
										'do nothing
									End Try
								End If
							End If

							.CommandText = "Delete from as_Teachers where LoginName='" & cb.ToolTip.ToString & "'"
							Try
								.ExecuteNonQuery()
							Catch ex As Exception
								'do nothing
							End Try
						End With
					End If
				End If
			Next
			daTeachers.SelectCommand.Parameters("@ID").Value = ""
			daTeachers.Fill(DsTeachers1)
			DG_Teachers.DataSource = New DataView(DsTeachers1.Tables(0), "", CType(ViewState("SortBy"), String) & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
			DG_Teachers.DataBind()
		Else
			'Response.Write(e.CommandName)
		End If
	End Sub

	'**************************************************************************
	'	A Sort commnad Data gird
	'**************************************************************************
	Private Sub DG_Teachers_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DG_Teachers.SortCommand
		daTeachers.SelectCommand.Parameters("@ID").Value = ""
		daTeachers.Fill(DsTeachers1)
		ViewState("SortBy") = e.SortExpression
		If CType(ViewState("Direction"), String) = "" Then
			ViewState("Direction") = " ASC "
		ElseIf CType(ViewState("Direction"), String).Trim = "ASC" Then
			ViewState("Direction") = " DESC "
		Else
			ViewState("Direction") = " ASC "
		End If
		DG_Teachers.DataSource = New DataView(DsTeachers1.Tables(0), "", e.SortExpression & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
		DG_Teachers.DataBind()

	End Sub

End Class
