注意:此代碼實際上是從我正在閱讀的教程書中編碼的,因此您可以想象這會讓我非常沮喪!我正在構建一個基本的購物車,使用VB.NET 4,Visual Studio 2010和MS SQL Server R2 2008.下面的代碼應該通過讀取會話變量來刪除購物車中的物品,編輯它以刪除相應的ProductID並將修改後的字符串返回到會話變量。然後它應該重新綁定數據到gridview(gvCart)來刷新數據......但是在這裏似乎存在錯誤。試圖創建購物車刪除按鈕
每次我在Visual Studio中構建網站時,它都會驗證正常。但每次我運行該網站,並嘗試使用刪除按鈕它給了我錯誤:
Incorrect syntax near ')'.
與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
哇。幹得好:) – Ryan 2012-03-20 17:24:18
謝謝兄弟,照顧購物車頁面!一件小事,在產品選擇頁面上,有一個標籤顯示購物車的當前內容。如果已經訪問過「購物車」頁面,則該標籤顯示爲'-1'作爲其內容的一部分。有沒有辦法不顯示它? – 2012-03-20 17:41:03
這是我建議的解決方法的副作用。變通辦法永遠不是最好的解決方案。實際的解決方案是在Session(「Cart」)中的列表爲空時正確重新組裝SQL查詢。 – 2012-03-20 17:43:42