Click here to Skip to main content
15,891,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to execute
C#
cmd.CommandText = "update onlineUsers set status='"+true+"' where username='"+txtUsername.Text+"'";

In the above query I am getting an error which says:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near '('.
Posted
Updated 18-Apr-15 11:55am
v2
Comments
[no name] 18-Apr-15 11:47am    
1.) Forget about using ugly SQL string concatenation, use Parameter instead.
2.) What db are you using? Does it understand boolean fields and value "true"? Maybe simply test with '0'/'1'.

1 solution

Probably, it's the username that's giving the problem.
But...you shouldn't do that. 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.
The chances are that fixing that will also solve your other problem, as well as making your code easier to read.
C#
cmd.CommandText = "update onlineUsers set status=@STAT where username=@UN";
cmd.Parameters.AddWithValue("@STAT", "true");
cmd.Parameters.AddWithValue("@UN", txtUsername.Text);
 
Share this answer
 
Comments
Member 11417132 18-Apr-15 11:59am    
I appreciate your response but I am still getting the same error..!!
OriginalGriff 18-Apr-15 12:09pm    
Then the code that is giving the error is not the code you are showing us - we need to see the context. Show us the whole method and indicate which line the exception comes on.
Nelek 18-Apr-15 18:21pm    
"Additional information: Incorrect syntax near '('."

I think that was a hint that the code posted was not exactly the error ;) ;P

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