Alasse
Alasse

Reputation: 361

ASP.NET microsoft access insert db error

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.

Upvotes: 1

Views: 121

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98868

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.

Upvotes: 3

Related Questions