Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one table A :data's are
categoryid   categoryname     image
   1           Breakfast   <Binarydata>
   2           Lunch       <Binarydata>
   3           Dinner      <Binarydata>

In PageLoad i display these data's in Checkboxlist
C#
SqlCommand cmd1 = new SqlCommand("select Categoryname from A", connect);
                SqlDataReader dr1 = cmd1.ExecuteReader();
                while (dr1.Read())
                {
                    chkboxcategory.Items.Add(dr1.GetValue(0).ToString());
                }

after add some item like Coffee in both breakfast and dinner means select two value in checkbox list then i add another table B..Example
C#
id    name   categoryname
1     Coffee   Breakfast
2     Coffee   Dinner

How to insert the data's in B table?
Posted

Try this...:)

C#
foreach (ListBox lb in checkedListBox1.Items)
{
    if (lb.SelectedItem)
    {
        str += lb.ValueMember + ",";
    }
}
 
Share this answer
 
Code:

C#
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected == true)
            {
                str += " " + CheckBoxList1.Items[i].ToString();
            }
        }



where str can be an array or a simple string, as per your requirement...
Hope this meets your requirement..
Thanks.
 
Share this answer
 
Comments
Member 12133159 3-Feb-16 4:41am    
Hello Shibashish,
I'm using same loop but in database it is taking "System.Web.UI.WebControls.CheckBoxList" any suggestions
C#


C#
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected == true)
            {
                string sqlInsert = "insert into tableB values (3,'Coffee','"+CheckBoxList1.Items[i].ToString()+"')";
SqlCommand cmd1 = new SqlCommand(sqlInsert ,connect);
cmd1.executenonquery();
            }
        }
 
Share this answer
 
Hi,

try this:


C#


C#
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected == true)
            {
                string sqlInsert = "insert into tableB values (3,'Coffee','"+CheckBoxList1.Items[i].ToString()+"')";
SqlCommand cmd1 = new SqlCommand(sqlInsert ,connect);
cmd1.executenonquery();
            }
        }
 
Share this answer
 
C#
using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand(cQuery))
    {
        cmd.Connection = conn;
        cmd.CommandTimeout = 0;
        cmd.CommandType = CommandType.Text;
        cmd.Connection.Open();
        for (int i=0; i < CheckBoxListChannels.Items.Count; i++)
        {
            if (CheckBoxListChannels.Items[i].Selected == true)
            {
                cmd.Parameters.Add("Chnl_FormID", SqlDbType.NVarChar, 50).Value = lblPublishingRequestId.Text;
                cmd.Parameters.Add("Channel", SqlDbType.NVarChar, 50).Value = CheckBoxListChannels.SelectedItem.Text;
cmd.ExecuteNonQuery();
            }
        }    
        
        cmd.Connection.Close();
        cmd.Dispose();
    }
}
 
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