how can i pass mu multiple selecteditems from listbox to database...
i convert it to string however , the currently or last selected item is the only values being pass to the database..

string list = listbox1.SelectedItems.ToString();

Recommended Answers

To get items that are selected (the same as all of them), you have to do a loop, and go through them one by one, and do insertion into database.
Example:

SqlConnection conn = new SqlConnection("connString");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = @"INSERT INTO MyTable VALUES …
Jump to Post

All 2 Replies

To get items that are selected (the same as all of them), you have to do a loop, and go through them one by one, and do insertion into database.
Example:

SqlConnection conn = new SqlConnection("connString");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = @"INSERT INTO MyTable VALUES (@param1)";
cmd.Parameters.Add("@param1", SqlDbType.VarChar, 50);
for(int i = 0; i < listBox1.SelectedItems.Count; i++)
{
    string item = listBox1.SelectedItems[i].ToString():
    cmd.Parametes["@param1"].Value = item;
    cmd.ExecuteNonQuery();  //each loop a new row will be inseted into DB.
}
cmd.Dispose();
conn.Dispose();

so there's no way to put all the list in one row? because i want it to be on one row...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.