Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey! I keep having problems with saving a list of items from my listbox to the database. Please show me how i would save all items to a table by clicking the save button once.
Posted

Add all these records in datatable then insert datatable in database

Hope this link will solve your problem.
http://msdn.microsoft.com/en-us/library/33y2221y%28v=vs.71%29.aspx[^]
 
Share this answer
 
Comments
Member 10268615 12-Sep-13 3:10am    
hi..how can i get listbox items from sqlserver database ..?
Try this:

C#
public string GetListItems()
{
    System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>();

    foreach (ListItem item in userList.Items)
    {
        if (item.Selected)
        {
            items.Add(item.Text);
        }
    }

    return String.Join(", ", items.ToArray());
}



hope it helps :)
 
Share this answer
 
suppose you have items in a list box.
Add save button and in the click event as follows:


C#
for (int i = 0; i < ListBox1.Items.Count; i++)
{
ListItem item= new ListItem();
item.Text = ListBox1.Items[i].Text;
item.Value = ListBox1.Items[i].Value;
SqlCommand cmd=new SqlCommand("insert into tablename(listcolmText,listcolmValue) values('"+item.Text +"',"+ item.Value +")",conn);
cmd.ExecuteNonQuery();

}
 
Share this answer
 
v2
Comments
Member 12294338 2-Mar-16 17:22pm    
ListItem item= new ListItem();
in this sentence i cant declear the ListItem still tell me that ther's no ListItem class
C#
protected void btnSave_Click(object sender, EventArgs e)
        {
            string value="";

            for (int i = 0; i < listbox1.Items.Count; i++)
            {               
                if(value != "")
                {
                  value += ",";
                }
                value += listbox1.Items[i].Text;
            }           
// Now you have all the values in comma (,) separated string.

          string[] arr = value.split(',');

         }

Now you can save these value in database one by one.
 
Share this answer
 
v2
Comments
Member 13373701 4-Oct-17 8:13am    
how to implementation in asp.net
Member 13373701 4-Oct-17 8:13am    
if field one more ?
You can create an object array of your selected items.
Then use bulk insert to put it into table.
Sample:
Bulk INSERT / UPDATE / DELETE in LINQ to SQL[^]
 
Share this answer
 
v2

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