【发布时间】:2015-10-25 13:52:12
【问题描述】:
我正在使用 ASP.NET Web 应用程序,但在将多个选中的复选框列表添加到数据库时遇到问题。我希望用户能够选择多个复选框。我已经尝试了一些方法,但它不起作用即使我的数据库连接与其他表正常工作,数据库仍然是空的。
Destinations 表:
CREATE TABLE [dbo].[Destinations]
(
[Id] INT NOT NULL PRIMARY KEY,
[North] INT NULL DEFAULT 0,
[West] INT NULL DEFAULT 0,
[South] INT NULL DEFAULT 0,
[East] INT NULL DEFAULT 0,
CONSTRAINT [FK_Destinations_DeliveryMen] FOREIGN KEY ([Id]) REFERENCES [DeliveryMen]([Delivery_ID])
)
而我的CheckBoxList 是:
<asp:CheckBoxList ID="CheckBoxList2" runat="server" RepeatColumns="2" RepeatDirection="Horizontal" Width="263px">
<asp:ListItem text="North " Value="1"></asp:ListItem>
<asp:ListItem text="South " Value="2"></asp:ListItem>
<asp:ListItem text="West " Value="3"></asp:ListItem>
<asp:ListItem text="East " Value="4"></asp:ListItem>
</asp:CheckBoxList>
更新: 我尝试过的 C# 代码:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
conn.Open();
string insertcheckboxlist = "insert into Destinations (North,West,South,East) values(@1,@3,@2,@4))";
SqlCommand comm = new SqlCommand(insertcheckboxlist, conn);
comm.Parameters.AddWithValue("@CheckBoxList2", CheckBoxList2.SelectedItem.Value);
【问题讨论】:
-
如果您发布用于尝试保存输出的代码,您将获得更多帮助。
-
好的,但这不会触发您的命令运行..您的代码中确实有它吗?
-
comm.ExecuteNonQuery();
-
我认为问题在于您的参数化查询需要 4 个参数(@1、@2、@3 和 @4),但您只传递了一个参数,甚至还有一个不同的名称(@CheckBoxList2 )。
-
我需要将
Identity Specification添加到Id字段并删除外键。
标签: c# html asp.net database checkboxlist