Click here to Skip to main content
16,021,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

XML
<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
Posted
Comments
Afzaal Ahmad Zeeshan 30-Dec-14 10:00am    
Where is the code-behind?

1 solution

Here is my behind code

C#
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();
                   }
               }
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900