none
Syntax error in Insert into statement RRS feed

  • Question

  • hello i have this code snippet and i keep getting this error: SYNTAX ERRROR IN INSERT INTO STATEMENT.  Please id really apprecaite some help.

    private

     

    void RegisterUser()

    {

     

    OleDbCommand registerCmd = new OleDbCommand();

    registerCmd.Connection = wcfConn;

    registerCmd.CommandType =

    CommandType.Text;

    registerCmd.CommandText =

     

    "INSERT INTO Users(matNo, fName, lName, sAdd, email, phone1, phone2, unit, title, regDate, username, password)"

    +

    " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

     

     

    //create register command parameters

     

    OleDbParameter matNoParameter, fNameParameter, lNameParameter, sAddParameter;

     

    OleDbParameter emailParameter, phone1Parameter, phone2Parameter, unitParameter;

     

    OleDbParameter titleParameter, regDateParameter, usernameParameter, passwordParameter;

    matNoParameter =

    new OleDbParameter("@matNo", OleDbType.Char);

    registerCmd.Parameters.Add(matNoParameter);

    matNoParameter.Value = user.MatNo;

    fNameParameter =

    new OleDbParameter("@fName", OleDbType.Char);

    registerCmd.Parameters.Add(fNameParameter);

    fNameParameter.Value = user.FirstName;

    lNameParameter =

    new OleDbParameter("@lName", OleDbType.Char);

    registerCmd.Parameters.Add(lNameParameter);

    lNameParameter.Value = user.LastName;

    sAddParameter =

    new OleDbParameter("@sAdd", OleDbType.Char);

    registerCmd.Parameters.Add(sAddParameter);

    sAddParameter.Value = user.SchoolAddress;

    emailParameter =

    new OleDbParameter("@email", OleDbType.Char);

    registerCmd.Parameters.Add(emailParameter);

    emailParameter.Value = user.Email;

    phone1Parameter =

    new OleDbParameter("@phone1", OleDbType.Char);

    registerCmd.Parameters.Add(phone1Parameter);

    phone1Parameter.Value = user.Phone1;

    phone2Parameter =

    new OleDbParameter("@phone2", OleDbType.Char);

    registerCmd.Parameters.Add(phone2Parameter);

    phone2Parameter.Value = user.Phone2;

    unitParameter =

    new OleDbParameter("@unit", OleDbType.Char);

    registerCmd.Parameters.Add(unitParameter);

    unitParameter.Value = user.Unit;

    titleParameter =

    new OleDbParameter("@title", OleDbType.Char);

    registerCmd.Parameters.Add(titleParameter);

    titleParameter.Value = user.Title;

    regDateParameter =

    new OleDbParameter("@date", OleDbType.Char);

    registerCmd.Parameters.Add(regDateParameter);

    regDateParameter.Value = user.RegDate;

    usernameParameter =

    new OleDbParameter("@username", OleDbType.Char);

    registerCmd.Parameters.Add(usernameParameter);

    usernameParameter.Value = user.UserName;

    passwordParameter =

    new OleDbParameter("@password", OleDbType.Char);

    registerCmd.Parameters.Add(passwordParameter);

    passwordParameter.Value = user.Password;

     

     

    //add parameters to registerCmd

     

     

     

     

     

     

     

     

    try

    {

    registerCmd.Connection.Open();

    registerCmd.ExecuteNonQuery();

    registerCmd.Connection.Close();

     

    MessageBox.Show("Congatulations, Your registration is successful", "Success",

     

    MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

     

     

    catch (OleDbException ex)

    {

     

    MessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

     

    catch (Exception ex)

    {

     

    MessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

     

    finally

    {

     

    registerCmd.Connection.Close();

    }

     

    }

    Thursday, January 20, 2011 10:33 PM

Answers

  • Hello toBbie,

     

    Welcome to the MSDN Forum and thank you for posting here.

    According to your description about your issue, I think we should check the type of parameters. Expecially the phone number and data time. Maybe this issue is because the different types between storing in database and in the code you set.  If so, we can modify the type either sides to make sure they are same.

    I hope it can help you. If you have any other questions please let us know.

     

    Have anice day,

     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 24, 2011 7:32 AM
    Moderator
  • The keyword "Password" is a reserved word if you are using a Microsoft Access database. You can enclose it in brackets to work around this issue but I would suggest changing the name altogether.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, January 25, 2011 2:20 PM

All replies

  • Hello toBbie,

     

    Welcome to the MSDN Forum and thank you for posting here.

    According to your description about your issue, I think we should check the type of parameters. Expecially the phone number and data time. Maybe this issue is because the different types between storing in database and in the code you set.  If so, we can modify the type either sides to make sure they are same.

    I hope it can help you. If you have any other questions please let us know.

     

    Have anice day,

     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 24, 2011 7:32 AM
    Moderator
  • The keyword "Password" is a reserved word if you are using a Microsoft Access database. You can enclose it in brackets to work around this issue but I would suggest changing the name altogether.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, January 25, 2011 2:20 PM