Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i select multiple checkbox from form then how to store in database nd how to write if condition for this?????
Posted
Comments
[no name] 14-Jun-12 4:58am    
What you want to do with this..?? Not clear..

If you want to insert the selected data in single column, you can use following code for storing the data into database. It will be inserting values with comma as a separator.

C#
string selectedItems = string.Empty; 
foreach (ListItem item in this.CheckBoxList1.Items) 
{
    if (item.Selected) 
                selectedItems += item + ","; 
}
SqlCommand cmd = new SqlCommand("Insert into YOUR_TABLE(YOUR_COLUMN) values('" + selectedItems + "')", cn); 
cn.Open(); // I am assuming that you have written down the connection string. 
int iCount = cmd.ExecuteNonQuery(); 
cn.Close();


For selecting:

C#
void LoadCheckList() 
    { 
        string selectedItems = ""; 
        SqlCommand cmd = new SqlCommand("Select YOUR_COLUMN from YOUR_TABLE", cn); // Write your Query i have taken as sample 
        SqlDataReader dr; 
        cn.Open(); 
        dr = cmd.ExecuteReader(); 
        if (dr.Read()) 
        { 
            selectedItems = dr["selectedItems"].ToString(); 
        }
        dr.Close();
        cn.Close(); 
        string[] arr = selectedItems.Split(','); 
        foreach (ListItem item in this.CheckBoxList1.Items) 
        { 
            foreach (string s in arr) 
            { 
                if (item.Text== s) 
                { 
                    item.Selected = true; 
                } 
            } 
        } 
    }
 
Share this answer
 
Comments
tanweer 14-Jun-12 3:13am    
good answer, my 5!
Vani Kulkarni 14-Jun-12 3:42am    
Thanks Tanweer
Sandeep Mewara 14-Jun-12 10:52am    
Good answer. 5!

There is a reply link in my comment on right side - use that to reply back. Above, you commented to your own answer.
Vani Kulkarni 15-Jun-12 0:56am    
Thanks!
C#
foreach(ListItem item in CheckBoxList1.Items)
            if(item.Selected)
                //Here you can get selected item's value 

here you can access all the checked elements.
 
Share this answer
 
foreach Checkbox in the form

if(CheckBox.Checked)
var a = ??? // assign
 
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