Solved

Button click doesn't update number

Posted on 2009-05-05
2
293 Views
Last Modified: 2013-11-27
Hi guys,

I have the following code for my item.aspx page. User clicks on the Add to Shopping Cart button. Record gets inserted in shopping cart table. The shopping cart link is supposed to change to Shopping Cart (Number_of_items_in_cart_here). But it doesn't change unless I go to another page after clicking the button or refresh page. How do I make that change after the first click? Inserting in the table works fine. I added master page code that actually checks for number of items in shopping cart table. Basically, itemsInCart var doesn't get updated after the button was clicked. It updates only after I navigate to some other page or refresh item.aspx. I guess there is a problem with my master page. Please help.

Thanks.

item.aspx:
 
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\titanmusic.accdb;Persist Security Info=True"
        Dim objConnection As New System.Data.OleDb.OleDbConnection(strConn)
        Dim strSQL As String = "INSERT INTO Cart (SessionID, ProductFK1) VALUES ('" & Session.SessionID & _
                                     "', " & DetailsView1.DataKey.Value & ")"
        Dim objCommand As New System.Data.OleDb.OleDbCommand(strSQL, objConnection)
 
        Try
            objConnection.Open()
            Try
                objCommand.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(Err.Number & " " & Err.Description)
            Finally
                objConnection.Close()
            End Try
        Catch ex As Exception
            MsgBox(Err.Number & " " & Err.Description)
        End Try
 
    End Sub
 
Master Page:
 
Public itemsInCart As Integer
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("SCart") = "CartSession"
        Dim ctl As Control
        Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\titanmusic.accdb;Persist Security Info=True"
        Dim objConnection As New System.Data.OleDb.OleDbConnection(strConn)
        Dim strSQL As String = "SELECT COUNT(*) FROM Cart WHERE SessionID='" & Session.SessionID & "'"
        Dim objCommand As New System.Data.OleDb.OleDbCommand(strSQL, objConnection)
 
        Try
            objConnection.Open()
 
            itemsInCart = objCommand.ExecuteScalar
 
            objConnection.Close()
        Catch ex As Exception
            MsgBox("Connection failed to open. Error: " & ex.ToString())
        End Try
 
    End Sub
 
                                    <% 
                                        If itemsInCart = 0 Then
                                            Response.Write("Shopping Cart")
                                        Else
                                            Response.Write("Shopping Cart (" & itemsInCart & ")")
                                        End If
                                    %>

Open in new window

0
Comment
Question by:RealSnaD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
JPJ78 earned 500 total points
ID: 24302810
The click event is handled after Page_Load, therefore has the new item not been inserted when you count the items in your masterpage.
Move the counting to PreRender.



    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Session("SCart") = "CartSession"
        Dim ctl As Control
        Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\titanmusic.accdb;Persist Security Info=True"
        Dim objConnection As New System.Data.OleDb.OleDbConnection(strConn)
        Dim strSQL As String = "SELECT COUNT(*) FROM Cart WHERE SessionID='" & Session.SessionID & "'"
        Dim objCommand As New System.Data.OleDb.OleDbCommand(strSQL, objConnection)
 
        Try
            objConnection.Open()
 
            itemsInCart = objCommand.ExecuteScalar
 
            objConnection.Close()
        Catch ex As Exception
            MsgBox("Connection failed to open. Error: " & ex.ToString())
        End Try
 
    End Sub

Open in new window

0
 
LVL 7

Author Comment

by:RealSnaD
ID: 24304142
That worked beautifully, thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reg Exp to extract Url from string asp 12 39
Sending ASP to server side 8 32
ASP sending two datas 2 23
SP inserts data with order number, then push the rest one order up 11 19
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question