Option Explicit On 
Option Strict On

Imports System.Data.SqlClient

Public Class AddRelatedContact
	Inherits System.Web.UI.UserControl
	Protected WithEvents btnSave As System.Web.UI.WebControls.Button
	Protected WithEvents txtHeading As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtInfo As System.Web.UI.WebControls.TextBox
	Protected WithEvents cmdPC_Select As System.Data.SqlClient.SqlCommand
	Protected WithEvents cmdPC_Update As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
	Protected WithEvents lblErrorMsg As System.Web.UI.WebControls.Label
	Protected WithEvents daPageContacts As System.Data.SqlClient.SqlDataAdapter

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.daPageContacts = New System.Data.SqlClient.SqlDataAdapter()
		Me.cmdPC_Update = New System.Data.SqlClient.SqlCommand()
		Me.SqlConn = New System.Data.SqlClient.SqlConnection()
		Me.cmdPC_Select = New System.Data.SqlClient.SqlCommand()
		'
		'daPageContacts
		'
		Me.daPageContacts.InsertCommand = Me.cmdPC_Update
		Me.daPageContacts.SelectCommand = Me.cmdPC_Select
		Me.daPageContacts.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetRelContacts", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ID", "ID"), New System.Data.Common.DataColumnMapping("Page_ID", "Page_ID"), New System.Data.Common.DataColumnMapping("Heading", "Heading"), New System.Data.Common.DataColumnMapping("InfoText", "InfoText")})})
		'
		'cmdPC_Update
		'
		Me.cmdPC_Update.CommandText = "[InsRelContacts]"
		Me.cmdPC_Update.CommandType = System.Data.CommandType.StoredProcedure
		Me.cmdPC_Update.Connection = Me.SqlConn
		Me.cmdPC_Update.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.cmdPC_Update.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "Page_ID", System.Data.DataRowVersion.Current, Nothing))
		Me.cmdPC_Update.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Heading", System.Data.SqlDbType.VarChar, 255, "Heading"))
		Me.cmdPC_Update.Parameters.Add(New System.Data.SqlClient.SqlParameter("@InfoText", System.Data.SqlDbType.VarChar, 255, "InfoText"))
		'
		'SqlConn
		'
		Me.SqlConn.ConnectionString = "data source=localhost;initial catalog=allsaints;persist security info=False;user " & _
		"id=sa;workstation id=GALAXY;packet size=4096"
		'
		'cmdPC_Select
		'
		Me.cmdPC_Select.CommandText = "[GetRelContacts]"
		Me.cmdPC_Select.CommandType = System.Data.CommandType.StoredProcedure
		Me.cmdPC_Select.Connection = Me.SqlConn
		Me.cmdPC_Select.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.cmdPC_Select.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))

	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
	Public Event ContactAdded()
	Public Property PageID() As Integer
		Get
			Return _PageID
		End Get
		Set(ByVal Value As Integer)
			_PageID = Value
		End Set
	End Property

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
		SqlConn.ConnectionString = sConStr
	End Sub

	Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If (Trim(txtHeading.Text) = "" Or Trim(txtInfo.Text) = "") Then
			lblErrorMsg.Text = "Contact heading or contact information 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 daPageContacts.InsertCommand
				.Parameters("@PageID").Value = _PageID
				.Parameters("@Heading").Value = Trim(txtHeading.Text)
				.Parameters("@InfoText").Value = Trim(txtInfo.Text)
				If .Connection.State = ConnectionState.Closed Then .Connection.Open()
				.ExecuteNonQuery()
			End With
			txtHeading.Text = ""
			txtInfo.Text = ""
			RaiseEvent ContactAdded()
		End If
	End Sub
End Class