missing operator exception oledb

MarkH12518
MarkH12518 used Ask the Experts™
on
I am trying to insert into msAccess using ole/c# and am getting a missing operator exception on the first string variable I'm trying to pass.

{"Syntax error (missing operator) in query expression 'Cpty A'."}

The first field is an int, the next 3 fields are doubles. The remaining fields are all strings/varchar. The exception is thrown when I try to pass Cpty A as a value to the insert statement before executing.

Can someone help me on this?

Thanks

try
                {
                    simTmp[i] = expAgg + sExp[i];
                    StringBuilder insertSQL = new StringBuilder(@"INSERT INTO " + tabnm + " (");
                    insertSQL.Append("  [SimNum], ");
                    insertSQL.Append("  [Leg0_MTM], ");
                    insertSQL.Append(" [Leg1_MTM], ");
                    insertSQL.Append("  [SetExp], ");
                    insertSQL.Append(" [CPTY], ");
                    insertSQL.Append(" [KAES_ENTITY], ");
                    insertSQL.Append(" [RPT_LEVEL_1], ");
                    insertSQL.Append(" [RPT_LEVEL_2], ");
                    insertSQL.Append(" [RPT_LEVEL_3]) ");
                    insertSQL.Append(" VALUES (");
                    insertSQL.Append(i + 1).Append(",");
                    insertSQL.Append(leg0exp).Append(",");
                    insertSQL.Append(leg1exp).Append(",");
                    insertSQL.Append(sExp[i]).Append(",");
                    insertSQL.Append(cpty).Append(",");
                    insertSQL.Append(KAESEnt).Append(",");
                    insertSQL.Append(RL1).Append(",");
                    insertSQL.Append(RL2).Append(",");
                    insertSQL.Append(RL3).Append(")");

                    OleDbCommand cmd = new OleDbCommand();
                    OleDbDataAdapter oledbAdapter = new OleDbDataAdapter();

                    cmd.Connection = connect;
                    cmd.CommandType = CommandType.Text;
                   
                    cmd.CommandText = insertSQL.ToString();
                    insSql = insertSQL.ToString();
                    oledbAdapter.InsertCommand = new OleDbCommand(insSql, connect);
                    oledbAdapter.InsertCommand.ExecuteNonQuery();

                    //cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    string exc = e.ToString();
                }
                
            }
            connect.Close();

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Consultant
Top Expert 2016
Commented:
if you have string values, you need to surround them by quotes:

                   insertSQL.Append(sExp[i]).Append(", '");
                    insertSQL.Append(cpty).Append("',");

Open in new window

Author

Commented:
Thanks very much for the quick reply. It works.

Author

Commented:
Again, thanks for quick response. Works fine now.

Mark

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial