B
B
beduin012016-04-20 16:00:25
PostgreSQL
beduin01, 2016-04-20 16:00:25

Why does the error occur: An operation is already in progress?

I can't figure out why the error is thrown: System.InvalidOperationException: An operation is already in progress.
A piece of code that if you throw it out, everything works fine, I marked it.

List<UserData> uds = new List<UserData>();
        UserData ud;

        List<string> dblist = GetListDBsList();
        if (dblist.Contains(config.PGdbName))
        {
            Console.WriteLine("Data Base exists: {0}", config.PGdbName);
        }

        else
        {
            Console.WriteLine("Data Base DO NOT exists: {0}", config.PGdbName);
            return;
        }

        NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=" + config.PGLogin + ";" +
           "Password=" + config.PGPass + ";Database=" + config.PGdbName + ";");

        //select datname from pg_database;

        try
        {
            conn.Open();

            Console.WriteLine("PG Connected");
        }

        catch(SocketException e)
        {
            Console.WriteLine(e.Message);

        }

        //NpgsqlTransaction tr = conn.BeginTransaction();
        // Где-то в этом блоке ошибка 
        string tablesListRequestSQL = @"SELECT table_name FROM information_schema.tables WHERE table_schema='public'";
        NpgsqlCommand commandGetDBTables = new NpgsqlCommand(tablesListRequestSQL, conn);
        NpgsqlDataReader drGetDBTables = commandGetDBTables.ExecuteReader();
        //tr.Commit();
        while (drGetDBTables.Read())
        {
            existsInDBTables.Add(drGetDBTables[0].ToString());
        }

        foreach (string table in requireTablesList) // 
        {
            if (!existsInDBTables.Contains(table)) // if element from requireTablesList do not found -> DB have not simmilar Tables!
            {
                notFoundedTables.Add(table);
            }

        }

        if (notFoundedTables.Count != 0) // if not empty
        {
            Console.WriteLine("Next tables are marked as reqired for Sync, but can't be found in DataBase: ");

            foreach (var table in notFoundedTables)
            {
                Console.WriteLine(table);
            }
        }
        // Конец блока где ошибка.


        Console.ReadKey();


         NpgsqlCommand command = new NpgsqlCommand("SELECT city, state FROM cities", conn);


        try
        {

            NpgsqlDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
               // UserData ud = new UserData();
                ud.id = Int32.Parse(dr[0].ToString());
                ud.guid = (dr[1].ToString());
                ud.name = (dr[2].ToString());
                ud.userblob = (byte[])dr[3];
                uds.Add(ud);
                //File.WriteAllBytes("outputimg.jpg", ud.userblob);
                //Console.ReadKey();

            }

        }

        finally
        {
            conn.Close();
        }

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2016-04-20
@SaNNy32

In general, An operation is already in progress is supposed to mean that you're trying to execute a new query on a connection that is still busy with an old one.

https://github.com/npgsql/npgsql/issues/826

M
Maa-Kut, 2016-04-20
@Maa-Kut

How about getting readers in using pick up?

using(NpgsqlDataReader drGetDBTables = commandGetDBTables.ExecuteReader())
{
        //tr.Commit();
        while (drGetDBTables.Read())
        {
            existsInDBTables.Add(drGetDBTables[0].ToString());
        }
}
...
using(NpgsqlDataReader dr = command.ExecuteReader())
{
            while (dr.Read())
            {
               // UserData ud = new UserData();
                ud.id = Int32.Parse(dr[0].ToString());
                ud.guid = (dr[1].ToString());
                ud.name = (dr[2].ToString());
                ud.userblob = (byte[])dr[3];
                uds.Add(ud);
                //File.WriteAllBytes("outputimg.jpg", ud.userblob);
                //Console.ReadKey();

            }
}

I suspect that the second ExecuteReader crashes due to the fact that the previous reader is not closed - the command is still being executed. By the way, the commands themselves can also be wrapped in using - they are also IDisposable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question