注:このコードは実際に私が読んでいるチュートリアルブックから実際にコード化されていますので、これは私が非常にイライラしていると想像することができます! VB.NET 4、Visual Studio 2010、およびMS SQL Server R2 2008を使用して、基本的なショッピングカートを構築しています。以下のコードは、セッション変数を読み込み、適切なProductIDを削除することによってカートからアイテムを削除することになっています修正する文字列をセッション変数に戻します。その後、データをグリッドビュー(gvCart)に再バインドしてデータをリフレッシュすることになっていますが、ここにはエラーがあるようです。ショッピングカートを作成する削除ボタン
私はVisual Studioでサイトを構築するたびに、妥当性を検証します。最後の括弧に向かって私を指しているIDEで
Incorrect syntax near ')'.
:しかし、私は、サイトを実行し、すべての時間は、それは私にエラーを与えて削除ボタンを使用しようとします。
私はこれで良い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
[EDIT]私も完了ため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クエリを含む[EDIT]あなたは私たちに表示されていないので、別のページ
<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
おかげで、カートのページの世話をする男! 1つの小さなこと、しかし、製品の選択ページには、カートの現在の内容を示すラベルがあります。 「カート」ページにアクセスした場合、その内容の一部としてラベルに「-1」と表示されます。それを表示しない方法はありますか? –
これは私が提案した回避策の副作用です。回避策は決して最良の解決策ではありません。実際のソリューションは、セッション( "カート")のリストが空の場合にSQLクエリを正しく再アセンブリします。 –