473,508 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get all selected values from listbox

JustRun
127 New Member
Hi,

I'm trying to get all values from an asp:listbox that enable multiple choices, and i made the following code but it only gets the last selected item, only one.
I need to collect them all
Expand|Select|Wrap|Line Numbers
  1.         for (int i = 0; i <= lstDept.Items.Count-1; i++)
  2.         {
  3.             if (lstDept.Items[i].Selected)
  4.             {
  5.                 try
  6.                 {
  7.                     cmd = new SqlCommand(
  8.                         "INSERT INTO CoDepartmentTable(CompanyID, DeptID) " +
  9.                         " VALUES (@CompanyID, @DeptID) ", cnn);
  10.  
  11.                     cnn.Open();
  12.                     cmd.Parameters.AddWithValue("CompanyID", new Guid(ddlCompany.SelectedValue));
  13.                     cmd.Parameters.AddWithValue("DeptID", new Guid(lstDept.SelectedValue));
  14.  
  15.                     if (cmd.ExecuteNonQuery() == 1)
  16.                         lblErr.Text = "Your data have been saved successfuly";
  17.                     else
  18.                         lblErr.Text = "Unable to insert data </br> please refresh the page then try again";
  19.                 }
  20.  
  21.                 catch (Exception ex)
  22.                 {
  23.                     lblErr.Text = "Cannot insert duplicated value, <br/> The department '" + lstDept.SelectedItem.Text + "' had been assigned to this company before";
  24.                 }
  25.                 finally
  26.                 {
  27.                     if (cnn != null) cnn.Close();
  28.                 }
  29.             }
  30.  
  31.         }
  32.  
Any Ideas :(
Sep 24 '08 #1
2 1498
kiran143
2 New Member
i have an example which i got from internet
Expand|Select|Wrap|Line Numbers
  1. <asp:Label id="Message" runat="server"></asp:Label>
  2.     <br />
  3.     Which city do you wish to look at hotels for?<br />
  4.     <form runat="server">
  5.         <asp:listbox id="list1" runat="server" selectionmode="multiple">
  6.             <asp:listitem>A</asp:listitem>
  7.             <asp:listitem>B</asp:listitem>
  8.             <asp:listitem>C</asp:listitem>
  9.         </asp:listbox>
  10.         <br />
  11.         <input type="submit" value="Submit Query" />
  12. </form>
  13.  
in page load paste this

Expand|Select|Wrap|Line Numbers
  1. string msgCitiesList = "";
  2.  
  3.       if (Page.IsPostBack == true)
  4.  
  5.         if (list1.Items[0].Selected == true)
  6.         {
  7.           msgCitiesList = msgCitiesList + list1.Items[0].Text + "<br />";
  8.         }
  9.  
  10.         if (list1.Items[1].Selected)
  11.         {
  12.           msgCitiesList = msgCitiesList + list1.Items[1].Text + "<br/>";
  13.         }
  14.  
  15.         if (list1.Items[2].Selected)
  16.         {
  17.           msgCitiesList = msgCitiesList + list1.Items[2].Text + "<br />";
  18.         }
  19.  
  20.         if (msgCitiesList != "")
  21.         {
  22.            Message.Text = "You have selected: <br />" + msgCitiesList;
  23.         }
  24.         else
  25.         {
  26.            Message.Text = "";
  27.         }
  28.       }
Sep 24 '08 #2
JustRun
127 New Member
Thank You, my code worked well just like that, I dont know ehy it didn't work b4 but at any rate, it works now.

Thank you
Sep 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2816
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
1
8178
by: Matthew Louden | last post by:
I created a listbox with multiple selectionmode property, but I have trouble to get a list of selected values. The following only gets the first selected value, not others. TextBox2.Text =...
3
5731
by: Big E | last post by:
I'm using ASP.Net and SQL Server. I have a listbox looks up data from a table and fills the listbox. I have a stored procedure that looks up the selected values and tries to show them as selected...
5
1991
by: Paul | last post by:
Hi I have a listbox and have the selection mode set to multiple in the property box. I am trying to set multiple selected values in code but it only seems to use the last one....
0
1249
by: Mardy | last post by:
Hello I'm using a multi select listbox that allows the user to select one or more values that are stored in a database. The listbox values are generated from a lookup table (disciplines) and the...
3
8445
by: Richard Albrecht | last post by:
I have been trying to figure out for days on how to read values from a Bound ListBox. The listBox gets the values from an Access Table. I can read values fine for Non-Bound ListBoxes, But the...
2
2747
by: John | last post by:
I have a listbox that is databound when my form loads. A user can then select and option using a drop down box. When the user selects an option the corresponding items in the listbox gets selected....
2
4069
by: Hitesh | last post by:
I have a listbox and the values get selected (highlighted) from code. I can highlight the corresponding list box items, but they do not show in the listbox I have to scroll through the list box to...
3
19049
by: Mitch | last post by:
Is the following a correct representation of the relationship of the selected index to selected item: INDEX ITEM 0 "item 1" 1 "item 2" 2 "item 3" and so on. I keep...
3
6282
by: peter.mosley | last post by:
I've tried googling for the answer to this problem, without any luck. I'm sure the truth must be out there somewhere! I have a multiselect listbox populated with many items (set by the RowSource...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.