Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
string strCheckValue = "";
          if (checkBox1.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox1.Text;
          }
          if (checkBox2.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox2.Text;
          }
          if (checkBox3.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox3.Text;
          }
          if (checkBox4.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox4.Text;
          }
          if (checkBox5.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox5.Text;
          }
          if (checkBox6.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox6.Text;
          }
          if (checkBox7.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox7.Text;
          }
          if (checkBox8.Checked)
          {
              strCheckValue = strCheckValue + "," + checkBox8.Text;
          }
          strCheckValue = strCheckValue.TrimStart(',');
          con.Open();
          cmd.CommandText =("insert into Table1 values('"+strCheckValue+"')");
          cmd.ExecuteNonQuery();
          da.Update(ds, "Table1");
          MessageBox.Show("Data Inserted ");
          con.Close();
Posted
Comments
Sergey Alexandrovich Kryukov 13-Mar-15 2:01am    
Every line of this code screams: this is something opposite to programming. Write code accurately, don't repeat anything, don't use immediate constants, don't hard-code, and so on...
You repeat the same thing 8 times...
—SA
MayankSemwal 13-Mar-15 2:09am    
i understand... i did with checkbox.items.selected . but was not working. so i tried to go with this. even this messy code throwing an error. help me find out
Sergey Alexandrovich Kryukov 13-Mar-15 8:43am    
Throw it all out, write everything accurately. Only then we can address things.
—SA
Santosh K. Tripathi 13-Mar-15 5:56am    
-SA is correct i want to give a a suggestion you can do like this also

string strCheckValue = string.Empty;

strCheckValue += GetCheckBoxText(checkBox1);
strCheckValue += GetCheckBoxText(checkBox2);
strCheckValue += GetCheckBoxText(checkBox3);
strCheckValue += GetCheckBoxText(checkBox4);
strCheckValue += GetCheckBoxText(checkBox5);
strCheckValue += GetCheckBoxText(checkBox6);
strCheckValue += GetCheckBoxText(checkBox7);

strCheckValue = strCheckValue.TrimStart(',');


private string GetCheckBoxText(CheckBox chkBox)
{
string result = string.Empty;
if (chkBox.Checked)
{
result = chkBox.Text + ",";
}
return result;
}

Sergey Alexandrovich Kryukov 13-Mar-15 8:42am    
This code is dreaded. No single line should be repeated. Instead of 7 controls, should be arrays, loops instead of repeated lines.
—SA

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