将复选框列表中选择的值插入表中 [英] Insert values selected in checkbox list into a table

查看:84
本文介绍了将复选框列表中选择的值插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我无法将复选框列表的选定值插入表格。

你能帮忙吗?我出去..



提前致谢!



我试过的代码是:



Hi,

I was not able to insert the selected values of a checkbox list into the table.
Could you please help me out..

Thanks in advance!

The code I tried is:

public void btnClick()
       {
           con1.Open();
           for (int i = 0; i < 4; i++)
           {
               CheckBoxList2.SelectedIndex = i;
               if (CheckBoxList2.Items[i].Selected)
               {
                   SqlCommand cmd1 = new SqlCommand("insert into DART_IDC_ADD_PROC_MAP(DAR_ID,ADD_PROC_SEL) values (123,'" + CheckBoxList2.Items[i].Text.ToString() + "'", con1);
                   cmd1.ExecuteNonQuery();
               }
               i++;
               MessageBox.Show("Inserted "+CheckBoxList2.SelectedValue.ToString());
           }
           da = new SqlDataAdapter("insert into DART_IDC_ADD_PROC_MAP values (123,"+i.ToString(), con1);
       }

推荐答案

问题是什么?



BTW,非常奇怪,你几行代码不符合你的意思:

1. CheckBoxList2.SelectedIndex = i; (为什么?)

2. MessageBox.Show(在ASP.NET中?)



你还没写好代码。对于你所寻求的,你需要这样做:

1.循环复选框列表中的所有项目

2.查看当前项目是否被选中

3.如果选中,则为项目添加插入声明

4.移至下一个项目。



简单!试试。
And the issue is?

BTW, pretty strange, you have few line of code that does not go what you say:
1. CheckBoxList2.SelectedIndex = i; (Why?)
2. MessageBox.Show (in ASP.NET?)

You have not written a good code. For what you seek, you need to do:
1. Loop though all the items of the checkboxlist
2. See if the current item is selected or not
3. If selected, put an insert statement for the item
4. Move to next item.

Simple! Try out.


检查这个

Check this
string qry;
foreach (ListItem item in CheckBoxList2.Items)
{
    if (item.Selected)
    {
       qry = "INSERT INTO DART_IDC_ADD_PROC_MAP(DAR_ID,ADD_PROC_SEL) VALUES (123,'" + item.Text + "')";
       SqlCommand cmd = new SqlCommand(qry, con1);
       con1.Open();
       cmd.ExecuteNonQuery();
       con1.Close();
     }
}





我希望连接字符串正常,DAR_ID不是主要或唯一的 ADD_PROC_SEL是varchar / nvarchar或char / char



I hope conection string is ok and "DAR_ID" is not primary or unique and "ADD_PROC_SEL" is of varchar/nvarchar or char/char


这篇关于将复选框列表中选择的值插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆