I have run the command without any error in mysql. my other command run fine but this code is not work. do someone know what happen with this code.
private static User GetUser(MySqlCommand cmd)
{
User usr = new User();
MySqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
usr.Id = Convert.ToInt32(rdr["Id"]);
usr.Level = (Level)Enum.Parse(typeof(Level), rdr["level"].ToString());
usr.Email = rdr["email"].ToString();
usr.CreatedDate = Convert.ToDateTime(rdr["createdDate"].ToString());
usr.LastLoginDate = Convert.ToDateTime(rdr["lastLoginDate"].ToString());
}
}
return usr;
}
public static User GetUserFromID(int userID)
{
string qry = "SELECT * FROM user WHERE ID = ?userID";
User user = new User();
MySqlConnection cnn = new MySqlConnection(Globals.CONNSTRING);
cnn.Open();
using (cnn)
{
MySqlCommand cmd = new MySqlCommand(qry, cnn);
cmd.Parameters.AddWithValue("userID", userID);
user = GetUser(cmd);
}
cnn.Close();
return user;
}
The code I paste here gave me error that
"The given key was not present in the dictionary."
on the line of Line 158:
MySqlDataReader rdr = cmd.ExecuteReader();
Do someone know what wrong happen with this code? I have added CharSet=utf8;
in connectionstring as people suggest in SO.
The database I use is mariaDB and connector is mysql latest connector. Do someone know if this have any trouble.
I have no problem while I run my other function. The problem happen in this single function where I use mysqldatareader execution.