I am running a .Net WebForms site that queries a MySQL database repeatedly in order to fill drop-downs on a form. The queries are almost all stored procedures if that makes a difference. This has worked consistently for over a year.
Sometime during the last week, with no code changes one of my pages started throwing the "Commands out of sync" error. But for some reason, it does this inconsistently. If I refresh the page several times, the page eventually loads successfully. But if I re-load again, it may succeed but may fail again.
After reading up on the error, it seems that my code isn't consuming all the data returned from the stored procedures. Maybe I need to close my ODBCDataAdapter or ODBCDataReader before opening the next one, or maybe I have to send some special command to clear out the query.
Here is the code that seems problematic -- myda is only created successfully sometimes:
public DataSet FillDatasets<T, T2, T3, T4>(T con, ref T4 reader, string sql, string tablename)
where T : IDbConnection, new()
where T2 : IDbDataAdapter, new()
where T3 : IDbCommand, new()
where T4 : IDataReader
{
// Declare a DataAdapter, could be OleDB or ODBC or something elsee
T2 myda;
T3 mycommand;
// Local dataset
DataSet ds = new DataSet();
// This is the StackOverflow way to instantiate a Data Adapter without arguments (which throws an error)
mycommand = (T3)Activator.CreateInstance(typeof(T3), sql, con);
reader = (T4) mycommand.ExecuteReader();
//mycommand.Dispose();
myda = (T2)Activator.CreateInstance(typeof(T2), sql, con);
myda.Fill(ds);
ds.Tables[0].TableName = tablename;
return (ds);
}The code is called like this:
DataSet ds = rs.FillDatasets<OdbcConnection, OdbcDataAdapter, OdbcCommand, OdbcDataReader>
(MySQLConnection, ref MySQLReader, sqlString, "ActiveTrips");I would attempt to narrow down the problem more (or offer more code) if I knew which direction to go with this. Any help appreciated.</div> <div>Thanks</div> <div></div>