c# - CSharp Prepared Statement with MySQL does not work -
hello folk, first tell new csharp , why did not know way of creating connection mysql wrong, because of had change prepared statement. please kind!
this method trying make works.
[httppost] public actionresult validateslogin (user euser) { var email = euser.email; var password = euser.password; try { mysqlcommand cmd = mysqlconn.cmd; cmd = new mysqlcommand(" user " + "where email=@email " + "and password=@password", mysqlconn.conn); cmd.prepare(); cmd.parameters.addwithvalue("@email", username); cmd.parameters.addwithvalue("@password", password); int result = (int)cmd.executereader(); //encrypting login user password //the encrypter , decrypter must have same key 'kahat' below var encryptpassword = encrypthelper.encryptstring(euser.password, "kahat"); // returns true when username , password match: if (result > 0) { system.web.security.formsauthentication.setauthcookie(euser.email, false); return redirecttoaction("index", "index", new { area = "index" }); } else { tempdata["message"] = "login failed. email or password supplied doesn't exist."; return view("index"); } } catch (exception ex) { return throwjsonerror(ex); } }
and code below actual connection works well.
[httppost] public actionresult validate(user euser) { try { //pull email , password login page var email = euser.email; var password = euser.password; //pull first email , password database var currentuser = this.rpgeneric.find<user>(" user email=:email ", new string[] { "email" }, new object[] { email }).firstordefault(); //encrypting login user password //the encrypter , decrypter must have same key 'kahat' below var encryptpassword = encrypthelper.encryptstring(euser.password, "kahat"); //comparing user password in login page , current password in db if (currentuser != null && encryptpassword.equals(currentuser.password, stringcomparison.ordinal) && currentuser.emailconfirmed == true) { system.web.security.formsauthentication.setauthcookie(euser.email, false); return redirecttoaction("index", "index", new { area = "index" }); } else { tempdata["message"] = "login failed. email or password supplied doesn't exist."; return view("index"); } } catch (exception ex) { return throwjsonerror(ex); } }
your sql query incomplete.
cmd = new mysqlcommand(" user " + "where email=@email " + "and password=@password", mysqlconn.conn);
you aren't telling mysql want do, need add "select " clause , column names you're selecting table:
cmd = new mysqlcommand("select [*],[column, column...] user " + "where email=@email " + "and password=@password", mysqlconn.conn);
Comments
Post a Comment