Listbox more than 1 value

Posted on 2012-08-16
Last Modified: 2012-08-23
I have a pair of listboxes and I fill 1 from the database (rlbRoles) and allow the user to select 1 or multiple items and move them to the other listbox (rlbActiveRoles).
I fill the listbox from a stored proceedure that returns 3 values for each item and I store them into a datatable. I use 2 of the values for value member and display member I need the third for checking permissions
The problem  is how do I iterate through the selected items and get all three items - here is what I have done so far

         DataTable dtRoles ;
         DataTable dtActiveRoles;
         public void fillRoles()
             string connectionString = ConfigurationManager.ConnectionStrings["TaskManager"].ToString();
             SqlConnection con = new SqlConnection(connectionString);
                 Sentry.Security.KKMembershipUser user = KKPlmData.User.Get(false);
                 int userid = user.UserID;
                 SqlCommand com = new SqlCommand("sp_Membership_GetAllRoleswithUser", con);
                 com.CommandType = CommandType.StoredProcedure;
                 com.Parameters.AddWithValue("@Useruid", userid);
                 SqlDataAdapter sqldba = new SqlDataAdapter(com);
                 DataSet dsRoles = new DataSet();
                 dtRoles = dsRoles.Tables[0];
                 rlbRoles.DataSource = dtRoles;
                 rlbRoles.DisplayMember = "RoleDefinition";
                 rlbRoles.ValueMember = "RoleID";
                 //rlbGroups.ValueMember = "Roledefuid"; <--I need this as well
                 dtActiveRoles = dsRoles.Tables[0].Clone();
                 rlbActiveRoles.DataSource = dtActiveRoles;
                 rlbActiveRoles.DisplayMember = "RoleDefinition";
                 rlbActiveRoles.ValueMember = "RoleID";
                 //rlbActiveGroups.ValueMember = "Roledefuid";<--I need this as well
                 string s = rlbGroups.ValueMember.ToString();
             catch (Exception ex)

Open in new window

I am not sure if it is in the table or what and how to get the values
Question by:r3nder
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    I might be misunderstanding something, but the ListBox class has a SelectedItems property which will provide you with a collection containing all the items a user selected. You can loop over that collection to do something with the selections.

    Is that what you are asking?
    LVL 20

    Expert Comment

    From the stored procedure you can write the query so that it returns the value in format like

    So your values might be like

    RoleNameCombined (Column name)

    And now bind this column to the ValueMember.

    When you retrieve the list of selected items from the listbox you can do like

    string defuid = lstBox.SelectedItem.Text.Split("~")[1];
    LVL 6

    Author Comment

    Hey Kaufmed, long time no.....hear from :) no what I was saying is I have a stored procedure that I didnt write that returns 3 values 2 of which are used in the listbox a third on is used for checking to see if a person is authorized to change that role. Then when I write that info of the selected role(s) back to the database into a users table i need all three values. Since I put all the values into a Datatable I was hoping that when I get the selected items that all three items for each row would be in there - if that is true how do I iterate through the selected items and get all the values
    for each(rolein rlbActiveroles.selectedItems)
     updateDB(dt.selecteditems(Roledefuid, RoleID,RoleDefinition)
    how would I do that? and would that work?
    LVL 20

    Accepted Solution

    You can use the DataTable.Select method to retrieve the row from the datatable.

    foreach(ListItem li in lstBox.SelectedItems)
               Datarow drRow = dt.Select("RoleID = " + li.Value);
               updateDB(drRow["Roledefuid"].ToString(), drRow["RoleID"].ToString(), drRow["RoleDefinition"].ToString());

    Open in new window

    LVL 6

    Author Closing Comment

    worked great - Thanks

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now