Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Counting Views for an ASP.net Page
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Counting Views for an ASP.net Page

 Post Reply Post Reply Page  123>
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: Counting Views for an ASP.net Page
    Posted: 29 October 2004 at 1:11am

I already know how you count the views for certain records in ASP. The SQL Statement should be something like:      
'Update the Views
SQL= "UPDATE Spastore SET Views = Views + 1 " & _
 "WHERE SpaStoreID = '" & strChosenStoreID & "'"

However, I am not sure how to do this in ASP.Net. I am trying to figure out where I should put this under my code for the DataList. Or should I create a separate sub procedure? Does anyone know the best way for me to accomplish this?

Here's some of my code:

'---------------------------------------------

' name: BindDataList()

'---------------------------------------------

Sub BindDataList()

 

              Dim strConnect As String

              Dim objConnect As New System.Data.SqlClient.SQLConnection(ConnectionString())

              Dim objCommand As New System.Data.SqlClient.SQLCommand

              Dim strSQL as String

              Dim SQL as String

              Dim dtaSpaStore As New System.Data.SqlClient.SQLDataAdapter()

              Dim dtsSpaStore As New DataSet()

              Dim strChosenStoreID

       

    

              objConnect.Open()

             

      

                   'Get incoming querystring values

                  strChosenStoreID = request.params("ID")

    

              'Start SQL statement

             strSQL = "Select * From SpaStore, City, State, Area"

                    strSQL = strSQL & " where SpaStore.City = City.City"

                    strSQL = strSQL & " and SpaStore.State = City.State"

                    strSQL = strSQL & " and SpaStore.State = State.State"

                    strSQL = strSQL & " and SpaStore.AreaID = Area.AreaID"

                    strSQL = strSQL & " and SpaStoreID = '" & strChosenStoreID & "'"

                    strSQL = strSQL & " and Approve=1"

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 29 October 2004 at 4:00am

This should work:


Dim SqlConn As New SqlConnection([connectionstring])
Dim SqlCmd As New SqlCommand

SqlCmd.CommandText = String.Format("UPDATE Spastore SET Views = Views + 1 WHERE SpaStoreID = '{0}'", strChosenStoreID) 

SqlCmd.Connection = SqlConn

SqlConn.Open()
SqlCmd.ExecuteNonQuery()
SqlConn.Close()

btw shouldnt SpaStoreID be an integer?



Edited by Mart
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 29 October 2004 at 1:43pm

The code that Mart gave me worked fine. strChosenStoreID is an integer. I should have not called it strChosenStoreID because str stands for string. I never dimmed it as a string. However, there's a strange problem. It will add 2 to the views instead of 1. I don't understand why this is happening. Views is an int datatype with default value of 0 in my table. How do I get the sql statement to only add one to the views like I indicated in the sql statement?

 

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 29 October 2004 at 1:49pm
Put the statement within
If Not Page.IsPostback
..
End IF
Maybe you have some postback event that would increase the views every time, you should have the whole binding within that actually.
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 29 October 2004 at 2:05pm

I have this code:

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              If Not Page.IsPostBack THEN
      BindDataList()
                 
              END IF
    End Sub 

The code for counting views is under BindDataList(). Should I make a separate sub procedure for counting views?

 



Edited by Misty
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 29 October 2004 at 2:11pm
You don't have to, just stick it in the BindDataList sub
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 29 October 2004 at 2:31pm

I've tried experimenting with my code, but I have been unsuccessful in getting the sql statement to only add 1 to the views. I decided to experiment with subtracting 1 from the views. It subtracted 2 from the views. This is puzzling.

Let me show you some of my code that may be significiant:

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                          If Not Page.IsPostBack THEN
      BindDataList()
     end if         &nbs p;  
          
    End Sub 

'---------------------------------------------

' name: BindDataList()

'---------------------------------------------

Sub BindDataList()

 

 

              Dim strConnect As String

              Dim objConnect As New System.Data.SqlClient.SQLConnection(ConnectionString())

              Dim objCommand As New System.Data.SqlClient.SQLCommand

              Dim strSQL as String

              Dim SQL as String

              Dim dtaSpaStore As New System.Data.SqlClient.SQLDataAdapter()

              Dim dtsSpaStore As New DataSet()

              Dim ChosenStoreID

                    Dim City as String

                    Dim AreaID

                    Dim CityID

                    Dim AreaName as String

                    Dim StateName as String

                    Dim State as String

                    Dim SpaStoreName as String

                    

              'Get incoming querystring values

              ChosenStoreID = request.params("ID")

             

             If Not Page.IsPostBack THEN

    

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 29 October 2004 at 3:11pm

Ok, time for some diagnostics

firstly import the System.Diagnostics namespace then do the following

  1. Straight after Private Sub Page_Load type 'Debug.Writeline("PageLoad")'
  2. Inside the If Not Page.IsPostBack in Page_Load type 'Debug.Writeline("PageLoadPostback")'
  3. Straight after Sub BindDataList type 'Debug.Writeline("BindDataList")'

then just a couple of things I noticed

  1. You don't need a If not page.IsPostBack in the BindDataList sub as that sub will only fire if it isn't a postback due to the way you call it in Page_Load
  2. You need to change:
    ChosenStoreID = request.params("ID")

    to:
    ChosenStoreID = Cint(request.params("ID"))

    for security reasons because you are not declaring ChosenStoreID as an integer people could inject code through that query string variable, for more info do a google for 'SQL injections' (you should also declare ChosenStoreId, AreaId and CityId as an integer

Almost forgot:
run the altered script via. Debug start in VS.NET and paste the text in the 'Output' frame into here, we can then work out why its being called twice



Edited by Mart
Back to Top
 Post Reply Post Reply Page  123>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.