-1

I'm having trouble with my code, I am getting a Invalid expression term ')' not sure what I'm doing wrong. Here is my code.

protected void btnSubmit_Click(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + Server.MapPath("WSC_DB.mdb") + "'; Persist Security Info=False");

    using (OleDbCommand cmd = new OleDbCommand("insert into Users(UserFirstName, UserLastName, ShipAddress, ShipCity, ShipState, UserPhone, UserEmail, UserName, UserPassword, LoginType) values (@FirstName, @LastName, @Address, @City, @State, @Zip, @Phone, @Email, @Username, @Password, @Logintype)", conn))
    {
        cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
        cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
        cmd.Parameters.AddWithValue("@City", txtCity.Text);
        cmd.Parameters.AddWithValue("@State", DropDownList1.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
        cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
        cmd.Parameters.AddWithValue("@Logintype", "U");
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}
TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
  • have you tried adding a space between Users and (UserFirstName ? – DarkSquirrel42 Feb 16 '14 at 23:41
  • the error shows on the first statement OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + Server.MapPath("WSC_DB.mdb") + "'; Persist Security Info=False"); – user2526337 Feb 16 '14 at 23:43

2 Answers2

1

You have 10 items in your columns list and 11 items in your values list. The values list includes

... @City, @State, @Zip, @Phone, @Email, ...

but there is no Zip in the columns list

... ShipCity, ShipState, UserPhone, UserEmail, ...
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
0

I seem to remember to that you cannot use named parameters in SQL statement when using Jet OLEDB. You will need to replace @FirstName, @LastName, ... with a question mark '?'.

For better analysis of your error please include the entire error message and stack trace.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • 1
    Actually, OleDb parameters can be assigned names, but the names are ignored and the order of the parameters is all that matters. That's why the question mark `?` placeholder is preferable (in my opinion). – Gord Thompson Feb 17 '14 at 09:19