This is my IActionResult, I'm working with MySQL, its supposed to make a user register, but for some reason everytime I try to execute it, I get the message "Connection must be valid and open" I've checked the connection string and it's valid, and I also open it, could anyone help me out?
Yes, I know this is prone to SQL Injection, so no need to tell me that.
public IActionResult Maak()
{
string conn = "server=localhost;user id=Job;password=SamplePassword;database=kingdom_of_uluth";
MySqlConnection con = new MySqlConnection(conn);
string userName = HttpContext.Request.Query["usernamesignup"];
string password = HttpContext.Request.Query["passwordsignup"];
con.ConnectionString = conn;
con.Open();
try
{
MySqlCommand com = new MySqlCommand($"INSERT INTO account(username, password, lastlogin) VALUES({userName}, {password}, now());");
com.ExecuteReader();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
con.Close();
return View("RegisterOK");
}
I've tried the solution that has been suggested by Henk Mollema, but then I get another error which is
Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll Unknown column 'asdasd' in 'field list'
in which asdasd is the username I put in the username field