Reputation: 1
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.
Upvotes: 0
Views: 75
Reputation: 671
Your insert statements are missing the closing bracket for the values.
Add a using Statement for the SQlConnection and SQLCommand, will make it easier to read and debug.
using (SqlConnection con = new SqlConnection(@"Data Source=MYDATASOURCE"))
{
con.Open();
using(SqlCommand cmd = new SqlCommand(
"Insert into [Voorraad] values(@IngredientID, @AantalInVoorraad, @MinimumVoorraad)", con))
{
cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
cmd.Parameters.AddWithValue("@AantalInVoorraad", txt_aantal.Text);
cmd.Parameters.AddWithValue("@MinimumVoorraad", txt_minimum.Text);
cmd.ExecuteNonQuery();
}
using(SqlCommand cmd = new SqlCommand(
"insert into [Ingredient] values(@IngredientID, @IngredientNaam)", con))
{
cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
cmd.Parameters.AddWithValue("@IngredientNaam", txt_ingredient.Text);
cmd.ExecuteNonQuery();
}
}
Upvotes: 0
Reputation: 174
Introducing ASP.NET Web Pages - Entering Database Data by Using Forms
cmd.CommandText = "Insert into [Voorraad] (IngredientID, AantalInVoorraad, MinimumVoorraad) values(@IngredientID, @AantalInVoorraad, @MinimumVoorraad)";
and
cmd.CommandText = "insert into [Ingredient] (IngredientID, IngredientNaam) values(@IngredientID, @IngredientNaam)";
Upvotes: 2