注意:这段代码实际上是从我正在阅读的教程书中编码的,所以你可以想象这对我来说非常令人沮丧!我正在构建一个基本的购物车,使用VB.NET 4,Visual Studio 2010和MS SQL Server R2 2008.下面的代码应该通过读取会话变量来删除购物车中的项目,编辑它以删除相应的ProductID并将修正字符串返回到会话变量。然后应该将数据重新绑定到gridview(gvCart)以刷新数据......但似乎这里出现了错误。
每次我在Visual Studio中构建网站时,它都会很好地验证。但每次我运行该网站,并尝试使用删除按钮,它给我错误:
')'附近的语法不正确。
IDE指向我最后的括号。
我现在已经把头发拉了4个小时,任何建议都值得赞赏!
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
[编辑] 我还包括为事件sqlCart_Selecting运行的代码,以便完成:
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
[编辑] 还包括gridview'gvCart'使用的SQL查询和在另一页上显示购物车内容的代码
<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
答案 0 :(得分:3)
我会在这里拍摄,因为你没有向我们展示一切(例如SQL查询)。
但感觉SQL查询正在使用IN
子句。当推车变空时,它似乎失败了。
由于Session("Cart")
包含产品ID的逗号分隔值,因此空列表会使以下SQL查询失败:
select id, name
from products
where id in ()
带有消息:
')'附近的语法不正确。
您必须向我们展示从Session("Cart")
重新加载查询的代码部分。应该重新组装查询。
但是你可以使用一个技巧!如果您的商品id
是数字且始终大于零,请更改此行:
strCart = String.Empty
到此:
strCart = '-1, '
<强>更新强>
我真的会教这个如何捕鱼。 :)问题出在这里:
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
当购物车为空时,Session("Cart")
不是Nothing
,但它是一个空字符串(前提是您删除了-1解决方法)。如果Session("Cart")
是空字符串,则where子句应仅为" WHERE culture.CultureID = 'en'"
,不提及产品ID。上帝的速度!