Option Explicit On 
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Public MustInherit Class FormEmails
	Inherits System.Web.UI.UserControl

	Protected WithEvents Update As System.Web.UI.WebControls.Button
	Protected WithEvents txtEmails As System.Web.UI.WebControls.TextBox

	Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))
	Dim cmd As New SqlCommand()
	Dim _FormType As String

	Public Property FormType() As String
		Get
			Return _FormType
		End Get
		Set(ByVal Value As String)
			_FormType = Value
		End Set
	End Property

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

	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

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		If Not Page.IsPostBack Then
			Select Case _FormType
				Case "Census"
					With cmd
						.CommandText = "Select CensusEmails from as_AdminUsers"
						.Connection = conn
						.CommandType = CommandType.Text
						If conn.State = ConnectionState.Closed Then conn.Open()
						Dim a As Object = .ExecuteScalar
						txtEmails.Text = CType(IIf(IsDBNull(a), "", a), String)
					End With

				Case "Alumni"
					With cmd
						.CommandText = "Select AlumniEmails from as_AdminUsers"
						.Connection = conn
						.CommandType = CommandType.Text
						If conn.State = ConnectionState.Closed Then conn.Open()
						Dim a As Object = .ExecuteScalar
						txtEmails.Text = CType(IIf(IsDBNull(a), "", a), String)
					End With

				Case "Picnic"
					With cmd
						.CommandText = "Select PicnicEmails from as_AdminUsers"
						.Connection = conn
						.CommandType = CommandType.Text
						If conn.State = ConnectionState.Closed Then conn.Open()
						Dim a As Object = .ExecuteScalar
						txtEmails.Text = CType(IIf(IsDBNull(a), "", a), String)
					End With

			End Select
		End If
	End Sub

	Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Update.Click
		Select Case _FormType
			Case "Census"
				With cmd
					.CommandType = CommandType.Text
					.CommandText = "Update as_AdminUsers set CensusEmails='" & Replace(txtEmails.Text, "'", "''") & "'"
					.Connection = conn
					If conn.State = ConnectionState.Closed Then conn.Open()
					.ExecuteNonQuery()
				End With

			Case "Alumni"
				With cmd
					.CommandType = CommandType.Text
					.CommandText = "Update as_AdminUsers set AlumniEmails='" & Replace(txtEmails.Text, "'", "''") & "'"
					.Connection = conn
					If conn.State = ConnectionState.Closed Then conn.Open()
					.ExecuteNonQuery()
				End With

			Case "Picnic"
				With cmd
					.CommandType = CommandType.Text
					.CommandText = "Update as_AdminUsers set PicnicEmails='" & Replace(txtEmails.Text, "'", "''") & "'"
					.Connection = conn
					If conn.State = ConnectionState.Closed Then conn.Open()
					.ExecuteNonQuery()
				End With
		End Select
	End Sub
End Class
