Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
suppose we have 10 checkbox thn 5 is selected.i want that 5 chechbox text store in string by seperated (,) thn that string pass to sqlquery for insertiojn in database.....please help me.........thanx
Posted
Comments
Muralikrishna8811 11-Oct-11 9:19am    
still not working

Hi,

Here I tried some code for your requirement.try this once...

ASP.NET
<asp:checkboxlist id="CheckBoxList1" runat="server" xmlns:asp="#unknown">
      <asp:listitem text=" checkbox1" value=" checkbox1"></asp:listitem>
      <asp:listitem text=" checkbox2" value=" checkbox2"></asp:listitem>
      <asp:listitem text=" checkbox3" value=" checkbox3"></asp:listitem>
      <asp:listitem text=" checkbox4" value=" checkbox4"></asp:listitem>
      <asp:listitem text=" checkbox5" value=" checkbox5"></asp:listitem>
      <asp:listitem text=" checkbox6" value=" checkbox6"></asp:listitem>
      <asp:listitem text=" checkbox7" value=" checkbox7"></asp:listitem>
      <asp:listitem text=" checkbox8" value=" checkbox8"></asp:listitem>
      <asp:listitem text=" checkbox9" value=" checkbox9"></asp:listitem>
      <asp:listitem text=" checkbox10" value=" checkbox10"></asp:listitem>
     </asp:checkboxlist><br />
     <asp:button id="Button2" runat="server" text="submit" onclick="Button2_Click" xmlns:asp="#unknown" /><br />
     <div id="resdiv" runat="server"></div>


And the code behind file contains following code

C#
protected void Button2_Click(object sender, EventArgs e)
{
    string str = string.Empty;
    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
        if (CheckBoxList1.Items[i].Selected)
        {
            if (str.Length > 0)
            {
                str = str + "," + CheckBoxList1.Items[i].Text;
            }
            else
            {
                str = str + CheckBoxList1.Items[i].Text;
            }
        }
    }
    resdiv.InnerHtml = "The Result String is :" + str;

}


Here you can use that str variable to store into database.

I hope you know how to store that value in database


All the Best
 
Share this answer
 
Comments
soniya sangal 23-Sep-11 6:40am    
thanx
Muralikrishna8811 11-Oct-11 9:03am    
you are welcome
Really bad idea. Don't save UI. Have a separate data layer and data binding with UI. Don't save strings with some , save logical data. In particular, 10 2-state check boxes maps to a 10-bit bit set, which is best represented with 16-bit integer (database type "SMALLINT"), one bit per check box, 6 bits remain unused.

If each bit in check box set makes separate semantic sense, create a mapping on a bit set represented by an enum type with semantic names for each member. In C# it will look like:

C#
enum OptionSet : System.Int16 {
   None = 0,
   CaseSensitive = 1 << 0, // 1
   Backward      = 1 << 1, // 2
   UseRegEx      = 1 << 2, // 4
   Selection     = 1 << 3, // 8
   //...
}


Store this thing in dababase type casted to SMALLINT, type case it back to OptionSet for semantic operation (business logic), map to the set of check boxes bit by bit using cycle and bit operations. I trust you know how to set, clear and test a bit…

—SA
 
Share this answer
 
v3

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