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
LVL 6
r3nderAsked:
Who is Participating?
 
Obadiah ChristopherConnect With a Mentor Commented:
You can use the DataTable.Select method to retrieve the row from the datatable.

http://msdn.microsoft.com/en-us/library/det4aw50(v=vs.90).aspx

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

0
 
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?
0
 
Obadiah ChristopherCommented:
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];
0
 
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?
0
 
r3nderAuthor Commented:
worked great - Thanks
0
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.