1
 con.Open();
        cmd = new OleDbCommand("insert into login (user, password) values ('" +textBox1 .Text + "','" + textBox2 .Text + "');",con);
        cmd.CommandType = CommandType.Text;
        int temp = cmd.ExecuteNonQuery();
        if (temp > 0)
        {
           textBox1.Text = null;
            textBox2.Text = null;
            MessageBox.Show("Record Successfuly Added");
        }
        else
        {
            MessageBox.Show("Record Fail to Added");
        }
        con.Close();

when i try to insert some of error appear ( syntax error in INSERT STATEMENT ) i'm try different method to values like Parameters or direct plz !

4
  • 1
    I think it is because of the key words like Login , user and password, trying putting square brackets [] around them and execute the same query again. Commented Jul 1, 2015 at 23:47
  • As @M.Ali has noted, User is a reserved word Commented Jul 1, 2015 at 23:50
  • Side Notes: 1) Use textBox1.Text = string.Empty instead of textBox1.Text = null. 2) You can close the connection right after query execution ( int temp = cmd.ExecuteNonQuery(); con.Close(); ). Commented Jul 1, 2015 at 23:52
  • its work now without any error, but when i open the database file i'm doesn't see any info. Commented Jul 2, 2015 at 0:26

1 Answer 1

2
  • Escape reserved keyword user
  • use parameterized query
  • avoid sql injection
  • Make use of disposable objects

Try this approach:

using (OleDbConnection con = new OleDbConnection(connectionString))
{
    con.Open();
    using (var cmd = new SqlCommand(
    "insert into login ([user], [password]) values (@user, @pass);",
    con))
    {
        cmd.Parameters.Add(new OleDbParameter("@user", textBox1.Text ));
        cmd.Parameters.Add(new OleDbParameter("@pass", textBox1.Text ));

        if (temp > 0)
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            MessageBox.Show("Record Successfuly Added");
        }
        else
        {
            MessageBox.Show("Record Fail to Added");
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

how can i do? i'm new user to this site

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.