Hey Slade, it is actually rather easy, and I would recommend an Access DB to handle something like this, but it is up to you.
Create a table in Access called tblHits with 2 columns; ID (Primary Key),
and one called Hit_Count. I have modified your code to show what I would do;
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
If Not Page.IsPostBack Then
Dim nCount As Int32 = 108 ' ||||| Why did you initialize this to 108? Doesn't matter.
nCount = GetCounterValue()
lblTest.Text = nCount.ToString()
End If
End Sub
Private Function GetCounterValue() As Int32
Dim objCmd As OleDbCommand
Dim objReader As OleDbDataReader
Dim strSQL As String
Dim intNewCount As Int32
Dim MyConn As OleDbConnection = New OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Northwind.mdb;User ID=Admin;Password=;")
strSQL = "SELECT Hit_Count FROM tblHits"
objCmd = New OleDbCommand(strSQL, MyConn)
If MyConn.State = ConnectionState.Closed Then
MyConn.Open()
End If
objReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
With objReader
intNewCount = CInt(.Item("Hit_Count")) + 1
End With
objReader.Close()
' ||||| Now Call the Update Hit Count
Call Update_HitCount(intNewCount)
' ||||| Return the updated count
Return intNewCount
End Function
Private Sub Update_HitCount(ByVal intValue As Int32)
' ||||| Could have made this a class visible variable
Dim MyConn As OleDbConnection = New OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Northwind.mdb;User ID=Admin;Password=;")
Dim strSQL As String
Dim objCmd As OleDbCommand
Dim intRecords As Integer
strSQL = "UPDATE tblHits "
strSQL &= "SET Hit_Count = " & CStr(intValue) ' ||||| Maybe you have it as an Integer in the DB
strSQL *= " WHERE ID = 1" ' ||||| The Primary Key to the Only row in table
objCmd = New OleDbCommand(strSQL, MyConn)
intRecords = objCmd.ExecuteNonQuery() ' ||||| Returns the number of rows affected
MyConn.Close()
End Sub
Probably not the most elegant way, but you get the point I am sure. :mrgreen:
Hope this helps dude!
:cool:
Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27