Reputation: 1167
As in title. I tried to do everything, I searched on internet everywhere but it doesn't work. Here's the code:
public void SetIP(String IP, String Username)
{
try
{
String commandString = "UPDATE `Users` SET `IP` = '@ip' WHERE '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 into Strings IP and Username. I get Username from TextBox and IP adress by doing this 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 for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);
return direction;
}
And I just call method SetIP like this: SetIP(GetIP(), UsernameBox.Text); But when I come to the database to check if it changed it's still the same. All the time.
//Edit: Got this error command: "There is already an open DataReader associated with this Connection which must be closed first."
I use this DataReaders:
public bool FindUsername(String Username)
{
String commandString = "select * from Users where 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 * from Users where 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 * from Users where Username = '"+Username+"' and Password = '"+Password+"' ;";
command = new MySqlCommand(commandString, connection);
MySqlDataReader connectionReader = command.ExecuteReader();
if (connectionReader.Read())
{
return true;
}
else
{
connectionReader.Close();
return false;
}
}
I'm using only "LoginSystem_FindUser" and after that SetIP, FindUser and FindEmail I use only for registration.
Upvotes: 0
Views: 120
Reputation: 1167
I got it. I forgot
connectionReader.Close();
after if statement. Thanks BTW.
Upvotes: 0
Reputation: 263933
'Username' = '@user'
will always return to false because it compares literally.
It's because you parameters were wrapped with single quotes. Remove the single quotes and it will work.
String commandString = "UPDATE `Users` SET `IP` = @ip WHERE Username = @user;";
One more thing, column names are identifiers so they should also not be surrounded with single quotes.
Upvotes: 2