且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将CheckBox列表值提交到数据库中

更新时间:2023-02-10 13:52:41

ConnectionStrings: MotorConnectionString %>

SelectCommand = SELECT * FROM [Product] > < / asp:SqlDataSource >
< / td >
< / tr >
< tr >
< td class = style3 >
& nbsp; < / td >
< td class = style2 >
< asp:按钮 ID = Button1 runat = server 文字 = 提交 字体粗体 =

字体大小 = 高度 = 45px Width = 150px / >
&LT; / td >
< / tr >
< / table >

< / div >

回复快速回复报告垃圾邮件编辑删除


这是我的后面代码



strCon = ConfigurationManager.ConnectionStrings [ MotorConnectionString]。ConnectionString;
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected == true
{
使用(SqlConnection sqlcon = new SqlConnection(strCon))
{
using (SqlCommand sqlcmd = new SqlCommand( INSERT INTO testProduct VALUES(@ PolNo,@ Business,@ Type,@ Product1,@ Product1。@ Product3)))
{
sqlcmd.CommandType = CommandType。文本;
sqlcmd.Parameters.AddWithValue( @ PolNo,TextBox1.Text);
sqlcmd.Parameters.AddWithValue( @ Business,DropDownList1.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Type,DropDownList2.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Product1,CheckBoxList1.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Product2,CheckBoxList1.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Product3,CheckBoxList1.SelectedValue);
sqlcmd.Connection = sqlcon;

sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
}
}
}


i am working on a project that will allow me submit multiple selected value from checklistBox control to sql 2008 database.

i want users to fill the textBox,pick values from the dropdown and check boxes.... after hitting the button... it shoud submit into the database.

Thanks in advance

see below the html structure:

<div>

  <table class="style1">
  <tr>
  <td class="style3">
  PolicyNumber</td>
  <td class="style2">
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  </td>
  </tr>
  <tr>
  <td class="style3">
  Business</td>
  <td class="style2">
  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

  onselectedindexchanged="DropDownList1_SelectedIndexChanged">
  <asp:ListItem>Select Item</asp:ListItem>
  <asp:ListItem>Private</asp:ListItem>
  <asp:ListItem>Commercial</asp:ListItem>
  </asp:DropDownList>
  </td>
  </tr>
  <tr>
  <td class="style3">
  Type</td>
  <td class="style2">
  <asp:DropDownList ID="DropDownList2" runat="server" Visible="False">
  </asp:DropDownList>
  </td>
  </tr>
  <tr>
  <td class="style3" bgcolor="White">
  Product</td>
  <td class="style2">
  &nbsp;</td>
  </tr>
  <tr>
  <td class="style3" bgcolor="White">
  &nbsp;</td>
  <td class="style2">
  <asp:CheckBoxList ID="CheckBoxList1" runat="server"

  DataSourceID="SqlDataSource1" DataTextField="Product" DataValueField="Product"

  RepeatColumns="2" RepeatDirection="Horizontal" Width="450px">
  </asp:CheckBoxList>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server"

  ConnectionString="<%$ ConnectionStrings:MotorConnectionString %>"

  SelectCommand="SELECT * FROM [Product]"></asp:SqlDataSource>
  </td>
  </tr>
  <tr>
  <td class="style3">
  &nbsp;</td>
  <td class="style2">
  <asp:Button ID="Button1" runat="server" Text="Submit" Font-Bold="True"

  Font-Size="Large" Height="45px" Width="150px" />
  </td>
  </tr>
  </table>

  </div>

Reply Quick Reply Report a Spam Edit Delete

ConnectionStrings:MotorConnectionString %>" SelectCommand="SELECT * FROM [Product]"></asp:SqlDataSource> </td> </tr> <tr> <td class="style3"> &nbsp;</td> <td class="style2"> <asp:Button ID="Button1" runat="server" Text="Submit" Font-Bold="True" Font-Size="Large" Height="45px" Width="150px" /> </td> </tr> </table> </div> Reply Quick Reply Report a Spam Edit Delete


Here is my behind code

strCon = ConfigurationManager.ConnectionStrings["MotorConnectionString"].ConnectionString;
       foreach (ListItem li in CheckBoxList1.Items)
       {
           if (li.Selected == true)
           {
               using (SqlConnection sqlcon = new SqlConnection(strCon))
               {
                   using (SqlCommand sqlcmd = new SqlCommand("INSERT INTO testProduct VALUES (@PolNo,@Business,@Type,@Product1,@Product1.@Product3)"))
                   {
                       sqlcmd.CommandType = CommandType.Text;
                       sqlcmd.Parameters.AddWithValue("@PolNo", TextBox1.Text);
                       sqlcmd.Parameters.AddWithValue("@Business", DropDownList1.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Type", DropDownList2.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Product1", CheckBoxList1.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Product2", CheckBoxList1.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Product3", CheckBoxList1.SelectedValue);
                       sqlcmd.Connection = sqlcon;

                       sqlcon.Open();
                       sqlcmd.ExecuteNonQuery();
                       sqlcon.Close();
                   }
               }
           }