?
Solved

Listbox more than 1 value

Posted on 2012-08-16
5
Medium Priority
?
673 Views
Last Modified: 2012-08-23
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
0
Comment
Question by:r3nder
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38303773
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
 
LVL 20

Expert Comment

by:informaniac
ID: 38304739
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
 
LVL 6

Author Comment

by:r3nder
ID: 38304915
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
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 38306014
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
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38328071
worked great - Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Make the most of your online learning experience.
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 …
Progress

850 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