Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:56:48+00:00 2026-05-31T17:56:48+00:00

Note: This code actually codes from a tutorial book I’m reading at the moment,

  • 0

Note: This code actually codes from a tutorial book I’m reading at the moment, so you can imagine this is frustrating me greatly! I’m building a basic shopping cart, using VB.NET 4, Visual Studio 2010 and MS SQL Server R2 2008. The code below is supposed to remove items from the cart, by reading a session variable, editing it to remove the appropriate ProductID and return the amending string to the session variable. It is then supposed to rebind the data to the gridview (gvCart) to refresh the data…but it seems here there is an error.

Every time I build the site in Visual Studio, it validates fine. But every time I run the site, and attempt to use the remove button it gives me the error:

Incorrect syntax near ‘)’.

with the IDE pointing me toward the final parenthesis.

I have been pulling my hair out at this for a good 4 hours now, any advice appreciated!

 Protected Sub gvCart_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvCart.SelectedIndexChanged
    'This method is hooked to the Remove button & is removing items from the cart
    Dim strProductId As String = gvCart.SelectedRow.Cells(1).Text
    If Session("Cart") IsNot Nothing Then
        'Remove selected ProductID from Session string and retrieve session string
        Dim strCart As String = Session("Cart").ToString()
        Dim arIDs As String() = strCart.Split(",")

        'Iterate through ID's in the 'Cart' array and rebuild the string leaving out the selected ID

        strCart = String.Empty
        For Each str As String In arIDs
            'use Trim to remove leading and trailing spaces
            If str.Trim() <> strProductId.Trim() Then
                strCart += str + ", "
            End If
        Next

        'Remove trailing space and comma
        If strCart.Length > 1 Then
            strCart = strCart.Trim()
            strCart = strCart.Substring(0, strCart.Length - 1)
        End If

        'Put back into session var
        Session("Cart") = strCart

        'Rebind gvCart, which forces the sqldatasource to requery
        gvCart.DataBind()
    End If
End Sub

[EDIT]
I am also including the code that runs for the event sqlCart_Selecting for completion sake:

 Protected Sub sqlCart_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles sqlCart.Selecting
    Trace.Warn("sqlCart_Selecting") 'aids debugging

    Dim strCart As String = String.Empty
    If Session("Cart") IsNot Nothing Then
        strCart = Session("Cart").ToString
        e.Command.CommandText &= " WHERE product.ProductID IN (" + strCart + ") AND culture.CultureID = 'en'"
    Else
        e.Cancel = True
    End If


End Sub

[EDIT]
Also including the SQL query used by the gridview ‘gvCart’ and the code for displaying the carts contents on another page

<asp:SqlDataSource ID="sqlCart" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" SelectCommand="SELECT product.ProductID, product.Name, product.ProductNumber, product.Color, subcat.Name AS SubcategoryName, cat.Name AS CategoryName, description.Description FROM Production.Product product JOIN Production.ProductSubcategory subcat ON product.ProductSubcategoryID = subcat.ProductSubcategoryID JOIN Production.ProductCategory cat ON subcat.ProductCategoryID = cat.ProductCategoryID JOIN Production.ProductModel model on product.ProductModelID = model.ProductModelID JOIN Production.ProductModelProductDescriptionCulture culture ON model.ProductModelID = culture.ProductModelID JOIN Production.ProductDescription description ON culture.ProductDescriptionID = description.ProductDescriptionID"></asp:SqlDataSource>


Protected Sub btnAddToCart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddToCart.Click
    'The contents of the cart will be saved in a Session object as a string of coma-delimited values of ProductID's
    Dim strCart As String = String.Empty
    Dim strProductID As String = gvProducts.SelectedDataKey.Value.ToString()

    If Session("Cart") Is Nothing Then
        strCart = strProductID
    Else
        strCart = Session("Cart").ToString() + ", " + strProductID
    End If

    Session("Cart") = strCart
    lblCart.Text = strCart
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:56:49+00:00Added an answer on May 31, 2026 at 5:56 pm

    I will take a shot here, since you are not showing us everything (the SQL query for instance).

    But it feels like the SQL query is using IN clause. And it appears to be failing when the cart goes empty.

    Since Session("Cart") contains the comma separated values of the product IDs, an empty list would make the following SQL query fail:

    select id, name 
    from products 
    where id in ()
    

    with the message:

    Incorrect syntax near ‘)’.

    You have to show us the portion of code that’s reloading the query from Session("Cart"). The query should be reassembled.

    But there’s one trick you can use! If your product ids are numeric and always greater than zero, change this line:

    strCart = String.Empty
    

    to this:

    strCart = '-1, '
    

    Update

    I will really teach how to fish on this one. 🙂 the problem is here:

    Dim strCart As String = String.Empty
    If Session("Cart") IsNot Nothing Then
        strCart = Session("Cart").ToString
        e.Command.CommandText &= " WHERE product.ProductID IN (" + strCart + ") AND culture.CultureID = 'en'"
    Else
        e.Cancel = True
    End If
    

    When cart is empty, Session("Cart") is not Nothing but it’s an empty string (provided that you removed the -1 workaround). If Session("Cart") is an empty string the where clause should be " WHERE culture.CultureID = 'en'" only, no mention to product ids. God speed!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself
this code is correct?? String note = text.txt; FileWriter file = new FileWriter(note); Scanner
Note: This question has broadened in scope from previous revisions. I have tried to
I was looking at some code from a tutorial for creating a carousel menu
I'm porting some code from lisp, but I got stuck at this part (apparently
Note: this was inspired by WebBrowser Event Properties? Why am I able to access
Note this question was originally posted in 2009, before C++11 was ratified and before
Note: This issue appears to be limited to SQL Server 2005 SP2 I have
Note: this is not real information: $ ssh-keygen -t rsa -C "tekkub@gmail.com" Generating public/private
Note: This problem occurs only on Mac, on Windows works fine. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.