2013-03-17 46 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