Im trying to learn how to access databases from a c# application. I have written this simple code, to access a MySql database I hosted on freemysqlhosting.com. Its a very simple windows forms application: it contains a button called 'login' that upon press, should put the username that has the password '1234' in the textbox called 'username' (should be "sampleUser").
This is the code I have tried:
private void login_Click(object sender, EventArgs e)
{
string connectionString = "server=sql11.freemysqlhosting.net;user id=*********;database=sql11158998;password=********;";
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM users WHERE password = 'hrsidkpi'", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
try
{
while (reader.Read())
{
username.Text = "" + reader[0];
}
}
finally
{
reader.Close();
}
}
}
However, when I press the button, the application hangs. No exception/error is thrown. I left it on for a couple of minutes and nothing happened. I know the connection is working and that the details in the connection string are true because I have a DataSet connection I made to the server using the visual studio's "Data Sources".
Even when I remove all code inside the using caluse, it freezes. If I remove con.Open() it does not hang, so I believe this is the line that's causing the issue.
I have tried to look for a solution on the internet, and found quite a few people having the same problem, however no solution was given, or the solution given had no effect on my situation.
Help would be greatly appreciated, and sorry for english mistakes if I made any.