Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
I want to create invoice
Error Is
An unhandled exception occurred while processing the request.
SqlException: Incorrect syntax near '/'.
Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, bool breakConnection, Action<action> wrapCloseInAction)

Stack Query Cookies Headers Routing
SqlException: Incorrect syntax near '/'.
Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, bool breakConnection, Action<action> wrapCloseInAction)
Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, bool breakConnection, Action<action> wrapCloseInAction)
Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, bool callerHasConnectionLock, bool asyncClose)
Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, out bool dataReady)
Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(string methodName, bool isAsync, int timeout, bool asyncWrite)
Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource<object> completion, bool sendToPipe, int timeout, out bool usedCache, bool asyncWrite, bool inRetry, string methodName)
Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
Food.Pages.Sales.IndexModel.executeProcINSERT(int InvoiceNum, int ItemId, decimal qty, Nullable<decimal> SalePrice, Nullable<decimal> discount, Nullable<decimal> AmountAfterDiscount, decimal Paid, decimal Remain, decimal Total, DateTime PaidDate, int CustomerId) in Index.cshtml.cs
+
            cmd.ExecuteNonQuery();  
Food.Pages.Sales.IndexModel.OnPost() in Index.cshtml.cs
+
                executeProcINSERT(InvoiceNum, ItemId, 1,Oitem.SalePrice ,InvoiceSale.Discount,
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+VoidHandlerMethod.Execute(object receiv

...
 
Microsoft.Data.SqlClient.SqlException: 'Incorrect syntax near '/'.'

in call storedProcedure


What I have tried:

C#
executeProcINSERT(InvoiceNum, ItemId, 1,Oitem.SalePrice ,InvoiceSale.Discount,
                      ((InvoiceSale.Discount * 1/100)*(InvoiceSale.Total))-InvoiceSale.Total, 
                      InvoiceSale.Paid, 
                      InvoiceSale.Paid-InvoiceSale.Total,
                      total, 
                      PaidDate,
                      1
                    );

public void executeProcINSERT(int InvoiceNum,int ItemId,decimal qty,decimal?SalePrice,decimal?discount, decimal?AmountAfterDiscount,
            decimal Paid, decimal Remain, decimal Total,DateTime PaidDate, int CustomerId )
        {
            string ConString = "Server=.;Database=OnlineMarket;Trusted_Connection=True;TrustServerCertificate=true;";
            
            SqlConnection cn = new SqlConnection(ConString);
            SqlCommand cmd = new SqlCommand($"InsertintoSalesInvoice {InvoiceNum},{ItemId},{qty},{SalePrice},{discount},{AmountAfterDiscount},{Paid},{Remain},{Total},{PaidDate},{CustomerId} ", cn);

            cn.Open();
            cmd.ExecuteNonQuery();  
            cn.Close();
Posted
Updated 7-Jul-23 10:33am
v2

1 solution

SqlCommand cmd = new SqlCommand($"InsertintoSalesInvoice {InvoiceNum},{ItemId},{qty},{SalePrice},{discount},{AmountAfterDiscount},{Paid},{Remain},{Total},{PaidDate},{CustomerId} ", cn);
That's not a valid SQL command, or even close.
SQL
INSERT INTO MyTable VALUES (...)
is pretty much the minimum INSERT command, but you shouldn't do it like that anyway - never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Comments
A Belal 8-Jul-23 10:20am    
of course i do backup

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900