1

I am recieving syntax error in insert into statement. I am using microsoft access database. here is code sample:

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("measurmentdb.mdb"));
        OleDbCommand cmd = new OleDbCommand("insert into tbl_Users(Username,Name,Surname,Password,Email,Phonenumber,CV)values(@un,@name,@sna,@pw,@em,@pn,@cv)", con);
        cmd.Parameters.AddWithValue("@un", txtusername.Text); 
        cmd.Parameters.AddWithValue("@name", txtname.Text);
        cmd.Parameters.AddWithValue("@sna", txtsurname.Text);
        cmd.Parameters.AddWithValue("@pw", txtpassword.Text);
        cmd.Parameters.AddWithValue("@em", txtemail.Text);
        cmd.Parameters.AddWithValue("@pn", txtphone.Text);
        cmd.Parameters.AddWithValue("@cv", txtcv.Text);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception ex)
    {

        throw ex;
    }
}

Everything in database is in text format:

tbl_Users:
Username
Name
Surname
Password
Email
Phonenumber
CV

I could not figure out what is wrong. I would appreciate your helps.

0

1 Answer 1

3

NAME and PASSWORD are reserved words on MS Access.

Use them with square brackets like [NAME] and [PASSWORD]

OleDbCommand cmd = new OleDbCommand("insert into tbl_Users(Username,[Name],Surname,[Password],Email,Phonenumber,CV)values(@un,@name,@sna,@pw,@em,@pn,@cv)", con);

If a reserved word is already in use, you can avoid error messages by surrounding each occurrence of the word with brackets ([ ]). However, the best solution is to change the name to a nonreserved word.

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.