Hi
I am just starting to learn programming so I apolgise if I come across a bit thick. I am trying to build a very basic hit counter for a web page. I have built a page that retrieves a value from a column called 'hits' from a table called 'tbl_hits' which is in a Access 2k DB. And as the page load I want the value to be incremented and then write this back to the DB. However I am having trouble with the update section.
My code is as follows:
Imports System.Data.OleDb
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Create Connection
Dim strConn As String ="Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\inetpub\ Dim cnCounter As New OleDbConnection(strConn)
cnCounter.Open()
'Create Command
Dim strSQL As String = "SELECT hits FROM tbl_hits"
Dim cmHits As New OleDbCommand(strSQL, cnCounter)
'Show Current Value
Dim hits As Integer = CType(cmHits.ExecuteScalar(), Integer)
Response.Write("Original Value = " & hits.ToString)
'Increment Value
Dim incHits As Integer = hits + 1
Response.Write("New Value = " & incHits.ToString)
'Update the database with the new value
cmHits.Connection = cnCounter
cmHits.CommandText = "UPDATE 'tbl_hits' SET 'hits' =" & incHits & """
cmHits.ExecuteNonQuery()
'Close DB Connection
cnCounter.Close()
cnCounter.Dispose()
Any help would be greatly appreciated, thanks in advance
I am just starting to learn programming so I apolgise if I come across a bit thick. I am trying to build a very basic hit counter for a web page. I have built a page that retrieves a value from a column called 'hits' from a table called 'tbl_hits' which is in a Access 2k DB. And as the page load I want the value to be incremented and then write this back to the DB. However I am having trouble with the update section.
My code is as follows:
Imports System.Data.OleDb
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Create Connection
Dim strConn As String ="Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\inetpub\ Dim cnCounter As New OleDbConnection(strConn)
cnCounter.Open()
'Create Command
Dim strSQL As String = "SELECT hits FROM tbl_hits"
Dim cmHits As New OleDbCommand(strSQL, cnCounter)
'Show Current Value
Dim hits As Integer = CType(cmHits.ExecuteScalar(), Integer)
Response.Write("Original Value = " & hits.ToString)
'Increment Value
Dim incHits As Integer = hits + 1
Response.Write("New Value = " & incHits.ToString)
'Update the database with the new value
cmHits.Connection = cnCounter
cmHits.CommandText = "UPDATE 'tbl_hits' SET 'hits' =" & incHits & """
cmHits.ExecuteNonQuery()
'Close DB Connection
cnCounter.Close()
cnCounter.Dispose()
Any help would be greatly appreciated, thanks in advance