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...
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sunny SharmaPosted Jul 4, 2013, 3:02 AM
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!