2012-10-06 25 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個函數,所以對於這一個實例來說,它看起來很多工作。