c# - Database Update doesn't work -
as in title. tried everything, searched on internet everywhere doesn't work. here's code:
public void setip(string ip, string username) { try { string commandstring = "update `users` set `ip` = '@ip' 'username' = '@user';"; command = new mysqlcommand(commandstring, connection); command.parameters.addwithvalue("@ip", ip); command.parameters.addwithvalue("@user", username); command.beginexecutenonquery(); } catch (exception e) { messagebox.show(e.message); } }
i correctly put both of values strings ip , username. username textbox , ip adress doing code:
public string getip() { string direction = ""; webrequest request = webrequest.create("http://checkip.dyndns.org/"); using (webresponse response = request.getresponse()) using (streamreader stream = new streamreader(response.getresponsestream())) { direction = stream.readtoend(); } //search ip in html int first = direction.indexof("address: ") + 9; int last = direction.lastindexof("</body>"); direction = direction.substring(first, last - first); return direction; }
and call method setip this: setip(getip(), usernamebox.text); when come database check if changed it's still same. time.
//edit: got error command: "there open datareader associated connection must closed first."
i use datareaders:
public bool findusername(string username) { string commandstring = "select * users username = '" + username + "';"; command = new mysqlcommand(commandstring, connection); mysqldatareader connectionreader = command.executereader(); if (connectionreader.read()) { connectionreader.close(); return true; } else { connectionreader.close(); return false; } } public bool findemail(string email) { string commandstring = "select * users email = '" + email + "';"; command = new mysqlcommand(commandstring, connection); mysqldatareader connectionreader = command.executereader(); if (connectionreader.read()) { connectionreader.close(); return true; } else { connectionreader.close(); return false; } } public bool loginsystem_finduser(string username, string password) { string commandstring = "select * users username = '"+username+"' , password = '"+password+"' ;"; command = new mysqlcommand(commandstring, connection); mysqldatareader connectionreader = command.executereader(); if (connectionreader.read()) { return true; } else { connectionreader.close(); return false; } }
i'm using "loginsystem_finduser" , after setip, finduser , findemail use registration.
'username' = '@user'
will return false because compares literally.
it's because parameters wrapped single quotes. remove single quotes , work.
string commandstring = "update `users` set `ip` = @ip username = @user;";
one more thing, column names identifiers should not surrounded single quotes.
Comments
Post a Comment