Listbox more than 1 value

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
Who is Participating?
Obadiah ChristopherConnect With a Mentor Commented:
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

käµfm³d 👽Commented:
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?
Obadiah ChristopherCommented:
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];
r3nderAuthor Commented:
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?
r3nderAuthor Commented:
worked great - Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.