Option Explicit On 
Option Strict On

Imports System.Data
Imports System.Data.SqlClient
Imports System.DateTime

Public Class AddEvent
	Inherits System.Web.UI.UserControl
	Protected WithEvents txtEventMsg As System.Web.UI.WebControls.TextBox
	Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
	Protected WithEvents daEvents As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents cmdGetEvent As System.Data.SqlClient.SqlCommand
	Protected WithEvents cmdInsEvent As System.Data.SqlClient.SqlCommand
	Protected WithEvents lblErrorMsg As System.Web.UI.WebControls.Label
	Protected WithEvents btnSave As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.daEvents = New System.Data.SqlClient.SqlDataAdapter()
		Me.cmdInsEvent = New System.Data.SqlClient.SqlCommand()
		Me.SqlConn = New System.Data.SqlClient.SqlConnection()
		Me.cmdGetEvent = New System.Data.SqlClient.SqlCommand()
		'
		'daEvents
		'
		Me.daEvents.InsertCommand = Me.cmdInsEvent
		Me.daEvents.SelectCommand = Me.cmdGetEvent
		Me.daEvents.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetEvents", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("EventID", "EventID"), New System.Data.Common.DataColumnMapping("PageID", "PageID"), New System.Data.Common.DataColumnMapping("EventDate", "EventDate"), New System.Data.Common.DataColumnMapping("Details", "Details")})})
		'
		'cmdInsEvent
		'
		Me.cmdInsEvent.CommandText = "[InsEvents]"
		Me.cmdInsEvent.CommandType = System.Data.CommandType.StoredProcedure
		Me.cmdInsEvent.Connection = Me.SqlConn
		Me.cmdInsEvent.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.cmdInsEvent.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "PageID", System.Data.DataRowVersion.Current, Nothing))
		Me.cmdInsEvent.Parameters.Add(New System.Data.SqlClient.SqlParameter("@EventDate", System.Data.SqlDbType.DateTime, 8, "EventDate"))
		Me.cmdInsEvent.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Details", System.Data.SqlDbType.VarChar, 7168, "Details"))
		'
		'SqlConn
		'
		Me.SqlConn.ConnectionString = "data source=GALAXY;initial catalog=Online_allsaints;persist security info=False;u" & _
		"ser id=sa;workstation id=GALAXY;packet size=4096"
		'
		'cmdGetEvent
		'
		Me.cmdGetEvent.CommandText = "[GetEvents]"
		Me.cmdGetEvent.CommandType = System.Data.CommandType.StoredProcedure
		Me.cmdGetEvent.Connection = Me.SqlConn
		Me.cmdGetEvent.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.cmdGetEvent.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
		Me.cmdGetEvent.Parameters.Add(New System.Data.SqlClient.SqlParameter("@EventDate", System.Data.SqlDbType.DateTime, 8))

	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()
	End Sub

#End Region
	Dim _PageID As Integer
	Dim _EventDate As Date
	Public Event EventAdded()

	Public Property PageID() As Integer
		Get
			Return _PageID
		End Get
		Set(ByVal Value As Integer)
			_PageID = Value
		End Set
	End Property

	Public Property EventDate() As Date
		Get
			Return _EventDate
		End Get
		Set(ByVal Value As Date)
			_EventDate = Value
		End Set
	End Property

	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")
		End If
	End Sub

	Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If (Trim(txtEventMsg.Text) = "" Or Trim(txtEventMsg.Text) = "") Then
			lblErrorMsg.Text = "New event message cannot be left empty !"
			lblErrorMsg.Visible = True
			Dim s As String = "<script type=""text/javascript"" language=""javascript"">"
			s &= "document.getElementById(""" & Me.ID.ToString & "_btnSave"").focus();"
			s &= "</script>"
			Me.Page.RegisterStartupScript("FocusManager", s)
		Else
			With daEvents.InsertCommand
				.Parameters("@PageID").Value = _PageID
				.Parameters("@Details").Value = Trim(txtEventMsg.Text)
				.Parameters("@EventDate").Value = _EventDate
				If .Connection.State = ConnectionState.Closed Then .Connection.Open()
				.ExecuteNonQuery()
			End With
			txtEventMsg.Text = ""
			RaiseEvent EventAdded()
		End If
	End Sub
End Class