mercredi 27 septembre 2017

How to select multiple checkbox from single column data in c# .net

I am trying to make a restaurant form which stores data to a database when a user orders food. If the user comes again and enters his first and last name, the other data, like food and pickup option, should be filed automatically. For the food, I have a checkbox.

Here is the insert code:

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;
}

The strCheckValue is stored in the database and gives result like this: ,Samosa,Biryani,Naan

Now I want to select all the food items a user previously selected while ordering the food when he hits remember me button.

For that my code is:

//checkbox value display
CheckBox1.Checked = false;
CheckBox2.Checked = false;
CheckBox3.Checked = false;
CheckBox4.Checked = false;
CheckBox5.Checked = false;
CheckBox6.Checked = false;
CheckBox7.Checked = false;
string aa = dr["ctm_food"].ToString();
string[] a = aa.Split(',');
Label10.Text = a[2].ToString();
foreach (Control cc in this.Controls)
{
    if(cc is CheckBox)
    {
        CheckBox b = (CheckBox)cc;
        for(int j=1; j<a.Length; j++)
        {
            if (a[j].ToString() == b.Text)
            {
                b.Checked = true;
            }
        }
    }
}

In label10, I can see the food that the user ordered. But the checkbox is not getting selected. What will be the right approach to complete this exercise?




Aucun commentaire:

Enregistrer un commentaire