我正在嘗試使用存儲過程將checkboxlist值插入到SQL Server中。我收到此錯誤如何循環CheckBoxList併爲每個選中的值向數據庫提交值?
項目爲了評估索引屬性,必須限定該屬性,並且參數必須由用戶明確提供。
這裏是我的存儲過程:
ALTER PROCEDURE [dbo].[usp_insertquestionnarie]
(@interestid int)
INSERT INTO [a_abacus].[dbo].[joininterest]
VALUES (@interestid)
這裏是我的VB代碼:
Protected Sub cmdsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdsubmit.Click
Dim strConnString As String = WebConfigurationManager.ConnectionStrings("a_abacus").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand("usp_insertquestionnarie", con) 'references connectionstring and calls store procedure'
cmd.CommandType = Data.CommandType.StoredProcedure 'sets default to be stored procedure'
lblmessage.Text = String.Empty
Dim Item As ListItem
lblMessage.Text = ""
For Each Item In chkboxinterest.Items
If Item.Selected = True Then
con.Open()
cmd.Parameters.AddWithValue("@interestid", Item.Value)
cmd.ExecuteNonQuery() 'for updating database'
lblmessage.Text = "Successfull"
cmd.Parameters.Clear()
Else
lblmessage.Text = "Error"
End If
con.Close()
Next
End Sub
這裏的aspx頁面的標記:
<asp:CheckBoxList ID="chkboxinterest" runat="server" RepeatColumns="3"
Width="650px">
<asp:ListItem class="chkbox" runat="server" Text="Aerobics" Value="1" />
<asp:ListItem class="chkbox" runat="server" Text="Antiques & Collectibles" Value="2" />
<asp:ListItem class="chkbox" runat="server" Text="Arts & Crafts" Value="3" />
<asp:ListItem class="chkbox" runat="server" Text="Astronomy" Value="4" />
<asp:ListItem class="chkbox" runat="server" Text="Auto Racing" Value="5" />
</asp:CheckBoxList>
什麼是你所得到 – 2011-06-10 15:40:56
錯誤這是否能改變什麼' Auto Racing asp:ListItem>'?奇怪的是,你無法獲得價值。 –
2011-06-10 16:04:31
也蒂姆感謝迴應,但是沒有任何改變 – Tim 2011-06-10 17:21:39