ASP .Net – MySql – Unable to read any data-Collection of common programming errors
I am stuck with a problem and do not have a single idea WTF is happening. I’ve got an C# web application that needs to communicate with a MySQL db using web services. I can use methods to insert data, but whenever I try to read anything (ExecuteReader or ExecuteScalar) it fails with this very strange message:
The given key was not present in the dictionary.
Now, the web service uses this class to communicate with the db:
public class DatabaseHelper
{
private MySqlCommand cmd;
private MySqlConnection con;
public DatabaseHelper()
{
String server = "localhost";
String database = "testdb";
String password = "password";
String username = "root";
String connString = "Server = " + server + "; Database = " + database + "; Uid = " + username + "; Pwd = " + password + "; default command timeout=60;";
con = new MySqlConnection();
con.ConnectionString = connString;
cmd = new MySqlCommand();
cmd.Connection = con;
}
public void RunExecuteNonQuery(string sql, MySqlParameter[] param)
{
try
{
cmd.CommandText = sql;
if (param != null)
{
cmd.Parameters.AddRange(param);
}
cmd.CommandText = sql;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Parameters.Clear();
}
catch (MySqlException ex)
{
con.Close();
}
}
public object RunExecuteScalar(string sql, MySqlParameter[] param)
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
cmd.CommandText = sql;
object ret;
con.Open();
ret = cmd.ExecuteScalar();
con.Close();
cmd.Parameters.Clear();
return ret;
}
public MySqlDataReader RunExecuteDataReader(string sql, MySqlParameter[] param)
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
cmd.CommandText = sql;
MySqlDataReader ret = null;
con.Open();
ret = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return ret;
}
}
It fails on cmd.ExecuteReader or cmd.ExecuteScalar!
On the other hand, a method from the web service looks as following:
[WebMethod]
public string[] GetCategories()
{
String sql = "SELECT * FROM category";
DatabaseHelper dh = new DatabaseHelper();
MySqlDataReader dr = dh.RunExecuteDataReader(sql, null);
List categories = new List();
while (dr.Read())
{
categories.Add(dr[0].ToString());
}
dr.Close();
return categories.ToArray();
}
It is really strange since the error is very cryptic (I am not using a dictionary at all) and I have used this same class in many other applications. I tried this same solution on a different computer with the same result. I have even tried different versions of MySql.Data.dll
The error stack:
System.Collections.Generic.KeyNotFoundException was unhandled by user code HResult=-2146232969 Message=The given key was not present in the dictionary. Source=mscorlib StackTrace: at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at MySql.Data.MySqlClient.CharSetMap.GetChararcterSet(DBVersion version, String CharSetName) at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData41() at MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData() at MySql.Data.MySqlClient.NativeDriver.ReadColumnMetadata(Int32 count) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at Panopticon.DataLayer.DatabaseHelper.RunExecuteDataReader(String sql, MySqlParameter[] param) in C:\Users\Visar\Documents\Visual Studio 2010\Projects\TestApp\TestApp\DataLayer\DatabaseHelper.cs:line 84 at Panopticon._Default.Page_Load(Object sender, EventArgs e) in C:\Users\Visar\Documents\Visual Studio 2010\Projects\TestApp\TestApp\Default.aspx.cs:line 16 at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: