Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
can anyone suggest the code to store multiple selected values from listbox to database in asp.net c# ?
Posted
Comments
Karthik. A 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 17-Oct-10 10:37am    
Your requirement is not clear.

Or something like this:

C#
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:

C#
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?
 
Share this answer
 
v2
Comments
Joezer BH 9-Sep-13 8:11am    
5ed!
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
                }
            }
        }
 
Share this answer
 
Comments
Mohammad Areeb khan 16-Feb-17 1:53am    
It works for me ....thanks!
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.
 
Share this answer
 
v2
Hi,

Try this-

HTML-

XML
<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-

C#
var selected = ListBox1.GetSelectedIndices().ToList();
var selectedValues = (from c in selected
                      select ListBox1.Items[c].Value).ToList();
 
Share this answer
 
C#
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
 
Share this answer
 
v2
See the last post in this[^] thread.
 
Share this answer
 
C#
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.....
 
Share this answer
 
v2
Comments
Mohammed Rehan 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
C#
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..
 
Share this answer
 
C#
for (int i = 0; i < chkgrouplist.Items.Count; i++)
        {
            if (chkgrouplist.Items[i].Selected)
            {
                query =chkgrouplist.Items[i].Value ;
               
            }
        }
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 27-Jan-12 9:31am    
Added pre tag
//window ListBox
for (int i = 0; i < DocsListBox.Items.Count; i++)
{
if (DocsListBox.GetSelected(i))
{
string selectedItem = DocsListBox.GetItemText(DocsListBox.Items[i]).ToString();
//insert command
}
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900