Why am I getting "Incorrect syntax near ')'."

StuBabyAight
StuBabyAight used Ask the Experts™
on
Hello experts, this is my first attempt at writing data to a SQL file with C#, so my problems may lie elsewhere.  And please, this self taught from books and online, so an answer that gives a bit more understanding to where I am going wrong or a pointer to a better online tutorial is most appreciated.  I get the above error when I try to execute the following :
string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Development\Documents\Visual Studio 2008\Projects\MyComicShop\MyComicShop\ShopData.mdf;Integrated Security=True;User Instance=True";
            string insertStr = "insert into tmptblSupplierInvoicePreProcess (@fldSupplierID, @fldSupplierInvoice, @fldQtyShipped, @fldItemCode, @fldItemDiscountCode, @fldItemDescription, @fldSupplierCurrencyRetailPrice, @fldSupplierCurrencyUnitPrice, @fldSupplierCurrencyAmount, @fldCategoryCode, @fldOrderType, @fldProcessedAs, @fldOrderNumber, @fldUPCCode, @fldISBNCode, @fldEANCode, @fldPONumber, @fldAllocatedCode)";


            using (var conn = new SqlConnection(connStr))
            using (var cmd = new SqlCommand(insertStr, conn))
            {
                conn.Open();

                        cmd.Parameters.AddWithValue("@fldSupplierID", supplierID);
                        cmd.Parameters.AddWithValue("@fldSupplierInvoice", supplierInvoice );
                        cmd.Parameters.AddWithValue("@fldQtyShipped", qtyShipped );
                        cmd.Parameters.AddWithValue("@fldItemCode", itemCode );
                        cmd.Parameters.AddWithValue("@fldItemDiscountCode", itemDiscountCode );
                        cmd.Parameters.AddWithValue("@fldItemDescription", itemDescription );
                        cmd.Parameters.AddWithValue("@fldSupplierCurrencyRetailPrice", supplierCurrencyRetailPrice );
                        cmd.Parameters.AddWithValue("@fldSupplierCurrencyUnitPrice", supplierCurrencyUnitPrice );
                        cmd.Parameters.AddWithValue("@fldSupplierCurrencyAmount", supplierCurrencyAmount );
                        cmd.Parameters.AddWithValue("@fldCategoryCode", categoryCode );
                        cmd.Parameters.AddWithValue("@fldOrderType", orderType );
                        cmd.Parameters.AddWithValue("@fldProcessedAs", processedAs );
                        cmd.Parameters.AddWithValue("@fldOrderNumber", orderNumber );
                        cmd.Parameters.AddWithValue("@fldUPCCode", upcCode );
                        cmd.Parameters.AddWithValue("@fldISBNCode", isbnCode );
                        cmd.Parameters.AddWithValue("@fldEANCode", eanCode );
                        cmd.Parameters.AddWithValue("@fldPONumber", poNumber );
                        cmd.Parameters.AddWithValue("@fldAllocatedCode", allocatedCode );

                        if (isValidSize == true)
                        {
                            cmd.ExecuteNonQuery();
                        }



When the error happens, I am able to get the following from Visual Studio, but I have no real idea what most of it means:

System.Data.SqlClient.SqlException was unhandled
  Message="Incorrect syntax near ')'."
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060
  Class=15
  LineNumber=1
  Number=102
  Procedure=""
  Server="\\\\.\\pipe\\20FE6807-3076-45\\tsql\\query"
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at MyComicShop.ImportTextClass.ImportTxtFile() in C:\Users\Development\Documents\Visual Studio 2008\Projects\MyComicShop\MyComicShop\ImportTextClass.cs:line 215
       at MyComicShop.Program.Main() in C:\Users\Development\Documents\Visual Studio 2008\Projects\MyComicShop\MyComicShop\Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
i think you're missing the "VALUES" keyword here:

string insertStr = "insert into tmptblSupplierInvoicePreProcess VALUES (@fldSupplierID, @fldSupplierInvoice, @fldQtyShipped, @fldItemCode, @fldItemDiscountCode, @fldItemDescription, @fldSupplierCurrencyRetailPrice, @fldSupplierCurrencyUnitPrice, @fldSupplierCurrencyAmount, @fldCategoryCode, @fldOrderType, @fldProcessedAs, @fldOrderNumber, @fldUPCCode, @fldISBNCode, @fldEANCode, @fldPONumber, @fldAllocatedCode)";

Hope this helps!

Author

Commented:
Cheers, and stand by, some easy points may show up tonight....

Commented:
this might be a silly question...

shouldn't you specify the fields those values are going to be plugged into?
i.e. insert into blah (field1, field2, field3) values (val1, val2, val3);

Author

Commented:
Probably.  The book I am working from has glossed this part a bit and left me floundering.   This was my original:

            string insertStr = "insert into tmptblSupplierInvoicePreProcess (fldSupplierID, fldSupplierInvoice, fldQtyShipped, fldItemCode, fldItemDiscountCode, fldItemDescription, fldSupplierCurrencyRetailPrice, fldSupplierCurrencyUnitPrice, fldSupplierCurrencyAmount, fldCategoryCode, fldOrderType, fldProcessedAs, fldOrderNumber, fldUPCCode, fldISBNCode, fldEANCode, fldPONumber, fldAllocatedCode) values (@supplierID, @supplierInvoice, @qtyShipped, @itemCode, @itemDiscountCode, @itemDescription, @supplierCurrencyRetailPrice, @supplierCurrencyUnitPrice, @supplierCurrencyAmount, @categoryCode, @orderType, @processedAs, @orderNumber, @upcCode, @isbnCode, @eanCode, @poNumber, @allocatedCode)";

But that gave me:

Must declare the scalar variable "@supplierID". and when searching for a solution, I found some code that was the way I've pasted.  Shoudl I ask a second question?

Author

Commented:
D'oh, i've worked it out, cheers for the nudge!

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