• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 873
  • Last Modified:

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
0
mugsey
Asked:
mugsey
  • 3
  • 2
1 Solution
 
DhaestCommented:
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;
            }
        }
0
 
mugseyAuthor 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
0
 
DhaestCommented:
Do you use a checkboxlist or do you use separated checkboxes ?

foreach(Control myControl in this.Controls)
{
   if (myControl.GetType().ToString() == "System.Windows.Forms.CheckBox")
        if (myControl.Text == "cookery")
 ...
0
 
mugseyAuthor 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
0
 
DhaestCommented:
Glad it's solved.
Don't forget to close the question
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now