SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=MYDATASOURCE";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into [Voorraad] values(@IngredientID,
@AantalInVoorraad, @MinimumVoorraad";
cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
cmd.Parameters.AddWithValue("@AantalInVoorraad", txt_aantal.Text);
cmd.Parameters.AddWithValue("@MinimumVoorraad", txt_minimum.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.CommandText = "insert into [Ingredient] values(@IngredientID, @IngredientNaam";
cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
cmd.Parameters.AddWithValue("@IngredientNaam", txt_ingredient.Text);
cmd.ExecuteNonQuery();
I want to insert data to the tables Voorraad and Ingredient. In the tables Voorraad there must IngredientID, AantalInVoorraad, MinimumVoorraad and Categorie be in the table after instert.
In the table Ingredient there must be an new Ingredientnaam be made. When i filling in the text boxes and after hitting the button insert i get the error: System.Data.SqlClient.SqlException: 'Incorrect syntax near '@MinimumVoorraad'.' Please help me!
I've edited to this:
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into [Voorraad] values(@IngredientID,
@AantalInVoorraad, @MinimumVoorraad)";
cmd.Parameters.AddWithValue("@IngredientID", txt_ID.ID);
cmd.Parameters.AddWithValue("@AantalInVoorraad", txt_aantal.Text);
cmd.Parameters.AddWithValue("@MinimumVoorraad", txt_minimum.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.CommandText = "insert into [Ingredient] values(@IngredientID,
@IngredientNaam)";
cmd.Parameters.AddWithValue("@IngredientID", txt_ID.ID);
cmd.Parameters.AddWithValue("@IngredientNaam", txt_ingredient.Text);
cmd.ExecuteNonQuery();
Does anybody know maybe another way to insert data to multiple tables in the datbase?? I've searched the whole internet for an answer but i can't find the right solution.