-2

I want to insert my DataGridView datas to my database i used parametrs

these are my database fileds

    CREATE TABLE [dbo].[Tbl_Order] (
    [Name]     VARCHAR (50) NULL,
    [Price]    INT          NULL,
    [Quantity] INT          NULL,
    [Total]    INT          NULL
);

and these are my codes

SqlConnection con = new SqlConnection("Data Source=DESKTOP-R34C6VV\\SQL;Initial Catalog=Student_Databse_2019_HW1;Integrated Security=True");

     con.Open();
     for (int i = 0; i < dataGridView1.Rows.Count - 1; i++ )
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO Tbl_Order(Name, Price, Quantity, Total) values(@Name, @Price, @Qty, @Total",con);
                cmd.Parameters.AddWithValue("@Name", dataGridView1.Rows[i].Cells[0].Value);
                cmd.Parameters.AddWithValue("@Price", dataGridView1.Rows[i].Cells[1].Value);
                cmd.Parameters.AddWithValue("@Qty", dataGridView1.Rows[i].Cells[2].Value);
                cmd.Parameters.AddWithValue("@Total", dataGridView1.Rows[i].Cells[3].Value);
                cmd.ExecuteNonQuery();
            }
    con.Close();
    dataGridView1.Rows.Clear();

I have an error on this code cmd.ExecuteNonQuery();

it says An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Incorrect syntax near '@Total'.

I have 4 fields in my DataGridView
Name , Price , Qty , Total

any one know what is the problem?

2 Answers2

3

You have not closed the round bracket in your SQL statement.

Azhar Khorasany
  • 2,712
  • 16
  • 20
0

From your code it looks like you're missing a ")" in your Insert statement.

QuestionGuyBob
  • 303
  • 1
  • 10