Click here to Skip to main content
15,608,053 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,

I have a form with eight checkbox.

My intention is to control if the checkbox is checked, so I used this code:

if(cbMouse.Checked == true) 
//do something

When checkbox is checked I want to insert the checkbox's name into a sql parameter.
For example:

if(cbMouse.Checked == true)
string query = "INSERT INTO TEST (things) VALUES (@things)";
SqlCommand cmd = new SqlCommand(query, cn);

cmd.Parameters.Add("@things", cbMouse.Text.ToUpper());

My question is: how I can do a multiple control for eight checkbox and insert checkbox's name into a single sql parameter (@things) ?
Sergey Alexandrovich Kryukov 27-Nov-14 3:26am    
"if (cbMouse.Checked == true)" is nonsense. cbMouse.Checked is Boolean, and then you compare a Boolean value with true, to get another Boolean.
If should be
if (cbMouse.Checked) //...

Sorry for not answering your question. You first need to sort out the basics.

As to your question... What is "multiple control"? What is checkbox's name? The property "Name"? Why using it?...
All that doesn't seem to make sense...

DamithSL 27-Nov-14 3:30am    
SA, I know you mean "if (cbMouse.Checked)", but mistakenly past same code in your comment.
Sergey Alexandrovich Kryukov 27-Nov-14 3:49am    
Oh, sure, thank you very much.
DamithSL 27-Nov-14 3:32am    
assume two checkboxes checked out of 8 checkboxes, then what is the sql statement you want to build? what is the value you expected as @things?
nicola_melc 27-Nov-14 3:38am    
So , I am creating an application to manage computer's repair.
When a customer leaves the PC , it could happen that leave the power adapter, mouse or some other object.
I use checkbox to manage this object.

I have 8 checkbox called : cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8.

I want to control if this 8 checkbox are checked.
And if are checked, write checkbox's name (cb1.Text or cb2.Text ....) into a sql parameter (@things).

For multiple control I refer to the if-else structure to do. Or how can i do? There is another way?

Maybe, now the problem is more clear.

1 solution


Put Panel control (name it panThings) on your form and then move your checkboxes to that panel to group them.
Then use this code:
var things = string.Join(",", panThings.Controls.OfType<CheckBox>().Where(x => x.Checked).Select(x => x.Text.ToUpper()));

string query = "INSERT INTO TEST (things) VALUES (@things)";
SqlCommand cmd = new SqlCommand(query, cn);

cmd.Parameters.Add("@things", things);

And don't forget to add System.Linq to your using clause at top of the file.

[Update 1]
Suppose that you have checkboxes with texts:
Mouse, Keyboard, Hard drive, Monitor and so on.
Then if you check i.e. Mouse and Monitor checkboxes output will be:

And that value will be stored in DB.

[Update 2 - Answer to comment]
To restore selected items do this:
// Get your thing from DB
var thingsFieldValue = dt.Rows[0]["PUT_YOUR_THINGS_DB_FIELD_NAME_HERE"].ToString();
// Split them and store in string[] (array of string)
var things = thingsFieldValue.Split(",".ToCharArray());

// Iterate through all elements in array
foreach (var thing in things)
    // Find proper CheckBox control
    var checkBox = panThings.Controls.OfType<CheckBox>().Where(x => x.Text.ToUpper() == thing).FirstOrDefault();
    // If checkbox was found, set its Checked property
    if (checkBox != null)
        checkBox.Checked = true;

Share this answer
nicola_melc 27-Nov-14 3:54am    
Perfect, it was what I wanted !
Marcin Kozub 27-Nov-14 3:55am    
Glad I could help you :)
nicola_melc 27-Nov-14 4:20am    
Another question:

if I want to read from database my data (@things) and split it, how can i do?

I'm using this code:

SqlCommand dati = new SqlCommand(selezioneDati, connessione);

SqlDataReader dr = dati.ExecuteReader();

DataTable dt = new DataTable("dbo.ripRiparazioni");


if (dt.Rows.Count > 0)
txtNome.Text = dt.Rows[0]["Nome"].ToString();
txtCognome.Text = dt.Rows[0]["Cognome"].ToString();
dtpData.Value = Convert.ToDateTime(dt.Rows[0]["Data"].ToString());
txtAnno.Text = dt.Rows[0]["Anno"].ToString();
txtCellulare.Text = dt.Rows[0]["Cellulare"].ToString();
txtIndirizzo.Text = dt.Rows[0]["Indirizzo"].ToString();
txtCitta.Text = dt.Rows[0]["Citta"].ToString();
txtMarca.Text = dt.Rows[0]["Marca"].ToString();
txtModello.Text = dt.Rows[0]["Modello"].ToString();
txtNumeroSerie.Text = dt.Rows[0]["NumeroSerie"].ToString();
rtbProblemaRiscontrato.Text = dt.Rows[0]["ProblemaRiscontrato"].ToString();

//read here @things and split. So I can put checked my checkbox directly from database
Marcin Kozub 27-Nov-14 4:35am    
I've updated my answer.
nicola_melc 27-Nov-14 5:11am    
Very good ! Thank You so much.

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