我試圖從複選框列表中將項目添加到SQL Server表中。我認爲這將是通過每個項目循環,然後將其插入到表很容易,但我得到一個未處理的異常與下面的代碼:將項目從複選框列表傳遞到SQL Server表
using (SqlConnection connection = new SqlConnection(connectionString))
{
for (int i = 0; i <= UPCList.Items.Count; i++)
{
string finalSubmit =
"INSERT INTO Boxes (BoxNumber)"
+ "VALUES @selected";
SqlCommand command = new SqlCommand(finalSubmit, connection);
command.Parameters.AddWithValue("@selected", i);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
}
編輯:下面工作的建議之一,但它將放入項目的ID而不是列表項本身的值。如何將列表中每個項目的值插入到SQL表格中?
什麼是例外? –
如果你想知道確切的錯誤,我會建議將你的代碼包裝在for循環中,圍繞'try {} catch {}' – MethodMan
這不是特定的。只是說不知道。 –