2013-03-17 77 views
0

我想使用Devart LinqConnect(Linq-to-Mysql)在asp.net C#和Mysql中製作購物車Web應用程序。輸入字符串格式不正確查詢字符串datalist購物車

當選擇一個類別在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 howProducts.aspX?id=9,它在Datalist中顯示產品列表。因爲它是購物車應用程序我有一個添加到購物車按鈕,當我點擊按鈕,而不是添加在會話車對象它給了我一個錯誤:

Index was outside the bounds of the array. on int ProductID = Convert.ToInt32(DataListProducts.DataKeyField[RowClicked]);

這是我在Datalist中項目的命令代碼。

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數據綁定:

<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> 

請任何人有一個答案...... 請幫助..

回答

2

通過一個轉換的值.ToInt32不是數字使它失敗

e.CommandArgument.ToString() 
or 
DataListProducts.DataKeyField[RowClicked] 
+0

請你能再次檢查我的問題我已編輯它我在下一行出現錯誤... – 2013-03-18 15:11:32