Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,
I am using access Database to insert values, following is the code:
C#
public Int32 InsertUser(string firstnm, string lastnm, string unm, string pwd, string add, string email, string contact)
    {
        int i = 0;
        OleDbConnection con = GetConnection();
        OleDbCommand cmd = new OleDbCommand("insert into usermaster(FullName,UserName,Password,Address,EmailID,ContactNo) values('" + firstnm + " " + lastnm + "','" + unm + "','" + pwd + "','" + add + "','" + email + "','" + contact + "')", con);
        con.Open();
        i = cmd.ExecuteNonQuery();
        con.Close();
        return i;
    }


Now when I am running it with VS it gives me Error "SYNTAX ERROR IN INSERT INTO STATEMENT" but when I use the same command and run it through Access query wizard it gets executed.

So where is the problem. Any help.
Posted

Just to add to what Mehdi says:
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. The chances are that will cure the problem you have spotted.
There are two other problems here:
1) You should Dispose OleDb objects when you are finished with them:
C#
using (OleDbConnection con = GetConnection())
    {
    using (OleDbCommand cmd = new OleDbCommand("INSERT INTO usermaster (FullName,UserName,Password,Address,EmailID,ContactNo) VALUES (@NM, @UN, @PW, @AD, @EM, @CN)", con))
       {
       cmd.Parameters.AddWithValue("@NM", firstnm + " " + lastnm);
       cmd.Parameters.AddWithValue("@UN", unm);
       cmd.Parameters.AddWithValue("@PW", pwd);
       cmd.Parameters.AddWithValue("@AD", add);
       cmd.Parameters.AddWithValue("@EM", email);
       cmd.Parameters.AddWithValue("@CN", contact);
       con.Open();
       return (cmd.ExecuteNonQuery());
       }
   }

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
 
Share this answer
 
v2
Comments
Mitul Birla 20-Dec-15 3:21am    
Thanks for the reply OriginalGriff, but I am still getting the same error.
But very strange thing noticed when I do Select, Update or Delete it works fine, Error is only for Insert statement.
OriginalGriff 20-Dec-15 3:49am    
Try "escaping" the column names:
...INTO usermaster (FullName,[UserName],[Password],[Address],[EmailID],ContactNo) VALUES...

But you do need to do all the other things as well - SQL Injection is not funny when it happens to you, and password security is very important!
Mitul Birla 20-Dec-15 4:04am    
Thanks a lot OriginalGriff, escaping column names solved it.
Yeah other things are important, working on now password encryption.
Thanks :)
OriginalGriff 20-Dec-15 4:09am    
You're welcome!
Gustav Brock 20-Dec-15 4:20am    
The only reason for your trouble was "Password" which is a reserved word, thus needs brackets, [Password] which, in general, are not needed.
Use parameter queries instead of string concatenation : http://www.mikesdotnetting.com/article/26/parameter-queries-in-asp-net-with-ms-access[^]
 
Share this answer
 

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