Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When this code is ran I get this runtime error:
"Syntax error in insert into statement"

C#
void SaveData()
        {//do the real work here
            string insertCMD = "INSERT INTO Users (Username,Password,RegisterEmail) VALUES (@Username,@[Password],@RegisterEmail)";
            string ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\RS_Studio_Login.mdb";
            using (OleDbConnection MyConn = new OleDbConnection(ConnStr))
            {
                try
                {//insertCommand.Parameters.Add("Time", OleDbType.Char).Value = strTime;
                    MyConn.Open();
                    OleDbCommand Cmd = new OleDbCommand(insertCMD, MyConn);
                    Cmd.Parameters.Add("@Username", OleDbType.Char).Value = textEdit1.Text;
                    Cmd.Parameters.Add("@[Password]", OleDbType.Char).Value = textEdit2.Text;
                    Cmd.Parameters.Add("@RegisterEmail", OleDbType.Char).Value = textEdit3.Text;
                    Cmd.ExecuteNonQuery();
                    MyConn.Close();
                    MessageBox.Show("User creation successful!");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }
Posted

According to MSDN article here[^], the OLEDB Provider does not support the Sql Style of Parameters. As it does not support the named parameters, they state as
Quote:
Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.
Change your code to look like the following:

C#
string insertCMD = "INSERT INTO Users (Usern,Passw,RegisterEmail) VALUES (?,?,?)";

and
C#
insertCMD.Parameters.Add(textEdit1.Text);
insertCMD.Parameters.Add(textEdit2.Text);
insertCMD.Parameters.Add(textEdit3.Text);


Does this help?
 
Share this answer
 
v3
Comments
shelby67 4-Jul-12 1:31am    
Even with these changes it fails. No error message at all, nothing to show it failed.
Pankaj Nikam 4-Jul-12 1:38am    
What are you getting in the MessageBox.Show(ex.Message); statement?
shelby67 4-Jul-12 11:51am    
tells me there's an error with the sql command syntax.
I've made the suggested changes to the following:
void SaveData()
{//do the real work here
string insertCMD = "INSERT INTO Users (Usern,[Passw],RegisterEmail) VALUES (@Usern,@Pwd,@RegisterEmail)";
string ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Sagitta\Documents\RS_Studio_User_Login.mdb";
using (OleDbConnection MyConn = new OleDbConnection(ConnStr))
{
try
{//insertCommand.Parameters.Add("Time", OleDbType.Char).Value = strTime;
MyConn.Open();
OleDbCommand Cmd = new OleDbCommand(insertCMD, MyConn);
Cmd.Parameters.Add("@Usern", OleDbType.Char).Value = textEdit1.Text;
Cmd.Parameters.Add("@Pwd", OleDbType.Char).Value = textEdit2.Text;
Cmd.Parameters.Add("@RegisterEmail", OleDbType.Char).Value = textEdit3.Text;
Cmd.ExecuteNonQuery();
MyConn.Close();
MessageBox.Show("User creation successful!");
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}

and used "Passw" and "Usern" instead of "Username and "Password" in the database
Pankaj Nikam 5-Jul-12 4:35am    
Check out the updated answer. Does it solve your problem?
Sandeep Mewara 4-Jul-12 1:40am    
See my answer. It's because of the reserved keyword used.
This problem may occur if your database table contains column names that use Microsoft Jet 4.0 reserved words.
PASSWORD is a KEYWORD here[^] and thus an error.

Change the column name to get away with the error.

Details, refer here: MS Support: You may receive a "Syntax error in INSERT INTO statement" error message [^]
 
Share this answer
 
v2
Comments
Vani Kulkarni 4-Jul-12 1:42am    
+5!
Pankaj Nikam 4-Jul-12 1:44am    
+5 from me too :)
[no name] 4-Jul-12 2:11am    
my 5+
[no name] 5-Jul-12 4:41am    
+5

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