Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting error: An operation is already in progress

Tags:

c#

postgresql

I am getting next error:

System.InvalidOperationException: An operation is already in progress.

I can't understand it's reason. If I comment block that start-end all work fine. If no I am betting error. Any ideas?

     public void PGConnect()
    {
        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);

        }

        //start
        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);
            }
        }
        // end 


        Console.ReadKey();


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


        try
        {

            NpgsqlDataReader dr = command.ExecuteReader(); // VS show error here
            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();
        }

Windows 7. VS2015. PG 9.5


How it's look like: my picture looks like this


More pictures here:

http://img.ctrlv.in/img/16/04/20/5717882d0b968.png

http://img.ctrlv.in/img/16/04/20/57179041abaa4.png

http://img.ctrlv.in/img/16/04/20/571790825648f.png

like image 384
Dmitry Bubnenkov Avatar asked Apr 20 '16 13:04

Dmitry Bubnenkov


2 Answers

You should close previous DbDataReader before using the next one. Not closing it causes this exception.

...    
if (notFoundedTables.Count != 0) // if not empty
{
    ...
}
// end 

/** IMPORTANT **/
/** Closing Data Reader **/
drGetDBTables.Close();

Console.ReadKey();

NpgsqlCommand command = new NpgsqlCommand("SELECT city, state FROM cities", conn);
...
like image 144
PiKos Avatar answered Oct 20 '22 08:10

PiKos


Make sure you are not mixed sync and async calls at the same time.

like image 1
Nalan Madheswaran Avatar answered Oct 20 '22 08:10

Nalan Madheswaran