Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:08:08+00:00 2026-06-06T03:08:08+00:00

I have written a custom class to handle database queries to a remote and

  • 0

I have written a custom class to handle database queries to a remote and local MySQL database, however when I do a nested loop I receive the below error:

MySql.Data.MySqlClient.MySqlException was unhandled
Message=There is already an open DataReader associated with this Connection which must be closed first.
Source=MySql.Data
ErrorCode=-2147467259
Number=0

My class currently looks like this

public class MySQLManager
{
    private MySqlConnection _MySQLRemoteConnection { get; set; }
    public void setup(string remoteUser, string remotePass, string remoteServerAddress, string remoteDb, string localUser, string localPass, string localServerAddress, string localDb)
    {
        _remote_server_address = remoteServerAddress;
        _remote_database = remoteDb;
        _remote_username = remoteUser;
        _remote_password = remotePass;    
    }
    public void connect()
    {
        try
        {
            _MySQLRemoteConnection = new MySqlConnection() { ConnectionString = string.Format("server={0};database={1};uid={2};password={3};", _remote_server_address, _remote_database, _remote_username, _remote_password) };
            _MySQLRemoteConnection.Open();
            _RemoteConnection = true;
        }
        catch (MySqlException ex)
        {
            _RemoteConnection = false;
        }
    }
    public MySqlCommand run(string query, List<MySqlParameter> dbparams = null)
    {
        connect();

        MySqlCommand sql = getConnection().CreateCommand();
        sql.CommandText = query;
        if (dbparams != null)
        {
            if (dbparams.Count > 0)
            {
                sql.Parameters.AddRange(dbparams.ToArray());
            }
        }
        //disconnect();
        return sql;
    }
    public MySqlDataReader fetch(MySqlCommand cmd)
    {
        //connect();
        var t = cmd.ExecuteReader();
        //disconnect();
        return t;
    }

And the code that I’m running to create the error, now I understand I can do the below example in a single query, this is an EXAMPLE query to re-create the error, writing it into a single query will not work with live examples.

query = "SELECT field1 FROM tmp WHERE field1 < 3";
            using (var sql = db.run(query))
            {
                txtResponse.Text += "Query ran" + nl;
                using (var row = db.fetch(sql))
                {
                    txtResponse.Text += "Query fetched" + nl;

                    db.connect();
                    while (row.Read())
                    {
                        txtResponse.Text += "Row : " + row[0].ToString() + nl;
                        query = "SELECT val1 FROM tmp2 WHERE field1 = '" + row[0].ToString() + "'";
                        //db.disconnect();
                        using (var sql2 = db.run(query))
                        {
                            txtResponse.Text += "Query ran" + nl;
                            db.disconnect();
                            using (var row2 = db.fetch(sql))
                            {
                                txtResponse.Text += "Query fetched" + nl;
                                db.connect();
                                while (row.Read())
                                {
                                    txtResponse.Text += "        Val : " + row2[0].ToString() + nl;
                                }
                            }
                        }
                    }
                }
            }

So how would I go about getting the second loop to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:08:10+00:00Added an answer on June 6, 2026 at 3:08 am

    For SQL Server, you could use MultipleActiveResultSets=true on connection string, but this most likely won’t work for MySQL.

    The other option is to use 2 connections, one for each data reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have written a custom SessionStoreProvider class that inherits from the SessionStateStoreProviderBase. I have
I have written a small custom class to run an audit trail in Lotus
I have written a custom class that depends significantly on touch dragging/positioning, outside of
I have written a custom model binder for List in my MVC project however
I have written a custom preference class which contains a simple progress bar in
I have a custom class written, which I instantiate from an event procedure similar
I have a custom class that I've written that extends ImageView (for Android Java).
I have written a custom class for overriding the seek method of QSlider. Basically
I have written a custom launcher in Eclipse which I can access via the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.