We help IT Professionals succeed at work.

foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.CheckBoxList'

mugsey
mugsey asked
on
903 Views
Last Modified: 2013-11-26
I am getting the error

foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.CheckBoxList'

When I try and run this code

 foreach (Control ctrl in this.chkListMyInterests)
        {
            if (ctrl is CheckBox)
            {
                CheckBox chk = (CheckBox)ctrl as CheckBox;
                if (chk.Text == "Maritime")
                {
                    if (chk.Checked)
                    {
                        //maritime is included in the search criteria
                        oClause.column = "[maritime]";
                        oClause.operand = " = ";
                        oClause.criteria = "1";
                        cClauses.Add(oClause);

                    }
                }
etc etc etec

What is the right syntax please
Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
I guess you'll have to use the CheckBoxList.Items

foreach (Control ctrl in this.chkListMyInterests.Items)

example:
foreach (ListItem htlRmItem in cblRoomFacilities.Items)
        {
            if (htlRmItem.Selected == true)
            {
                cmdInsertRmFac.Parameters.Clear();
                cmdInsertRmFac.Parameters.Add("@HotelID", SqlDbType.Int).Value = txtID.Text;
                cmdInsertRmFac.Parameters.Add("@RoomFacilityID", SqlDbType.Int).Value = htlRmItem.Value;
            }
        }

Author

Commented:
Hi

But I need to check the name of each checkbox, for example

  if (chk.Text == "cookery")
{
//amend sql query
}

How can I do that
Project manager
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Actually I have sorted it using

  foreach (ListItem ctrl in this.chkListMyInterests.Items)
        {
         
               
                if (ctrl.Text == "Cookery")
                {
                    if (ctrl.Selected)
                    {
                        //cookery is included in the search criteria
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
Glad it's solved.
Don't forget to close the question
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.