2012-10-06 21 views
0

我想创建一个在线商店网站。如何使用密钥来填充网格视图来自会话

我创建了一个datalist,显示我的产品在我的网站中,我把LinkBut​​ton放在我的数据手册中,用于获取用户选择的产品的productID并将其添加到购物车。

<asp:LinkButton ID="LinkButton1" runat="server" Text="Add To Cart" CommandName="addtocart" CommandArgument='<%# Eval("id")%>' > 

当用户点击LinkBut​​ton的此事件触发:

protected void theDataList_ItemCommand(object source, DataListCommandEventArgs e) 
{ 
//this add the selected product-id to the list<int> and put it in the 
// session["addtocart"] 
     if (e.CommandName == "addtocart") 
     {         
      int productid = Convert.ToInt32(e.CommandArgument); 
      Label6.Text = productid.ToString(); 


      List<int> productsincart = (List<int>)Session["addtocart"]; 
      if (productsincart == null) 
      { 
       productsincart = new List<int>(); 
      } 

      productsincart.Add(productid); 

      Session["addtocart"] = productsincart; 
} 

之后,当用户点击该按钮,它的文字是“秀购物车”, 用户将看到购物车。 aspx页面

Response.Redirect("shoppingcart.aspx"); 

在此页面中我有一个gridview,我想将其绑定到会话[“addtocart”]; 当页面加载gridview显示他们的ID在会话中的选定产品[“购物车”] 但它不起作用,并且发生此错误: System.InvalidCastException:对象必须实现IConvertible。

这是相关代码:

  <asp:GridView ID="GridView3" runat="server" 
      DataSourceID="SqlDataSource1" > 

      <columns> 
      <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" /> 
      <asp:BoundField DataField="name" HeaderText="post" SortExpression="post" /> 
      <asp:BoundField DataField="price" HeaderText="salary" 
       SortExpression="salary" /> 
      <asp:BoundField DataField="color" HeaderText="years" SortExpression="years" /> 

     </columns> 

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:employeeConnectionString4 %>" 
     SelectCommand="SELECT * FROM [products] WHERE ([productid] = @productid)"> 
     <SelectParameters> 
      <asp:SessionParameter DefaultValue="7" Name="cart" SessionField="cart" 
       Type="Int32" /> 
     </SelectParameters> 

我知道这个问题是关系到GridView3和会话[“addtocart”],我觉得GridView3不能从整数列表对象转换值存储在会话[“addtocart”]整数,在我看来,错误来自会话对象转换为整数列表,但我不知道如何解决这个问题,如果任何身体帮助我,我成为非常感谢。

Session [“cart”]中的对象是一个Integer列表,其中包含用户选择他们购买的productsid列表。 它给了我这个错误:

对象必须实现IConvertible。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。

异常详细信息:System.InvalidCastException:对象必须实现IConvertible。

源错误:

在当前web请求的执行过程中生成未处理的异常。关于异常的来源和位置的信息可以使用下面的异常堆栈跟踪来标识。

堆栈跟踪:

[InvalidCastException: Object must implement IConvertible.] 
System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +2880621 
System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +141 
System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63 
System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +301 
System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +264 
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +472 
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19 
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 
System.Web.UI.WebControls.GridView.DataBind() +4 
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82 
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72 
System.Web.UI.Control.EnsureChildControls() +87 
System.Web.UI.Control.PreRenderRecursiveInternal() +44 
System.Web.UI.Control.PreRenderRecursiveInternal() +171 
System.Web.UI.Control.PreRenderRecursiveInternal() +171 
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842 
+0

请告诉我们以下内容:1)购物车对象是什么,2)给您错误的代码(特定线路)。通过查看你的代码,看起来问题出在你的SqlDataSource中,而不是网格视图。 –

+0

Session [“cart”]中的对象是一个Integer列表,其中包含用户选择购买的productsid列表。 –

回答

0

的问题是在你的SQL参数。正如你试图做的那样,它不能将类型List<int>转换为Int32。它看起来像List对象无论如何都没有实现iConvertible,所以你需要一种不同的方法。

我通常不使用的SqlDataSource,你是干什么的,但这里是你可以建立你想要的查询语法正确的方式:

更新您的标记是:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:employeeConnectionString4 %>" /> 

然后在Page_Load中,构建连接字符串是这样的:

public void Page_Load(object sender, EventArgs e) 
{ 
    this.SqlDataSource1.SelectCommand = String.Format("SELECT * FROM [products] WHERE [productid] in ({0}))", GetInValues(Session["cart"] as List<int>)); 
} 
public string GetInValues(List<int> lst) 
{ 
    System.Text.StringBuilder sValues = new System.Text.StringBuilder(); 
    if (i == null) 
     sValues.Append(0); 
    else 
    { 
     foreach (int i in lst) 
     { 
      if (sValues.Length > 0) 
       sValues.Append(", "); 
      sValues.Append(i); 
     } 
    } 
    return sValues.ToString(); 
} 

就像我说的,有可能是一种方式的标记要做到这一点,但它不是我熟悉的。

你也可以创建一个继承List和实现iCovertible的类,但我想不出一种方法可以像SqlParameter一样工作。另外,如果我没有记错的话,iConvertible接口就像15个函数,所以对于这一个实例来说,它看起来很多工作。