Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: My First Update Page

Status
Not open for further replies.

IndyGill

Technical User
Jan 15, 2001
191
0
0
GB
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
 
Your quotes are misplaced on your update command. It should look like this:

cmHits.CommandText = "UPDATE tbl_hits SET hits = '" & incHits & "'"

Note that the only single quotes that are used are around the value that is being inserted (in this case, incHits).

As a side note, if hits is set as a numerical field in your table, then there is no need for the single quotes around the value.

HTH! Kevin B.
.Net Programmer [thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top