Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET ListBox
can anyone suggest the code to store multiple selected values from listbox to database in asp.net c# ?
Posted 17-Oct-10 4:19am
Comments
Karthik. A at 17-Oct-10 10:26am
   
Try to be a bit more elaborate, give us some code to help you in a better way. I guess you must be able to get multiple selected values from the listbox (w/ SelectionMode set to Multiple). If your question is about the next steps, give us some more info. to help you...
shakil0304003 at 17-Oct-10 10:37am
   
Your requirement is not clear.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Or something like this:
 
foreach (object item in listbox.SelectedItems)
{
    // do domething
}
 
In my case, I'd put a property into the "item" object that returns an XML representation of the object, and pass that to the database. That way, the loop shown above would look something like this:
 
XElement root = new XElement("root");
foreach (object item in listbox.SelectedItems)
{
    XElement node = (item as MyItemType).AsXElement;
    root.Add(node);
}
// pass the root element as a string to your stored proc
 
See how clean and tidy that is?
  Permalink  
v2
Comments
Canny Brisk at 9-Sep-13 8:11am
   
5ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

something link this
if (ListBox1.Items.Count > 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    string selectedItem = ListBox1.Items[i].Text;
                    //insert command
                }
            }
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Its upto your design of database. You can store the data in multiple ways.
One way , you can put the multiple rows according to the selection.
Or you can also save it using some separator like comma in a single row.
 
To get selected items Iterate all the items in listbox and find all the seleced elements using the selected property of listitem. Now according to your design, you can save one by one, insert it in database in one go.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Hi,
 
Try this-
 
HTML-
 
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Selected="True" Value="1">a</asp:ListItem>
    <asp:ListItem Selected="True" Value="2">b</asp:ListItem>
    <asp:ListItem Value="3">n</asp:ListItem>
</asp:ListBox>
 
Code-
 
var selected = ListBox1.GetSelectedIndices().ToList();
var selectedValues = (from c in selected
                      select ListBox1.Items[c].Value).ToList();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

See the last post in this[^] thread.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

public string GetListBoxSelStringInComma(ListBox Listbox1)
{
string selectedItem = "";
if (Listbox1.Items.Count > 0)
{
for (int i = 0; i < Listbox1.Items.Count; i++)
{
if (Listbox1.Items[i].Selected)
{
if (selectedItem == "")
selectedItem = Listbox1.Items[i].Text;
else
selectedItem += "," + Listbox1.Items[i].Text;
}
}
}
return selectedItem;
}
// This Function Will Return all the selected values of Listbox in a string
 seperated by comma
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

for (int i = 0; i < chkgrouplist.Items.Count; i++)
        {
            if (chkgrouplist.Items[i].Selected)
            {
                query =chkgrouplist.Items[i].Value ;
               
            }
        }
  Permalink  
v2
Comments
Anuja Pawar Indore at 27-Jan-12 9:31am
   
Added pre tag
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

string q = "select ReqItemNo, BalanceOnStock from Product where ProHead ='" + DDLItem.SelectedItem.Text + "'";
        cm = new SqlCommand(q, cn);
        SqlDataReader r = cm.ExecuteReader();
        while (r.Read())
        {
            
            LBox.Items.Add(r.GetString(0));
            LBoxShow.Items.Add(r[1].ToString());
            foreach (ListItem lboxItem in LBox.Items)
            {
               abc = Convert.ToInt32(lboxItem);
                foreach (ListItem lShowItem in LBoxShow.Items)
                {
 
                    dal = Convert.ToInt32(lShowItem);
                }
                showdata = (Convert.ToInt32(dal) - Convert.ToInt32(abc) * Convert.ToInt32(TxtWorkSelection.Text));
                              
            }
            
        }
this is not working please give me help.....
  Permalink  
v2
Comments
Mohammed Rayhaan at 14-Jun-12 7:36am
   
Hi,
i want to save the id of multiple selected items in database as i am taking the items from a different table and binding it to listbox.
thanks,
please reply soon
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 12

protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            int _count = lstEmployees.Items.Count;
            if (_count != 0)
            {
                for (int i = 0; i < _count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = lstEmployees.Items[i].Text;
                    item.Value = lstEmployees.Items[i].Value;
                    lstSelectedEmployees.Items.Add(item);
                }
            }
            lstEmployees.Items.Clear();
        }
 
Happy Coding..
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 495
1 Maciej Los 258
2 OriginalGriff 229
3 BillWoodruff 170
4 Zoltán Zörgő 165
0 OriginalGriff 7,934
1 DamithSL 6,084
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,174
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100