I am currently attempting to integrate a Hit Counter to the site that has the following ascx file. I am unable to get a DB connection no matter how I modify the objConnection portion. The site is using the standard "Personal" connection on the development PC and the modified connection string for Go Daddy, both working well without the Hit Counter portion.
Public Display As String ' {TEXT|images|none}
Sub Page_Load(Source As Object, E As EventArgs)
Dim strFileName As String
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim strSQLQuery As String
Dim intCount As Integer
' Get name of the file using the counter to use as db key.
' --------------------------------------------------------
strFileName = Request.ServerVariables("SCRIPT_NAME")
' Set up our database connection.
' -------------------------------
objConnection = New SqlConnection("Data Source=10.2.1.214;" _
& "Initial Catalog=samples;User Id=samples;Password=password;" _
& "Connect Timeout=15;Network Library=dbmssocn;")
' Get current hit count for the page using the counter.
' -----------------------------------------------------
' Build SQL query that retrieves the current count.
strSQLQuery = "SELECT hit_count FROM hit_count WHERE page_name='" & strFileName & "';"
' Create new command object passing it our SQL query
' and telling it which connection to use.
objCommand = New SqlCommand(strSQLQuery, objConnection)
' Open conn. Execute query. Leave conn open for update.
objConnection.Open()
intCount = objCommand.ExecuteScalar()
' Increment count.
' ----------------
intCount = intCount + 1
' Write new count back to the database.
' -------------------------------------
' If intCount = 1 then we retrieved a zero which should mean
' that there's no record in the DB for this filename so we
' need to insert a new record. Build SQL accordingly.
' If you ever need to reset a counter... start it at 1!
If intCount = 1 Then
strSQLQuery = "INSERT INTO hit_count " _
& "(page_name, hit_count) " _
& "VALUES ('" & strFileName & "', 1)"
Else
strSQLQuery = "UPDATE hit_count " _
& "SET hit_count=" & intCount & " " _
& "WHERE page_name='" & strFileName & "';"
End If
' Set up our command.
objCommand = New SqlCommand(strSQLQuery, objConnection)
' Execute the command and close the connection.
objCommand.ExecuteNonQuery()
objConnection.Close()
' Display count.
' --------------
Select Case LCase(Display)
Case "none"
Case "images"
litCounter.Text = GetCountImage(intCount)
Case Else '"text"
litCounter.Text = intCount
End Select
End Sub
Function GetCountImage(strCount As String) As String
Dim I As Integer
Dim strTemp As String
strTemp = ""
' Build a string that will display our image files.
' You can easily replace the files modify the string
' or do whatever else you want.
For I = 1 to Len(strCount)
strTemp = strTemp & "<img src=""images/digit_"
strTemp = strTemp & Mid(strCount, I, 1)
strTemp = strTemp & ".gif"" alt="""
strTemp = strTemp & Mid(strCount, I, 1)
strTemp = strTemp & """ width=""20"" height=""27"" />"
Next I
GetCountImage = strTemp
End Function
</script>
<asp:Literal id="litCounter" runat="server" />
I am having difficulty getting this changed to work with the "Personal" DB.
I need this to work via the web.config file so it works locally on the development PC and also when hosted on Go Daddy. I have the site minus the Hit Counter working on both at this time.
Anyone have any ideas?
Robert H. Hanson