Reputation: 285
I am trying to multiple queries in my code as below but it throws exception at line MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader();
since I already have an rdr open
I googled and found @according to MSDN its not supported,I need guidance on how to fix it?
public IEnumerable<BuildNotes_op> RetrieveBuildDetails(string BuildID)
{
....
var conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = AciDev.aciDevConnectionString;
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = @"query1";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader(); --> **rdr open here**
....
while (rdr.Read())
{
.....
if (rdr[4] != DBNull.Value)
....................
else
........
string sql_get_latest_build_notes_cmd = "query2";
MySqlCommand get_latest_build_notes_cmd = new MySqlCommand(sql_get_latest_build_notes_cmd, conn);
MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader(); --> **throws exception here**
if (rdr_get_latest_build_notes.HasRows)
{
....................
}
rdr_get_latest_build_notes.Close();
if (BuildDetails != null)
{
BuildNotesDetails.Add(BuildDetails);
}
}
rdr.Close();
conn.Close();
return BuildNotesDetails;
}
catch
{
throw;
}
finally
{
conn.Close();
}
Exception:
There is already an open DataReader associated with this Connection which must be closed first.
Upvotes: 4
Views: 1371
Reputation: 4191
try this:
public IEnumerable<BuildNotes_op> RetrieveBuildDetails(string BuildID)
{
var conn = new MySql.Data.MySqlClient.MySqlConnection();
var conn2 = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = AciDev.aciDevConnectionString;
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = @"query1";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader(); --> **rdr open here**
....
while (rdr.Read())
{
.....
if (rdr[4] != DBNull.Value)
....................
else
........
string sql_get_latest_build_notes_cmd = "query2";
conn2.ConnectionString = AciDev.aciDevConnectionString;
Console.WriteLine("Connecting to MySQL...");
conn2.Open();
MySqlCommand get_latest_build_notes_cmd = new MySqlCommand(sql_get_latest_build_notes_cmd, conn2);
MySqlDataReader rdr_get_latest_build_notes = get_latest_build_notes_cmd.ExecuteReader(); --> **throws exception here**
if (rdr_get_latest_build_notes.HasRows)
{
....................
}
rdr_get_latest_build_notes.Close();
conn2.Close();
if (BuildDetails != null)
{
BuildNotesDetails.Add(BuildDetails);
}
}
rdr_get_latest_build_notes.Close();
rdr.Close();
conn.Close();
conn2.Close();
return BuildNotesDetails;
}
catch
{
throw;
}
finally
{
rdr_get_latest_build_notes.Close();
rdr.Close();
conn.Close();
conn2.Close();
}
Creating a second connection
var
Upvotes: 1
Reputation: 1062580
options:
List<T>
or similar, before starting the inner query / queriesjoin
etc to read all the data in one go, as a single - wide -result set - and process it at the clientselect
operations in a single command to retrieve the data in multiple grids, and process them at the clientUpvotes: 0