2012-03-20 48 views
0

注意:此代碼實際上是從我正在閱讀的教程書中編碼的,因此您可以想象這會讓我非常沮喪!我正在構建一個基本的購物車,使用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 

回答

3

我會在這裏進行拍攝,該代碼,因爲你沒有向我們展示everythi ng(例如SQL查詢)。

但感覺就像SQL查詢使用的是IN子句。當購物車變空時,它似乎失敗了。

由於Session("Cart")包含產品ID的逗號分隔值,空單將使下面的SQL查詢失敗:

select id, name 
from products 
where id in() 

與消息:

Incorrect syntax near ')'.

你必須告訴我們從Session("Cart")重新加載查詢的代碼部分。該查詢應該重新組合。

但有一個技巧,你可以使用!如果你的產品id s爲數字始終大於零,改變這一行:

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。上帝的速度!

+1

哇。幹得好:) – Ryan 2012-03-20 17:24:18

+0

謝謝兄弟,照顧購物車頁面!一件小事,在產品選擇頁面上,有一個標籤顯示購物車的當前內容。如果已經訪問過「購物車」頁面,則該標籤顯示爲'-1'作爲其內容的一部分。有沒有辦法不顯示它? – 2012-03-20 17:41:03

+0

這是我建議的解決方法的副作用。變通辦法永遠不是最好的解決方案。實際的解決方案是在Session(「Cart」)中的列表爲空時正確重新組裝SQL查詢。 – 2012-03-20 17:43:42

相關問題