Link to home
Start Free TrialLog in
Avatar of r3nder
r3nderFlag for United States of America

asked on

Listbox more than 1 value

Hello,
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);
             try
             {
                 Sentry.Security.KKMembershipUser user = KKPlmData.User.Get(false);
                 con.Open();
                 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();
                 sqldba.Fill(dsRoles);
                 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();
                 con.Close();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
                 con.Close();
             }
         }

Open in new window


I am not sure if it is in the table or what and how to get the values
Thanks
R
Avatar of kaufmed
kaufmed
Flag of United States of America image

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?
From the stored procedure you can write the query so that it returns the value in format like

RoleID~Roledefuid
So your values might be like

RoleNameCombined (Column name)
----------------------------------
1~Def1
2~Def2

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];
Avatar of r3nder

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India 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 r3nder

ASKER

worked great - Thanks