Hi, I need help for this problem that i'm trying to solve for a while (i'm Using ASP.net and C#). How do i submit the values from selected checkbox lists into database using c#....


Here's the code:
..........................................................................

<asp:CheckBoxList ID="CheckBoxList1" runat="server" Font-Bold="False" 
                    Font-Names="Vrinda" Width="338px" style="text-align: left">
                    <asp:ListItem>Public Tap</asp:ListItem>
                    <asp:ListItem>Well Water</asp:ListItem>
                    <asp:ListItem>Hand Pump</asp:ListItem>
                    <asp:ListItem>Tap Water Within House</asp:ListItem>
                </asp:CheckBoxList>


 <asp:CheckBoxList ID="CheckBoxList2" runat="server" Font-Bold="False" 
                    Font-Names="Vrinda">
                    <asp:ListItem>Within the House</asp:ListItem>
                    <asp:ListItem>Neighbour's House</asp:ListItem>
                    <asp:ListItem>Within 50 Meters</asp:ListItem>
                    <asp:ListItem>More than 50 Meters</asp:ListItem>
                </asp:CheckBoxList>

................................

Add an event handler

OnSelectedIndexChanged="Check"

Now in the Check function .Add this

for(i=0;i<=CheckBoxList1.Items.Count-1;i++)
{
      if(CheckBoxList1.Items[i].Selected))
        //insert into database
}

Hi, I need help for this problem that i'm trying to solve for a while (i'm Using ASP.net and C#). How do i submit the values from selected checkbox lists into database using c#....


Here's the code:
..........................................................................

<asp:CheckBoxList ID="CheckBoxList1" runat="server" Font-Bold="False" 
                    Font-Names="Vrinda" Width="338px" style="text-align: left">
                    <asp:ListItem>Public Tap</asp:ListItem>
                    <asp:ListItem>Well Water</asp:ListItem>
                    <asp:ListItem>Hand Pump</asp:ListItem>
                    <asp:ListItem>Tap Water Within House</asp:ListItem>
                </asp:CheckBoxList>


 <asp:CheckBoxList ID="CheckBoxList2" runat="server" Font-Bold="False" 
                    Font-Names="Vrinda">
                    <asp:ListItem>Within the House</asp:ListItem>
                    <asp:ListItem>Neighbour's House</asp:ListItem>
                    <asp:ListItem>Within 50 Meters</asp:ListItem>
                    <asp:ListItem>More than 50 Meters</asp:ListItem>
                </asp:CheckBoxList>

................................

Hi try the below. It works...

foreach (ListItem litem in CheckBoxList1.Items)
{
if (litem.Selected)
//Insert in to database
}

i have written this code but it is not working.....
plz help me how to insert the selected data from check box list into database...
and one more thing,each list item have separate column in database......
like....

                      |                       |                       |                         |
WSTapWater | WSWellWater | WSPublicTap | WSHandPump  |          
                      |                       |                       |                         |

c#,code.............

string str;
for(i=0; i<CheckBoxList1.Items.Count; i++)
        { 
            if(CheckBoxList1.Items[i].Selected)        
              {

                  str += CheckBoxList1.Items[i].Text;                  
            }
        }

       cmd.Parameters.AddWithValue("@WSTapWater", str);
        cmd.Parameters.AddWithValue("@WSWellWater", str);
        cmd.Parameters.AddWithValue("@WSPublicTap", str);
        cmd.Parameters.AddWithValue("@WSHandPump", str);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.