Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Winforms Npgsql 3.0.5 "An operation already in progress" error when trying to run multiple commands inside same connection

I'm trying to run the following snippet in C# Winforms. This piece of code is working well with pgsql 2.2.6 adapter. What correction can be made in order to work fine with pgsql3.0.5 adapter? Thanks.

NpgsqlConnection conn = new NpgsqlConnection(MainForm2.MyConString);
        {
            conn.Open();
            using (NpgsqlCommand cmd = new NpgsqlCommand("SELECT rfid,name,sc_id from passenger ORDER by name", conn))
            {
                NpgsqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var obj = new PassengerClass
                    {
                        RFID = dr.GetString(0),
                        Name = dr.GetString(1),
                        sc_id = dr.GetInt32(2)
                    };
                    s = dr.GetString(0);
                    try { ret.Add(s, obj); }
                    catch (Exception ex) { SM.Debug("Fail to add RFID Name in hash RFID:" + s + ex.ToString()); }
                }
            }
            MainForm2.PassHash = ret;
            try
            {
                using (NpgsqlCommand cmd = new NpgsqlCommand(string.Format("UPDATE place set useridx ={0} where useridx=0", MainForm2.userIDX), conn))
                    cmd.ExecuteNonQuery();

                using (NpgsqlCommand cmd = new NpgsqlCommand(string.Format("UPDATE zonename set useridx ={0} where useridx=0", MainForm2.userIDX), conn))
                    cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                SM.Debug("Error on update users IDX for place and zone with value 0 :" + ex.ToString());
            }

So, at the second command statement it gives me the following error:

A first chance exception of type 'System.InvalidOperationException' occurred in Npgsql.dll

Additional information: An operation is already in progress.

enter image description here

EDIT Additional info: enter image description here

like image 857
Adrian Stanculescu Avatar asked Mar 29 '16 12:03

Adrian Stanculescu


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C full form?

Full form of C is “COMPILE”.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You need to dispose of the NpgsqlDataReader which you get in the first ExecuteReader call: wrap it with a using statement just like you do with your NpgsqlCommand.

Disposing NpgsqlDataReader doesn't close the connection - only disposing the connection does that. An open reader corresponds to an open command currently running, which you must close before executing a new command. For atomicity, you can just start a transaction which encompasses several commands.

like image 132
Shay Rojansky Avatar answered Sep 23 '22 00:09

Shay Rojansky