What I am trying to do:

Execute DML statements into database (SSCE) using Datagridview and command buttons.

The Problem:

I am getting exact same error as this post: SQL [Error]: There was an error parsing the query. [ Token line number = 1,Token line offset = 44,Token in error = - ]

Based on those answers and others available on the web, I have validated the query string, yet not able to solve it. There's also one other aspect I have doubts.

private void button2_Click(object sender, EventArgs e)
        {
            using (SqlCeConnection CONN = new SqlCeConnection("Data 
                                          Source=LocalDBSSCompactEdition.sdf;"))
            {
                SqlCeCommand comm = new SqlCeCommand();
                comm.Connection = CONN;
                CONN.Open();
                int i = dataGridView2.Rows.Count-1;

                    String queryString = @"INSERT INTO tblEmployee VALUES ("
                    + dataGridView2.Rows[i].Cells["E_ID"].Value + ", "
                    + dataGridView2.Rows[i].Cells["FirstName"].Value + ", "
                    + dataGridView2.Rows[i].Cells["LastName"].Value + ", "
                    + dataGridView2.Rows[i].Cells["DeptID"].Value + ");";
                    comm.CommandText = queryString;
                    comm.ExecuteNonQuery();

            }
        }

1) E_ID column is IDENTITY(auto-increment). However I got an error, saying that I must include all the columns in DataGridview to match to the database table. Could this be the issue that I am getting or could it be my syntax?

2) I want to insert new rows/updates/deleted rows from Datagridview to the database table using a button click event. Is this the efficient way of doing so?

Some insights to the right direction is appreciated.

有帮助吗?

解决方案

I managed to get it solved. I don't believe in answering my own-question. Just added the answer hoping for someone else's reference in the future.

1) i variable was referring to an empty row in the datagridview...Hence the values to be inserted were null and these columns are specified NOT NULL...

2) First rule was to follow the usual INSERT statement when ID column is auto-increment. So I specify the columns that I want to insert data for.

3) The data that I was entering were not quoted to treat as String. Fixed.

private void button2_Click(object sender, EventArgs e)
        {
            using (SqlCeConnection CONN = new SqlCeConnection("Data Source=LocalDBSSCompactEdition.sdf;"))
            {
                CONN.Open();
                SqlCeCommand comm = CONN.CreateCommand();                
                int i = dataGridView2.Rows.Count-1;

                    String queryString = @"INSERT INTO tblEmployee (FirstName, LastName, DeptID) VALUES ('"                    
                    + dataGridView2.Rows[5].Cells["FirstName"].Value + "','"
                    + dataGridView2.Rows[5].Cells["LastName"].Value + "',"
                     + dataGridView2.Rows[5].Cells["DeptID"].Value + ");";
                    comm.CommandText = queryString;
                    comm.ExecuteNonQuery();
                    CONN.Close();    
            }
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top