Option Explicit On 
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Public Class frmMgrPicnic
	Inherits System.Web.UI.Page
	Protected WithEvents lnkExport As System.Web.UI.WebControls.LinkButton
	Protected WithEvents Conn As System.Data.SqlClient.SqlConnection
	Protected WithEvents DA_AllPicnic As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents DG_Picnic As System.Web.UI.WebControls.DataGrid
	Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents DS_AllPicnic1 As AllSaints.DS_AllPicnic
	Protected WithEvents Counter1 As AllSaints.Counter

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.DA_AllPicnic = New System.Data.SqlClient.SqlDataAdapter()
		Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
		Me.Conn = New System.Data.SqlClient.SqlConnection()
		Me.DS_AllPicnic1 = New AllSaints.DS_AllPicnic()
		CType(Me.DS_AllPicnic1, System.ComponentModel.ISupportInitialize).BeginInit()
		'
		'DA_AllPicnic
		'
		Me.DA_AllPicnic.SelectCommand = Me.SqlSelectCommand1
		Me.DA_AllPicnic.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetAllPicnic", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ID", "ID"), New System.Data.Common.DataColumnMapping("LastName", "LastName"), New System.Data.Common.DataColumnMapping("AttendingsAdult", "AttendingsAdult"), New System.Data.Common.DataColumnMapping("AttendingsChildren", "AttendingsChildren"), New System.Data.Common.DataColumnMapping("PhoneExt", "PhoneExt"), New System.Data.Common.DataColumnMapping("Phone", "Phone"), New System.Data.Common.DataColumnMapping("Alumnus", "Alumnus"), New System.Data.Common.DataColumnMapping("GraduateYear", "GraduateYear"), New System.Data.Common.DataColumnMapping("MaidenName", "MaidenName"), New System.Data.Common.DataColumnMapping("Former", "Former"), New System.Data.Common.DataColumnMapping("FormerPosition", "FormerPosition"), New System.Data.Common.DataColumnMapping("FormerYears", "FormerYears"), New System.Data.Common.DataColumnMapping("Comments", "Comments"), New System.Data.Common.DataColumnMapping("PostDate", "PostDate"), New System.Data.Common.DataColumnMapping("IP", "IP")})})
		'
		'SqlSelectCommand1
		'
		Me.SqlSelectCommand1.CommandText = "[GetAllPicnic]"
		Me.SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure
		Me.SqlSelectCommand1.Connection = Me.Conn
		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))
		'
		'Conn
		'
		Me.Conn.ConnectionString = "data source=GALAXY;initial catalog=Online_allsaints;persist security info=False;u" & _
		"ser id=sa;workstation id=GALAXY;packet size=4096"
		'
		'DS_AllPicnic1
		'
		Me.DS_AllPicnic1.DataSetName = "DS_AllPicnic"
		Me.DS_AllPicnic1.Locale = New System.Globalization.CultureInfo("en-US")
		Me.DS_AllPicnic1.Namespace = "http://www.tempuri.org/DS_AllPicnic.xsd"
		CType(Me.DS_AllPicnic1, 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 Conn.State = ConnectionState.Closed Then
			Conn.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 Not Page.IsPostBack Then
			Counter1.PageID = 470		 '459 the local id
			DA_AllPicnic.Fill(DS_AllPicnic1)
			DG_Picnic.DataSource = New DataView(DS_AllPicnic1.Tables(0))
			DG_Picnic.DataBind()
		End If
	End Sub

	'**************************************************************************
	'	Data gird has a new item created now
	'**************************************************************************
	Private Sub DG_Picnic_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_Picnic.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 Alumni records"
			End With
			e.Item.Cells(1).Controls.Add(btnDel)
		End If

		If e.Item.ItemType = ListItemType.Pager Then
			If (DS_AllPicnic1 Is Nothing) AndAlso DS_AllPicnic1.Tables(0).Rows.Count < DG_Picnic.PageSize Then
				e.Item.Visible = False
			End If
		End If
	End Sub

	'**************************************************************************
	'	Get this page Data to display is now 
	'**************************************************************************
	Private Sub DG_Picnic_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DG_Picnic.PageIndexChanged
		DG_Picnic.CurrentPageIndex = e.NewPageIndex
		DA_AllPicnic.Fill(DS_AllPicnic1)
		DG_Picnic.DataSource = New DataView(DS_AllPicnic1.Tables(0), "", CType(ViewState("SortBy"), String) & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
		DG_Picnic.DataBind()
	End Sub

	'**************************************************************************
	'	Data gird sorting is now cliched
	'**************************************************************************
	Private Sub DG_Picnic_SortCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DG_Picnic.SortCommand
		DA_AllPicnic.Fill(DS_AllPicnic1)
		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_Picnic.DataSource = New DataView(DS_AllPicnic1.Tables(0), "", e.SortExpression & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
		DG_Picnic.DataBind()
	End Sub

	'**************************************************************************
	'	A child command has been fired up by a control in the Data gird
	'**************************************************************************
	Private Sub DG_Picnic_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_Picnic.ItemCommand
		If e.CommandName = "DeleteAllSelected" Then
			Dim dgi As DataGridItem
			For Each dgi In DG_Picnic.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 = "Delete from as_PicnicForm where ID=" & CType(cb.ToolTip, Integer)
							.CommandType = CommandType.Text
							If Conn.State = ConnectionState.Closed Then Conn.Open()
							.Connection = Conn
							.ExecuteNonQuery()
						End With
					End If
				End If
			Next
			DA_AllPicnic.Fill(DS_AllPicnic1)
			DG_Picnic.DataSource = New DataView(DS_AllPicnic1.Tables(0), "", CType(ViewState("SortBy"), String) & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
			DG_Picnic.DataBind()
		Else
			'Response.Write(e.CommandName)
		End If

	End Sub

	'**************************************************************************
	'	A Export of Alumni data 
	'**************************************************************************
	Private Sub lnkExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkExport.Click
		Response.ClearHeaders()
		Response.AddHeader("Content-Type", "text/csv")
		Response.AddHeader("Content-Disposition", "attachment; filename=AlumniData.csv")
		DA_AllPicnic.Fill(DS_AllPicnic1)

		Dim dr As DataRow
		Dim dc As DataColumn
		Dim objText As Object
		Dim sText As String
		Dim AlumniID As Integer = 0
		Dim dtc As DataColumn
		For Each dtc In DS_AllPicnic1.Tables(0).Columns
			Response.Write(dtc.ColumnName() & ",")
		Next
		Response.Write(vbCrLf)
		'now write the each every record by traversing columns
		For Each dr In DS_AllPicnic1.Tables(0).Rows
			For Each dc In DS_AllPicnic1.Tables(0).Columns
				objText = dr.Item(dc)
				'get the id of this sensus if the column is [ID]
				If dc.ColumnName.Trim.ToUpper = "ID" Then
					AlumniID = CType(objText, Integer)
				End If

				If IsDBNull(objText) Then
					Response.Write(",")
				Else
					sText = CType(objText, String)
					sText = Replace(sText, Chr(13), "\n")
					sText = Replace(sText, Chr(10), "\r")
					Response.Write(Chr(34) & CType(sText, String) & Chr(34) & ",")
				End If
			Next
			Response.Write(vbCrLf)
		Next
		Response.End()
	End Sub

End Class
