1

I get the error Syntax error in INSERT INTO statement in following line command.ExecuteNonQuery();

BTW, I already red some similar posts and I tried to fix my problem but I could not able to do so.

My database is MS Access.

Any help will be great!

private void btn_register_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_name.Text) || string.IsNullOrEmpty(txt_family.Text))
            {
                MessageBox.Show("Some information is missed");
            }
            else
            {
                this.Hide();
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText = " insert into Login (FirstName,FamilyName,UserName,PassWord,RepeatPassword,Email,MaxTime,LearningStyle) values ('" + txt_name.Text + "','" + txt_family.Text + "','" + txt_username.Text + "','" + txt_password.Text + "','" + txt_repeat.Text + "','" + txt_mail.Text + "','" + UpDownTime.Value + "','" + UpDownLearningStyle.Value + "')";
                command.ExecuteNonQuery();
                MessageBox.Show("Your Registeration was successfull !");
                connection.Close();
                Login F1 = new Login();
                F1.Show();  

            }

        }
3
  • 2
    Debug and copy the value of CommandText i.e. constructed query and run it in Access directly. Does it work? Did you try that? Commented Oct 21, 2015 at 11:52
  • Also note that, saying I tried to fix my problem but I could not is not sufficient, you need to actually show what you tried! Commented Oct 21, 2015 at 11:53
  • 2
    More important, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks. And use using statement to dispose your connection and command. Commented Oct 21, 2015 at 12:04

1 Answer 1

1

Try this. maybe it could help

Password is a reserved keyword

command.CommandText = " insert into Login (FirstName,FamilyName,UserName,PassWord,RepeatPassword,Email,....

try this

command.CommandText = " insert into Login (FirstName,FamilyName,UserName,[PassWord],RepeatPassword,Email,.....
Sign up to request clarification or add additional context in comments.

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.