Option Strict On
Option Explicit On 

Imports System.Data
Imports System.Data.SqlClient

Public MustInherit Class Counter
	Inherits System.Web.UI.UserControl
	Protected WithEvents lblCounter As System.Web.UI.WebControls.Label

#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
	Dim _PageID As Integer
	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
		If Not (Page.IsPostBack) Then
			Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))
			If conn.State = ConnectionState.Closed Then conn.Open()
			Dim cmd1 As New SqlCommand()
			With cmd1
				.Connection = conn
				.CommandType = CommandType.Text
				'the banners hit system is used here as counter
				'a special type of 'Counter' banner is used here
				'and each new session request is saved as a hit for this 'Counter' banner
				'so the id of 'Counter' banner is reterived first and then stored in db as hit
				'.CommandText = "Select ID from as_Banners where Func='Counter'"
				'Dim counter_id As Integer = CType(.ExecuteScalar.ToString.Trim, Integer)
				.CommandText = "Select Count(Banner_ID) from as_BannerHits where Page_ID=" & _PageID
				lblCounter.Text = .ExecuteScalar().ToString()
				'lblCounter.Text = CType(System.Math.Ceiling(CType(lblCounter.Text, Double) / 2), String)
				lblCounter.Text = "Hit Counts for this page: " & lblCounter.Text.PadLeft(8, Chr(48))
				.Dispose()
			End With
			conn.Dispose()
		End If
	End Sub

	Public Sub ReloadCounter()
		Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))
		If conn.State = ConnectionState.Closed Then conn.Open()
		Dim cmd1 As New SqlCommand()
		With cmd1
			.Connection = conn
			.CommandType = CommandType.Text
			'the banners hit system is used here as counter
			'a special type of 'Counter' banner is used here
			'and each new session request is saved as a hit for this 'Counter' banner
			'so the id of 'Counter' banner is reterived first and then stored in db as hit
			'.CommandText = "Select ID from as_Banners where Func='Counter'"
			'Dim counter_id As Integer = CType(.ExecuteScalar.ToString.Trim, Integer)
			.CommandText = "Select Count(Banner_ID) from as_BannerHits where Page_ID=" & _PageID
			lblCounter.Text = .ExecuteScalar().ToString()
			'lblCounter.Text = CType(System.Math.Ceiling(CType(lblCounter.Text, Double) / 2), String)
			lblCounter.Text = "Hit Counts for this page: " & lblCounter.Text.PadLeft(8, Chr(48))
			.Dispose()
		End With
		conn.Dispose()
	End Sub

End Class