Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having some trouble with an insert statement I am using to insert strings into an access database and I keep receiving a "Syntax error in INSERT INTO statement."

C#
public void Insert(Person p)
        {
            try
            {
                command.CommandText = "INSERT INTO NewUser(Position, Forename, Surname, [Telephone Number], Username, [Password], [Confirm Password], [Start Date]) VALUES('" + p.PositionPositionCmbx1+ "', '" + p.FirstName_txt1+ "', '" + p.Surname_txt1+ "', '" + p.Telnumber_txt+ "', '" + p.PasswordNU_txt1+ "', '" + p.ConfirmPasswrd_txt1+ "')";
                command.CommandType = System.Data.CommandType.Text;
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }


What I have tried:

Putting [] around all values in the insert statement
Posted
Updated 13-Nov-17 4:56am
v2

1 solution

You are passing less number of values than the mentioned numbers of columns. Your INSERT query expects 8 values but you are passing only 6 values. First correct that.

Secondly, put your query in a string variable and see what is the actual query being evaluated to. Something like following-
C#
string query ="INSERT INTO NewUser(Position, Forename, Surname, [Telephone Number], Username, [Password], [Confirm Password], [Start Date]) VALUES('" + p.PositionPositionCmbx1+ "', '" + p.FirstName_txt1+ "', '" + p.Surname_txt1+ "', '" + p.Telnumber_txt+ "', '" + p.PasswordNU_txt1+ "', '" + p.ConfirmPasswrd_txt1+ "')";


UPDATE
The above is just to keep things simple and used your code only. But your code is seriously prone to SQL Injection[^]. And the best thing is you don't need to reinvent the wheel rather just some minor changes can do the job. Please check following links for further help. I strongly recommend to check and let me know if I can help in implementing that.
Using Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

If you still need help, please let me know :)
 
Share this answer
 
v2
Comments
OriginalGriff 13-Nov-17 11:28am    
Reason for my vote of one: never encourage anyone to concatenate strings to form and SQL command - please read up on SQL Injection, and then fix all your own apps as a matter of priority.
Suvendu Shekhar Giri 13-Nov-17 11:55am    
Ah! I deserve ONE.
I have been an advocate of Security Measures for SQL Injection and other Top 10 OWASP and have said this several times in my answers in CP but was so silly to miss here.

Many thanks for reminding @OriginalGriff. Will take care of this in future as well. I have updated the answer.
OriginalGriff 13-Nov-17 12:04pm    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900