Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6567353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:19:35+00:00 2026-05-25T14:19:35+00:00

foreach (ListItem item in ListBoxMembers.Items) { if (item.Selected) { countSelected += 1; } }

  • 0
 foreach (ListItem item in ListBoxMembers.Items)
 {
     if (item.Selected)
     {
         countSelected += 1;
     }

 }   
 for (int counter = 0; counter < countSelected; counter++)
 {
    string firstName = ListBoxMembers.SelectedItems[counter].Value;
 }

This isn’t returning the selected value. Where am I going wrong?
the error it throws is
System.Web.UI.WebControls does not contain defenition for listboxmembers.selectedItems error

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Web.Security;
using System.Data;
using System.Web.UI.WebControls;

These are the name spaces I am using.

This is what I am trying to do

 for (int counter = 0; counter < countSelected; counter++)
            {

        //To get User ID
            string firstName=ListBoxMembers.SelectedItems[counter].Value;

           // string firstName = ListBoxMembers.Items[counter].Value;
            string GUserIDQueryText = "SELECT UserID FROM tbl_user WHERE FirstName ";
            int userID = Convert.ToInt32(server.performQuery(GUserIDQueryText, firstName, MySqlDbType.VarChar));

            //Insert into tbl_userGroups
            string insertIDText = "INSERT INTO tbl_usergroups 

(tbl_group_GroupID,tbl_user_UserID) VALUES(@tbl_group_GroupID,@tbl_user_UserID)";
...
     }

I want to add all the selected users to the table.

the error it throws is System.Web.UI.WebControls does not contain defenition for listboxmembers.selectedItems Are you missing a directory or assembly directive. Why i am not able to use selectedItems

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:19:36+00:00Added an answer on May 25, 2026 at 2:19 pm
    countSelected  =  ListBoxMembers.Items.Cast<ListItem>().Where(i => i.Selected).Count();
    

    if you are trying to get all selected items, you can do

    var selectedNames = ListBoxMembers.Items.Cast<ListItem>()
                                      .Where(i => i.Selected)
                                      .Select(i => i.Value)
                                      .ToList()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code: foreach (ListItem item in lbUnassigned.Items) { if (item.Selected)
foreach(Listitem item in CheckboxList1.Items) { if(item.Selected == true) { return true; } } return
Imagine that during a foreach(var item in enumerable) The enumerable items change. It will
foreach (ListItem li in ListBox1.Items) { SqlConnection con = new SqlConnection(@Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true);
foreach($arrayOne as $value){ do function } In the above example, I'd like to pass
foreach (var person in peopleList.Where(person => person.FirstName == Messi)) { selectPeople.Add(person); } I am
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
IEnumerable<String> existedThings = from mdinfo in mdInfoTotal select mdinfo.ItemNo; IEnumerable<String> thingsToSave = from item
Although this probably isn't best practice, I am trying to clear a series of
I want to show all fields of a certain ListItem. This includes LookUpFields and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.