We help IT Professionals succeed at work.

C# and MS Access 2007

deoyagya
deoyagya asked
on
HI All,

I am struggling to determine the error in the following lines of the code. When the application try to insert the values in database, this is giving me error message. Incorrect "Insert into syntex.

I have checked almost everything matching DB columns, Order and Syntex etc, but the error is not going away.

Any help will to have this fixed will be appreciated.

Thanks,
deoyagya
public void insertRolesAndRates(string strRole, decimal rate, string strProjectCode, string strCurrency)
    {
        string strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Management\Management.accdb";

        string sSql = "Insert into Admin.RolesAndRates(Role,Rate,ProjectCode,Currency) values(@role,@rate,@projectCode,@currency)";
        using (OleDbConnection myConnection = new OleDbConnection(strConnection))
        {
            myConnection.Open();

            // Initialize the SQL command object.
            using (OleDbCommand myCOmmand = new OleDbCommand(sSql, myConnection))
            {
                myCOmmand.Parameters.Add("@role", OleDbType.VarChar).Value = strRole;
                myCOmmand.Parameters.Add("@rate", OleDbType.Decimal).Value = rate;
                myCOmmand.Parameters.Add("@projectCode", OleDbType.VarChar).Value = strProjectCode;
                myCOmmand.Parameters.Add("@currency", OleDbType.VarChar).Value = strCurrency;

                //Execute the commend to insert the data in the database.
                myCOmmand.ExecuteNonQuery();
            }
        }
    }

Open in new window

Comment
Watch Question

Author

Commented:
I tried with removing the qusalifier Admin, but this still doesn't work.
looks like some field names are reserved words (like Currency), can you try the following?

string sSql = "Insert into Admin.RolesAndRates(Role,Rate,ProjectCode,[Currency]) values(@role,@rate,@projectCode,@currency)";
also try this

string sSql = "Insert into Admin.RolesAndRates(Role,Rate,ProjectCode,[Currency]) values(?,?,?,?)";
public void insertRolesAndRates(string strRole, decimal rate, string strProjectCode, string strCurrency)
    {
        string strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Management\Management.accdb";

        string sSql = "Insert into Admin.RolesAndRates(Role,Rate,ProjectCode,[Currency]) values(?,?,?,?)";
        using (OleDbConnection myConnection = new OleDbConnection(strConnection))
        {
            myConnection.Open();

            // Initialize the SQL command object.
            using (OleDbCommand myCOmmand = new OleDbCommand(sSql, myConnection))
            {
                myCOmmand.Parameters.Add("@role", OleDbType.VarChar).Value = strRole;
                myCOmmand.Parameters.Add("@rate", OleDbType.Decimal).Value = rate;
                myCOmmand.Parameters.Add("@projectCode", OleDbType.VarChar).Value = strProjectCode;
                myCOmmand.Parameters.Add("@currency", OleDbType.VarChar).Value = strCurrency;

                //Execute the commend to insert the data in the database.
                myCOmmand.ExecuteNonQuery();
            }
        }
    }

Open in new window

Author

Commented:
Excellent.