none
Very strange SQL INSERT command error RRS feed

  • Question

  •  

    "INSERT INTO tblEquip(buydate,Country,feature,pc,Provider,Serial,Status,Warranty,Year,Typeid ) VALUES ('12/12/2008','Viet Nam','Supper Power ',0,'IBM','ABCDEF','Out of warranty','12/12/2008',1965,5);"

     

    I use Visual 2005 IDE and code in C# , with an MS Access 2003 Databse. 

    Above SQL command is gotten from Debug.

    It works well when i paste into a query in MS Access 2003.

    But in my project, it return an error : Syntax error in INSERT INTO statement  when I use a try catch statement.

     

    Here my related code :

     

    public void New(DateTime buydate, string Country, string feature, Decimal pc, string Provider, string Serial, string Status, DateTime Warranty, Decimal year,Decimal typeId)
    {
    string strCmd = "INSERT INTO tblEquip(buydate,Country,feature,pc,Provider,Serial,Status,Warranty,Year,Typeid ) VALUES ('" + "12/12/2008" + "','" + Country + "','" + feature + "'," + pc + ",'" + Provider + "','" + Serial + "','" + Status + "','" + "12/12/2008" + "'," + year +","+typeId+");";
    Execute(strCmd);
    }

    //----------------------------------------------------------------------

    private void Execute( string strCmd)
    {
    OleDbConnection conn = new OleDbConnection(strConn);
    OleDbCommand cmd = new OleDbCommand(strCmd, conn);
    try
    {
    conn.Open();
    cmd.ExecuteNonQuery();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    conn.Close();
    }

    }

     

    Where is my wrong ? Please help me.Thanks!


    Monday, June 4, 2007 8:17 AM

Answers

  • You could get this error in a case if some of the field names or table name is a reserved word. I believe in your case it is word Year. In this case you need to put square brackets around those names, like

     

    INSERT INTO tblEquip(buydate,Country,feature,pc,Provider,Serial,Status,Warranty,Year,Typeid ) VALUES ('12/12/2008','Viet Nam','Supper Power ',0,'IBM','ABCDEF','Out of warranty','12/12/2008',1965,5);"

     

     

    You also do not need to use semicolon at the end of the statement
    Monday, June 4, 2007 10:48 AM
    Moderator

All replies

  • You could get this error in a case if some of the field names or table name is a reserved word. I believe in your case it is word Year. In this case you need to put square brackets around those names, like

     

    INSERT INTO tblEquip(buydate,Country,feature,pc,Provider,Serial,Status,Warranty,Year,Typeid ) VALUES ('12/12/2008','Viet Nam','Supper Power ',0,'IBM','ABCDEF','Out of warranty','12/12/2008',1965,5);"

     

     

    You also do not need to use semicolon at the end of the statement
    Monday, June 4, 2007 10:48 AM
    Moderator
  •  

     

    Thanks, Vmazur !

     

    Idont know  that Year  is a keyword.

    With your help, my problem is fixed!

     

    Good luck , VMazur !

    Tuesday, June 5, 2007 1:06 AM