無法加入購物車 |
|
咖啡牛奶(真的很急)
更新時間:2008/10/5 上午 12:45:26 |
|
老師您好: 我照著挑戰ASP.NET For VB這本書做了一個購物車的子系統,但我有做了一點小修改,就是當使用者在sleep.aspx這一頁中,依照條件選完飯店之後,飯店的資訊會出現在下面的GridView裡,之後使用者就可以點加入購物車將飯店加入購物車裡面,但是每當我按下加入購物車的時候,都會出現[變數名稱 '@cartCustomer' 已經宣告。變數名稱在一個查詢批次或預存程序內必須是唯一的。]的訊息,錯誤的程式碼是在sleep.aspx.vb裡面,其中程式碼如下 ------------------------------------------------------------ Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Session("cartName") = Session.SessionID End Sub ------------------------------------------------------------ Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Dim ads As New SqlDataSource Dim dv As Data.DataView ads.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Travel.mdf;Integrated Security=True;User Instance=True" dv = ads.Select(New DataSourceSelectArguments) Dim i As Integer For i = 0 To GridView1.Rows.Count - 1 ads.InsertCommand = "INSERT INTO [sleepcart] ([cartCustomer], [SleepCounty], [SleepName], [SleepMoney]) VALUES (@cartCustomer, @SleepCounty, @SleepName, @SleepMoney)" ads.InsertParameters.Add("cartCustomer", Session("cartName")) ads.InsertParameters.Add("SleepCounty", GridView1.Rows(i).Cells(0).Text.ToString) ads.InsertParameters.Add("SleepName", GridView1.Rows(i).Cells(1).Text.ToString) ads.InsertParameters.Add("SleepMoney", GridView1.Rows(i).Cells(3).Text.ToString) ads.Insert() Next Response.Redirect("~/cartBasket.aspx") End Sub ------------------------------------------------------------ 上面的程式碼是將sleep.aspx中GridView裡面的飯店資料加到sleepcart這張資料表中,但每次都有問題,我也找不出原因在哪,希望老師能幫幫我,真的很急,拜託您了(檔案也已經寄到老師的email了) |
|
|
|
文淵閣工作室
更新時間:2008/10/7 上午 09:02:47 |
|
SQL SERVER 中處理資料的參數也是一種變數,您將其置於 FOR NEXT 迴圈中,所以產生變數重複宣告的錯誤。請在 sleep.aspx.vb 第 19 列(ads.Insert()之後)加入 ads.InsertParameters.Clear() 以清除變數即可。 |
|
|
|
咖啡牛奶(真的很急)
更新時間:2008/10/8 上午 12:21:03 |
|
老師,先謝謝您幫我解決了,但是又出現新問題,加了那一行後,加進去是沒問題了,但變成我按加入飯店之後,我沒選的也全部加進去了,這該怎麼解決呢? |
|
|
|
文淵閣工作室
更新時間:2008/10/13 上午 09:31:33 |
|
1. 將 sleep.aspx 的 <asp:TemplateField> 改為: <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:ImageButton ID="LinkButton1" runat="server" ImageUrl="~/image/shoppingcart.gif" CommandName="Select"></asp:ImageButton> </ItemTemplate> </asp:TemplateField> 1. 將 sleep.aspx.vb 的 ImageButton1_Click 改為: Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged Dim ads As New SqlDataSource Dim dv As Data.DataView ads.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Travel.mdf;Integrated Security=True;User Instance=True" dv = ads.Select(New DataSourceSelectArguments) Dim i As Integer = GridView1.SelectedIndex ads.InsertCommand = "INSERT INTO [sleepcart] ([cartCustomer], [SleepCounty], [SleepName], [SleepMoney]) VALUES (@cartCustomer, @SleepCounty, @SleepName, @SleepMoney)" ads.InsertParameters.Add("cartCustomer", Session("cartName")) ads.InsertParameters.Add("SleepCounty", GridView1.Rows(i).Cells(0).Text.ToString) ads.InsertParameters.Add("SleepName", GridView1.Rows(i).Cells(1).Text.ToString) ads.InsertParameters.Add("SleepMoney", GridView1.Rows(i).Cells(3).Text.ToString) ads.Insert() Response.Redirect("~/cartBasket.aspx") End Sub
|
|
|
|
|
|
|