I need to Insert all the selected items from the listbox to a database(sql server) .can anyone help me to insert it into database...
Insert all the selected items from the listbox to a database
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Sunny SharmaRank #223 · 11.1K reputationPosted Jul 3, 2013, 11:02 PM EDT
Follow the pattern below that I've for you to do so:
---------------------------------------------------
Listbox1.SelectedItems gives you all the selected items in a list box.
--------------------------
private void button3_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(myConnectionString);
conn.Open();
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{ var selectedValue = listBox1.SelectedItems[i].ToString(); //you get the selected value here. SqlCommand cmd = new SqlCommand("insert into myTable values('" + selectedValue + "';", conn); cmd.ExecuteNonQuery();
}
conn.Close();
}
---------------------------------------------------
Hope you got the point.
Happy Coding :)
Mark it as answer if it helps.
Cheers!