Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
cmd.CommandText = 
@"INSERT INTO SSS_Contribution 
VALUES (
EmpID = " + txtEmployeeID.Text + @" , 
LastName = '" + txtEmployeeLastName.Text + @"', 
FirstName = '" + txtEmployeeFirstName.Text + @"', 
MiddleName = '" + txtEmployeeMiddleName.Text + @"', 
SSS = '" + txtSSSNo.Text + @"', 
Client = '" + txtClient.Text + @"', 
DeptCoor = '" + txtCoordinator.Text + @"', 
FirstERShare = '0.00', 
FirstEEShare = '0.00', 
FirstTotal = '0.00', 
SecondERShare = " + y.ToString() + @", 
SecondEEShare = " + txtSSS.Text + @", 
SecondTotal = (" + txtSSS.Text + " + " + y.ToString() + @"), 
TotalERShare = (" + y.ToString() + @"), 
TotalEEShare = (" + txtSSS.Text + @"), 
TotalContribution = (" + txtSSS.Text + " + " + y.ToString() + @")
)";


Whats wrong in my sql statement? It keeps having this error
Incorrect syntax near '='.


I tried debugging it many times. Thank you kind sirs :)
Posted
Updated 16-Sep-14 22:25pm
v3
Comments
Thomas Nielsen - getCore 17-Sep-14 4:20am    
make a stored procedure instead!
If you must do this, use string.format method instead to clarify your syntax and/ or put the sql statement into a variable before assigning to cammand text, then place a breakpoint and copy paste to your MSSQL management studio or whateer rdbms client you have and try to execute to get more help on the problem if it isn't already clear
George Jonsson 17-Sep-14 4:24am    
You should post the resulting string as well.

Your approach is wrong from the very beginning. You should never create a query by concatenation of string taken from your UI. Instead, you need to use parametrized statements. Please see: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

If you do it your way, you make your application totally vulnerable to a well-known exploit: SQL Injection. The user can write anything in the UI, including some SQL fragment. Are you getting the idea? This is how: http://xkcd.com/327[^].

Please see my past answers:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

—SA
 
Share this answer
 
Your INSERT query syntax is wrong. The syntax is

SQL
INSERT INTO <table_name> [(<ColumnName1, ColumnName2, ... ColumnNameN>)] VALUES (Data1, Data2... DataN);


Apart from that you need to use Parameterized SQL as explained by Sergey.
 
Share this answer
 
v2
SQL
cmd.CommandText = "INSERT INTO SSS_Contribution VALUES (" + txtEmployeeID.Text + " ,'" + txtEmployeeLastName.Text + "','" + txtEmployeeFirstName.Text + "','" + txtEmployeeMiddleName.Text + "',  '" + txtSSSNo.Text + "',  '" + txtClient.Text + "','" + txtCoordinator.Text + "', '0.00', '0.00',  '0.00',  " + y.ToString() + ", " + txtSSS.Text + ",(" + txtSSS.Text + " + " + y.ToString() + "), (" + y.ToString() +"), (" + txtSSS.Text + "), (" + txtSSS.Text + " + " + y.ToString() + "))";


* store concatenated section (i marked - underlined : to string and insert )
 
Share this answer
 
v4
Comments
Nelek 17-Sep-14 5:45am    
Sorry, I edited you answer to add the code tags, but it looked weird so I did a rollback to let it as it was.
As Sergey has written. Do not concat SQL statements in a string and use user input directly.

A simple description that is easy to understand can be found here: Give me parameterized SQL, or give me death/[^]

A simple user input in "txtEmployeeLastName.Text" like => Hello 'World
will make the SQL statement fail.
 
Share this answer
 

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