Scott G

Scott G

  • NA
  • 103
  • 7.1k

Insert listbox contents to database

Sep 11 2018 8:53 AM
I am creating an application with several textboxes and a few listboxes. The first listbox is getting a list of games from a SQL table which contains ID, Name and Type columns. The listbox is display and value for the listbox is the Name. When clicking a button I can move the name from the first listbox to the second. Now I want to be able to write the ID of the items in the second listbox to another SQL table when saving.
 
The below code executes fine and inserts to the promo table. But when I try either adding a second insert statement (INSERT INTO promo_games(promo_ID, Game_ID) VALUES(@PID, @GameID)) to newPromo or creating a new string with a separate SQL insert for the listbox is when I run into problems. I am just not sure how to handle this to insert the multiple items and to take the ID from the item.
 
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["myConnStr"].ConnectionString;
SqlConnection misc = new SqlConnection(conn);
string newPromo = "INSERT INTO admin.promo (ID, masterId, Master, name, ...more text fields)"
"VALUES(@ID, @MasterID,@Master, @Name,...more text fields)"; 
SqlCommand insPromo = new SqlCommand(newPromo, misc);
insPromo.Parameters.AddWithValue("@ID", txtID.Text);
insPromo.Parameters.AddWithValue("@MasterID", txtMasterID.Text);
insPromo.Parameters.AddWithValue("@Master", chkMaster.Checked);
insPromo.Parameters.AddWithValue("@Name", txtName.Text);
...
...
misc.Open();
insPromo.ExecuteNonQuery();
misc.Close();
 
 
 

Answers (1)