Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is syntax error in Insert into statement this line
C#
string cb = "insert into Sales1 ([InvoiceNo],[InvoiceDate],[SubTotal],[VATPercentage],[VATAmount],[GrandTotal],[TotalPayment],[PaymentDue]) VALUES ('" + txtInvoiceNo.Text + "'," + dtpInvoiceDate.Text + "," + txtSubTotal.Text + "," + txtTaxPer.Text + "," + txtTaxAmt.Text + "," + txtTotal.Text + "," + txtTotalPayment.Text + ",'" + txtPaymentDue.Text + "')";
Posted
Updated 8-Nov-14 1:05am
v2
Comments
Tomas Takac 8-Nov-14 5:59am    
are you using SQL Server? I guess invoice date in not in quotes? You need to pass dates the same way you are passing strings.

Do you know that this implementation is susceptible of SQL Injection[^]?

I would suggest you to rather use Stored Procedure[^] or Parameterized Queries[^] in SQL.
 
Share this answer
 
Comments
[no name] 8-Nov-14 7:49am    
Good hint but not OP's Problem at the Moment. Anyway a 5.
Manas Bhardwaj 8-Nov-14 7:52am    
thx Bruno :)
The "syntax error" is trivial, compared to the other problems this has.

Do not 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. Use Parametrized queries instead.

Doing that will very likely start to remove the syntax error - but it may leave some others you need to look at as well.
Starting with your dates...
1) Make sure you don't store them as NVARCHAR values: use DATETIME or you will have massive problems in future.
2) Don't pass them through as strings, even as parameters: that leaves the way open for SQL to misinterpret the date format and either throw an exception or worse, insert teh wrong data in your DB. You C# code has access to the user setting for Culture and thus can work out exactly what date he expects: SQL doesn't, and can't. Check and convert your dates to DateTime values in your C# and pass that through to SQL as a parameter.
 
Share this answer
 
Comments
Mike Meinz 8-Nov-14 8:25am    
Plus Parameterized queries provide a performance benefit over concatenated string queries.
[no name] 8-Nov-14 9:03am    
Also my 5

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