Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to fire this query through c# code to mysql server database but following error is occurred. Is my query wrong. plz help. I am using mysql server 5.0

string chkUser = "Select * FROM [User] where Email='"+ UserName + "'AND Password='" + Password + "';";

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

What I have tried:

I am trying to fire this query through c# code to mysql server database but following error is occurred. Is my query wrong. plz help. I am using mysql server 5.0

string chkUser = "Select * FROM [User] where Email='"+ UserName + "'AND Password='" + Password + "';";

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
Posted
Updated 23-Mar-16 10:34am
Comments
Richard Deeming 23-Mar-16 16:13pm    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Also, you're storing passwords in plain text. That is an extremely bad idea. You should only ever store a salted hash of the password, using a unique salt per record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Kishor-KW 23-Mar-16 16:29pm    
yes I know about mysql injection but here i did not take care of it. please give me solution on same query
CHill60 23-Mar-16 16:29pm    
What was the word or character after "near"? That would be the bit of the error message that tells you where the problem is!

1 solution

Your query should be
C#
string chkUser = "Select * FROM [User] where Email=@UserName AND Password=@Password;";
Use command parameters to provide the values for @UserName and @Password You've said you know about this so I haven't provided an example.

Note the space before AND
 
Share this answer
 
Comments
Kishor-KW 24-Mar-16 15:30pm    
Means we can't use this syntax in mysql. as we can in mssql
CHill60 26-Mar-16 14:17pm    
The MySQL classes for .NET also take command parameters so you can use this for MySQL. See MySQL :: MySQL Connector/Net Developer Guide :: 5.1.4 Working with Parameters[^]

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