An error with an sql string (c#) -


i have written following script (in c#):

string     sqlcommand = "select   * " +                  "from     tblmatches " +                  "where matchplayernick =" + comboboxplayer.text + " " +                  "order matchname "; 

when run program, this: "data type mismatch in criteria experssion". datatype of matchplayer is, of course, "text".

what's wrong script then?

thanks!

string     sqlcommand = "select   * " +                  "from     tblmatches " +                  "where matchplayernick ='" + comboboxplayer.text + "' " +                  "order matchname "; 

but query above vulnerable sql injection. can prevented if parameterized values using command object , parameters.

try code snippet:

string content = comboboxplayer.text; string connstr = "connection string here"; string sqlcommand = @"select   *                           tblmatches                        matchplayernick = @content                       order matchname"; using (sqlconnection conn = new sqlconnection(connstr)) {     using(sqlcommand comm = new sqlcommand())     {         comm.connection = conn;         comm.commandtext = sqlstatement;         comm.commandtype = commandtype.text;          comm.parameters.addwithvalue("@content", content);          try         {             conn.open();             // other codes here         }         catch(sqlexception e)         {             // exception             // not hide             // e.message.tostring()         }     } } 

for proper coding

  • use using statement propr object disposal
  • use try-catch block handle objects

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -