0

I'm having trouble with the df.ExecuteNonQuery(); claiming that the insert statement has SQL errors. I'm not sure why the other parts of the program is working with the Insert statement, but the student one just refuses to work.
Database: http://puu.sh/hoTCv/c1ccb77551.png

OleDbCommand df = new OleDbCommand("INSERT into Students(ID,Password,FirstName,LastName,Street,City,State,Zip,EMail,GPA)" + "VALUES (?,?,?,?,?,?,?,?,?,?)", db);
//creating parameters
df.Parameters.AddWithValue("@ID", iDText.Text);
df.Parameters.AddWithValue("@Password", PassText.Text);
df.Parameters.AddWithValue("@FirstName", fnText.Text);
df.Parameters.AddWithValue("@LastName", LnText.Text);
df.Parameters.AddWithValue("@Street", StreetText.Text);
df.Parameters.AddWithValue("@City", CityText.Text);
df.Parameters.AddWithValue("@State", StateText.Text);
df.Parameters.AddWithValue("@Zip", ZipText.Text);
df.Parameters.AddWithValue("@EMail", EmailText.Text);
df.Parameters.AddWithValue("@GPA", GPAText.Text);
df.ExecuteNonQuery();
db.Close();
2
  • 4
    Password is a reserved keyword. It must be escape [Password]. Commented Apr 24, 2015 at 6:19
  • What's the error message? Commented Apr 24, 2015 at 6:20

3 Answers 3

1

Password is a reserved keyword in Microsoft OLE DB Provider. You need to use square brackets like [Password]. As a best practice, change your column name to non-reserved word.

Also don't use AddWithValue method. It may generate unexpected results. Use .Add() overloads to specify your OleDbType and your parameter size.

And would be better to use using statement to dispose your OleDbConnection and OleDbCommand automatically instead calling .Close() or .Dispose() methods manually.

Sign up to request clarification or add additional context in comments.

Comments

0

Can this:

INSERT into Students(ID,Password ....

To this:

INSERT into Students(ID,[Password] ....

2 Comments

@Sorry Don't forget to mark it as an answer, if it helped you. This way you will give reputation to person, who helped you
Yes, I can't mark a answer into 9 minutes after its been asked.
0

Password is a reserved keyword. Wrap your password like [Password]

Microsoft SQL Server uses reserved keywords for defining, manipulating, and accessing databases. Reserved keywords are part of the grammar of the Transact-SQL language that is used by SQL Server to parse and understand Transact-SQL statements and batches. Although it is syntactically possible to use SQL Server reserved keywords as identifiers and object names in Transact-SQL scripts, you can do this only by using delimited identifiers.

Comments

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.