0

Devart LinqConnect(Linq-to-Mysql) を使用して、asp.net C# と Mysql でショッピング カート Web アプリケーションを作成しようとしています。

カテゴリを選択すると、対応する製品が ShowProducts.aspx ページに表示されます。クエリ文字列値からデータをフェッチするデータリストを追加しました。

私はメンズの下のカテゴリシャツなど、各カテゴリに割り当てましたハイパーリンクにIDを与えました

<li><a href="ShowProducts.aspx?id=8">Mens</a><span class="rarrow">&#9654;</span>
                        <ul class="sub2">
                            <li><a href="ShowProducts.aspx?id=9">Shirts</a></li>

それが他のページに渡されると、 ShowProducts.aspx が S として、DatalisthowProducts.aspX?id=9に製品リストが表示されます。ショッピング カート アプリケーションであるため、セッション カート オブジェクトに追加する代わりにボタンをクリックすると、[カートに追加] ボタンが表示されます。エラーが表示されます。

インデックスが配列の範囲外だった。の上int ProductID = Convert.ToInt32(DataListProducts.DataKeyField[RowClicked]);

これは Datalist Item コマンドの私のコードです。

  protected void DataListProducts_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "AddToCart")
        {
            int RowClicked = Convert.ToInt32(e.CommandArgument);
            int ProductID = Convert.ToInt32(DataListProducts.DataKeyField[RowClicked]);
            List<int> ProductsInCart = (List<int>)Session["Cart"];

            if (ProductsInCart == null)
            {

                ProductsInCart = new List<int>();
            }

            ProductsInCart.Add(ProductID);
            Session["Cart"] = ProductsInCart;
        }
    }

これは、ShowProducts.aspx 内の私の DataBindings です。

 <asp:LinqDataSource ID="LinqDataSourceProducts" runat="server" ContextTypeName="ShoppingContext.ShoppingDataContext"
    EntityTypeName="" Select="new (ProductName, ProductUnitPrice, ProductBrand, Category, ParentID, ProductImage, ProductID)"
    TableName="Products" Where="ParentID == @ParentID1">
    <WhereParameters>
        <asp:QueryStringParameter DefaultValue="0" Name="ParentID1" QueryStringField="id"
            Type="Int64" />
    </WhereParameters>
</asp:LinqDataSource>
<asp:DataList ID="DataListProducts" runat="server" DataSourceID="LinqDataSourceProducts" 
    RepeatDirection="Horizontal" RepeatColumns="4" 
Style="margin-left: 87px" CellSpacing="50"
    CellPadding="50" onitemcommand="DataListProducts_ItemCommand">
    <ItemTemplate>
        <table style="border:5px dotted #c0c0c0">
            <tr>
                <td>
                    <asp:ImageButton ID="ImageButton1" ImageUrl='<%# GetImage(Eval("ProductImage")) %>'
                        runat="server" Width="250px" Height="270px" />
                    <br />
                    <center>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
                        <asp:Label ID="LabelProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label><hr />
                    </center>
                    By : &nbsp;<b><asp:Label ID="Label2" runat="server" Text='<%# Eval("ProductBrand") %>'></asp:Label></b><br />
                    <asp:Button ID="ButtonAddToCart" runat="server" Text="Add To Cart" CommandName="AddToCart" CommandArgument='<%# Eval("ProductID") %>'
                        Style="padding-top: 3px; left: 5px" />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

誰でもこれに答えてください...助けてください..

4

1 に答える 1

2

Convert.ToInt32 に渡される値の 1 つが数値ではないため、失敗します

e.CommandArgument.ToString()
or
DataListProducts.DataKeyField[RowClicked]
于 2013-03-17T19:25:54.210 に答える