Click here to Skip to main content
15,915,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Design:
C#
<table class="style1">
        <tr>
            <td align="right">
                Name :</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server">
            </td>
        </tr>
        <tr>
            <td align="right">
                Address :</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server">
            </td>
        </tr>
        <tr>
            <td align="right">
                Mobile :</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server">
            </td>
        </tr>
        <tr>
            <td align="right">
                 Product Items :</td>
            <td>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                    RepeatDirection="Horizontal">
                    <asp:ListItem>LapTop
                    <asp:ListItem>Mobile
                    <asp:ListItem>Bike
                
            </td>
        </tr>
        <tr>
            <td align="right">
                 </td>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
            </td>
        </tr>
        <tr>
            <td align="right">
                 </td>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Label">
            </td>
        </tr>
        <tr>
            <td align="right">
                 </td>
            <td>
                 </td>
        </tr>
        <tr>
            <td align="right">
                 </td>
            <td>
                 </td>
        </tr>
        <tr>
            <td align="right">
                 </td>
            <td>
                 </td>
        </tr>
    </table>

Code:
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
       con.Open();
       string str = "insert into CheckTable values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + CheckBoxList1.SelectedItem.Value + "')";
       SqlCommand cmd = new SqlCommand(str, con);
       cmd.ExecuteNonQuery();
       con.Close();
       Label1.Text = "data inserted sucessfully";
   }

Here once fill these like name: kkkk, address: hhhhhh, mobile: 1234567890, product items: laptop, mobile, bike


here after fill store in database table but not storing like:
columns:(name,address,mobile,productItems)= kkkk,hhhhhh,1234567890,laptop,mobile,bike

but storing data in table like:kkkk,hhhhhh,1234567890,laptop

here not store checkbox list all data and i want all please reply me any examples and above code any mistakes please reply me
Posted
Updated 19-Jun-14 19:40pm
v3
Comments
Dinesh.V.Kumar 20-Jun-14 1:36am    
Try the below

string selectedItems= string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
if(item.selected)
selectedItems=item.selected.value + ",";

}

selectedItems = selectedItems.Remove(selectedItems.Length-1, 1);

PS : I am not sure of the syntax, but you can use this logic...Try and let me know if its working..
Bh@gyesh 20-Jun-14 1:42am    
Check your insert query is properly generated or not? Try Dinesh's answer to build 3rd parameter i.e. productitems

Update your code as below:

C#
protected void Button1_Click(object sender, EventArgs e)
  {
      string selectedValues = "";

      foreach (var item in CheckBoxList1.Items)
      {
          selectedValues = selectedValues + "," + item.ToString();

      }

      selectedValues = selectedValues.Substring(1, selectedValues.Length - 1);

      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
      con.Open();
      string str = "insert into CheckTable values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + selectedValues + "')";
      SqlCommand cmd = new SqlCommand(str, con);
      cmd.ExecuteNonQuery();
      con.Close();
      Label1.Text = "data inserted sucessfully";
  }
 
Share this answer
 
Comments
member1431 20-Jun-14 2:47am    
Dear Prasad Avanoori,

protected void Button1_Click(object sender, EventArgs e)
{
string selectedValues = "";

foreach (var item in CheckBoxList1.Items)
{
selectedValues = selectedValues + "," + item.ToString();

}

selectedValues = selectedValues.Substring(1, selectedValues.Length - 1);

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
con.Open();
string str = "insert into CheckTable values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + selectedValues + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = "data inserted sucessfully";
}
here small error coming means:

once select laptop,mobile,bikes these are store in table like: laptop,mobile,bikes
and
select laptop only store in table like: laptop,mobile,bikes

please check and reply me
You have to loop through all the selected Items. and store in different variables then try to insert.
 
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