Link to home
Start Free TrialLog in
Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

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;
            }
        }
Avatar of mugsey

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mugsey

ASKER

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
Glad it's solved.
Don't forget to close the question