Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a groupbox in which there are 12 checkbox and i want to store its checked value property in a table in different column.but i dont know how to do, please anyone help me
Posted

1 solution

You can create 12 columns (assuming the number of check-boxes is not going to change often resulting in an update to the database and code as well) in your database table as boolean or binary types and store 1 or true if the check-box was checked from the UI and 0 otherwise. Similarly, when retrieving the values if the column value is 1 then check the corresponding check-box else not.

This is definitely one way to do, but may be there is a cleverer way to achieve this as well. Hope this helps.

Cheers.
 
Share this answer
 
Comments
143Deepak 20-Aug-12 10:22am    
sir,i already have the table columns of boolean data types,but i did not understand how to bind the particular checkbox to specific column,and how to get the value at once from groupbox,sir please guide me.
I.explore.code 20-Aug-12 10:41am    
when you retrieve the values from the table they would be either "true" or "false", right? so if the value for a particular is "true" you set the corresponding checkbox's checked property to "true" otherwise "false". in pseudocode for e.g.,
if ((bool)ds.Tables[0].Rows[i]["isavailable"] == true)
{
checkboxIsAvailable.Checked = true;
}

you would have to do this for all such columns and checkboxes. Does this help?
143Deepak 20-Aug-12 11:22am    
sir if u dont mind,will u like to guide me that how to get the checkbox value(checked or unchecked) please provide me a block of code.for inserting data.
I.explore.code 20-Aug-12 11:31am    
I honestly its that complicated if you try a little harder. A CheckBox control has a "Checked" property which you can read while inserting your data. In the pseudocode that I posted in my last comment, just swap the condition and action of the if block. if (checkbox.Checked == true) {(bool)ds.Tables[0].Rows[i]["isavailable"] = true;} (assuming you are using dataset for moving data).
143Deepak 20-Aug-12 12:07pm    
i know sir,but doing like that the code will get big .is there any way or by using any type of loop which return only checked or unchecked no. of checkbox,please go through this code it need any modification than please do it.


void LoadCheckList()
{
string intersts = "";
SqlCommand cmd = new SqlCommand("Select Interests from Demo", cn); // Write your Query i have taken as sample
SqlDataReader dr;
cn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
intersts = dr["Interests"].ToString();
}
cn.Close();
string[] arr = intersts.Split(',');
foreach (ListItem item in this.CheckBoxList1.Items)
{
foreach (string s in arr)
{
if (item.Text== s)
{
item.Selected = true;
}
}
}
}

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