Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Problem:
I have a WPF Application that has a button that when clicked will create a username and password and insert it into my database. The database was created in Microsoft Access 2007. The problem is when I compile and run it I get an error that says "Syntax Error in INSERT INTO command", or something to that effect. Point being, I have a problem that is in desperate need of an answer.
Info:
OS: Windows 7 Ultimate - x64
Compiler: Visual Studio 2010 Ultimate
Language: C# 4.0 WPF
Database Application: Microsoft Access 2007

I'm using SQL, not MySQL

My Code:
C#
if (strDesiredPassword.Text == strDesiredPassword2.Text && String.IsNullOrWhiteSpace(strDesiredUser.Text) == false)
            {
                string insertCMD = "INSERT INTO LoginTable (User,Pass) VALUES (@User,@Pass)";
                string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\MedDesk\Login.accdb;";
                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("@User", OleDbType.Char).Value = strDesiredUser.Text;
                        Cmd.Parameters.Add("@Pass", OleDbType.Char).Value = strDesiredPassword2.Text;
                        Cmd.ExecuteNonQuery();
                        MyConn.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

            }
Posted

1 solution

I suspect that User is a reserved word in Access - so you can't use it as is, you need to surround it with square brackets

string insertCMD = "INSERT INTO LoginTable ([User],Pass) VALUES (@User,@Pass)";


will probably do it
 
Share this answer
 
Comments
shelby67 14-Feb-12 22:50pm    
I will try that in a moment, I really appreciate your reply!
elrro 8-Jul-13 0:32am    
correct

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