Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the below error show in data fatch from mysql database in c# with core asp.net

Error Message: MySqlException
Exception Type: MySql.Data.MySqlClient.MySqlException
Error Location : There is already an open DataReader associated with this Connection which must be closed first.


this problem is not regular but when multiple use login and request fire for data fatch

please give me some solution.

What I have tried:

public static DataTable SelectMethod(string _select)
        {
            DataTable _dtsel = new DataTable("tab1");
            try
            {
                ConOpen();
                using (MySqlCommand cmd = new MySqlCommand(_select, con))
                {
                    MySqlDataAdapter _mysel = new MySqlDataAdapter(cmd);
                    _mysel.Fill(_dtsel);
                }
            }
            catch (Exception ex) { SendErrorToText(ex); }
            return _dtsel;
        }
Posted
Updated 2-Sep-19 3:58am

C#
ConOpen();
using (MySqlCommand cmd = new MySqlCommand(_select, con))
  {
  MySqlDataAdapter _mysel = new MySqlDataAdapter(cmd);
  mysel.Fill(_dtsel);
  }
My guess is ConOpen checks to see if con is open and if not then open it.
You then run your query.
And you just leave the connection open

Simple solution is to run con.Close() as soon as you are done with the data adapter
 
Share this answer
 
MadMyche is right - and the best solution is not to use a "shared" connection object, but to create it as required and Dispose it when you are finished. The simplest way to do that is in a using block:
C#
using (MySqlConnection con = new MySqlConnection(strConnect))
    {
    con.Open();
    using (MySqlCommand cmd = new MySqlCommand("SELECT Id, description FROM myTable", con))
        {
        using (MySqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["Id"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }
Do the same for every DB usage, and your problem goes away - even if you start multithreading later.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900