0
        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.

3
  • 1
    You need to close the bracket at the end of your insert statement. P.S. Make use of a Using statement. See What is the c-sharp using block and why should I use it Commented Jun 5, 2018 at 13:31
  • Both of your inserts are missing a closing parenthesis Commented Jun 5, 2018 at 13:35
  • I've changed this Commented Jun 5, 2018 at 14:01

2 Answers 2

2

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)";
Sign up to request clarification or add additional context in comments.

Comments

0

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();
           }
        }

3 Comments

Thanks for the comment. I tried this but i got the error: Incorrect syntax near '@MinimumVoorraad'.
I've copied your insert statement and forgot to add the missing brackets, try with my new edit.
Ah I see why, the brackets were outside of the double quotes, I have edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.