Syntax error in MySQL Query in C# .Net

Last Reply on Feb 07, 2014 02:21 AM By Azim

Posted on Feb 07, 2014 02:18 AM

I am writing a code where query is like:

select *from users where username!='ABC';

but when i write it in c# code as

string excludedname="abc";

string query="select *from users where username !='"+excludedname+"'";

its giving error in syntax what is the correct way to do this??

Posted on Feb 07, 2014 02:21 AM Modified on on Feb 07, 2014 03:36 AM

Don’t use concatenated query use parameterized  query

string query="select * from users where username <> @UserName";

 pass the parameter like this,

cmd.Parameters.AddWithValue("@UserName", this.txtName.Text.Trim());