Print Page | Close Window

Counting Views for an ASP.net Page

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=12350
Printed Date: 29 March 2026 at 7:41am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Counting Views for an ASP.net Page
Posted By: Misty
Subject: Counting Views for an ASP.net Page
Date 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"




Replies:
Posted By: Mart
Date 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?



Posted By: Misty
Date 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?

 



Posted By: michael
Date 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.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Misty
Date 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?

 



Posted By: Mart
Date Posted: 29 October 2004 at 2:11pm
You don't have to, just stick it in the BindDataList sub


Posted By: Misty
Date 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

    



Posted By: Mart
Date 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



Posted By: Misty
Date Posted: 29 October 2004 at 3:57pm

I added <%@ Import Namespace="System.Diagnostics" %> to the top of my web page. I also added the three lines of debugging code. But I am not completely sure about how to do what you said: Almost forgot:
run the altered script via. Debug start in VS.NET and paste the text in the 'Output' frame into here,

I have never created a VS.net page. I still have a lot to learn about ASP.net.

 



Posted By: Mart
Date Posted: 29 October 2004 at 4:43pm

The screen shot below shows how you open in debug mode:

http://www.devjunkies.com/examples/debug.jpg - http://www.devjunkies.com/examples/debug.jpg



Posted By: Misty
Date Posted: 29 October 2004 at 11:46pm

Mart, I couldn't see it clearly. It looked something like:



Posted By: Mart
Date Posted: 30 October 2004 at 5:29am
Probably because your using IE, hover your mouse around the bottom right corner then press the expand icon that pops up


Posted By: Misty
Date Posted: 30 October 2004 at 2:23pm

I was able to see the image clearly after clicking on the icon to expand the picture. I hate to say this, but I don't have Visual Studio .Net installed on my computer. I just never purchased it. I use Dreamweaver or Microsoft Interdev 6.0 to edit my ASP.Net code. I jusy wish that I could figure out why it is adding 2 to the views instead of 1.



Posted By: Mart
Date Posted: 30 October 2004 at 3:01pm
Oh, ok, just change debug.writeline to response.write and paste in the first few lines of view source


Posted By: Misty
Date Posted: 30 October 2004 at 3:08pm

I change it to response.write. Here are the results that I got:

PageLoadPageLoadPostbackBindDataListPageLoadPageLoadPostback BindDataList

Here's what I have in my code now:

Response.write("PageLoad")

Response.write("PageLoadPostback")

Response.write("BindDataList")

Is this what I was supposed to get for the results?



Posted By: Misty
Date Posted: 01 November 2004 at 2:00am

I don't think that it was supposed to show the results above. I just wish I knew why it was adding 2 to the views instead of 1. Did I use Response.write the correct way?



Posted By: Mart
Date Posted: 01 November 2004 at 3:59am
You have placed the Response.Write's correctly and from the look of it Page_Load is being called twice and they are both not postbacks, I would rewrite the page, that usually solves things.


Posted By: Misty
Date Posted: 01 November 2004 at 2:14pm

Mart,

I found out exactly what was wrong. I wish that I had known about this before. Thank you for the information about response.write because it certainly helped me.

The line of code:  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load was what caused the sql statement to execute twice. I changed the code to Private Sub Page_Load(). Do you know why the code associated with Sub Page_Load caused the sql statement to be executed twice? I am just curious about why this happened.
                           



Posted By: Mart
Date Posted: 01 November 2004 at 3:37pm

Oh sorry, I'm stupid, you just needed to tweak the page directive to

<%@ Page language="vb" AutoWireupEvent="false" %>

the problem is that because you are using inline code asp.net will automatically fire Sub Page_Load (which caused the first execution) then asp.net will fire the MyBase.Load event handler (which caused the second execution because you had Handles MyBase.Load). Setting AutoWireupEvent will tell asp.net not to fire MyBase.Load



Posted By: Misty
Date Posted: 01 November 2004 at 3:45pm
I got the following error message: The 'AutoWireupEvent' attribute is not supported by the 'page' directive. when I added the above page directive. I thought I'd let you know about this. It worked fine when I just had Sub Page_Load().


Posted By: Mart
Date Posted: 01 November 2004 at 3:52pm
meant to be AutoEventWireup="false"... I am half asleep tonight


Posted By: Misty
Date Posted: 01 November 2004 at 3:56pm

Thanks! It worked.




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net