How to Select data from table 1 and insert into table 2 in PostgresQLusing C#










0















I am trying to fetch data from one table and insert it into another table on Postgres using C# on Visual Studio code.
I keep getting this error:



An unhandled exception of type 'Npgsql.NpgsqlOperationInProgressException' occurred in System.Private.CoreLib.dll: 'A command is already in progress:



I am guessing this is happening because my code tries to use the connection as it is being used by the first query. I, however, do not know how to fix it.



Here is my code below - Forgive any rookie mistakes, this is my first time ever using C# to do this.



using System;
using Npgsql;

class Sample

static void Main(string args)

// Specify connection options and open an connection
NpgsqlConnection conn = new NpgsqlConnection("Server=xxxxxxxx;User
Id=xxxxxxx;" +
"Password=xxxxxxx;Database=xxxxxxxx;");
conn.Open();

//Define select query
NpgsqlCommand cmd = new NpgsqlCommand("select * from table1", conn);


// Execute a query
NpgsqlDataReader dr = cmd.ExecuteReader();

//Define Insert query
NpgsqlCommand cmd1 = new NpgsqlCommand("insert into table2
values(dr[0],dr[1])", conn);

// Read all rows and output the first column in each row
while (dr.Read())
cmd1.ExecuteNonQuery();

// Close connection
conn.Close();




Thanks in Advance!










share|improve this question


























    0















    I am trying to fetch data from one table and insert it into another table on Postgres using C# on Visual Studio code.
    I keep getting this error:



    An unhandled exception of type 'Npgsql.NpgsqlOperationInProgressException' occurred in System.Private.CoreLib.dll: 'A command is already in progress:



    I am guessing this is happening because my code tries to use the connection as it is being used by the first query. I, however, do not know how to fix it.



    Here is my code below - Forgive any rookie mistakes, this is my first time ever using C# to do this.



    using System;
    using Npgsql;

    class Sample

    static void Main(string args)

    // Specify connection options and open an connection
    NpgsqlConnection conn = new NpgsqlConnection("Server=xxxxxxxx;User
    Id=xxxxxxx;" +
    "Password=xxxxxxx;Database=xxxxxxxx;");
    conn.Open();

    //Define select query
    NpgsqlCommand cmd = new NpgsqlCommand("select * from table1", conn);


    // Execute a query
    NpgsqlDataReader dr = cmd.ExecuteReader();

    //Define Insert query
    NpgsqlCommand cmd1 = new NpgsqlCommand("insert into table2
    values(dr[0],dr[1])", conn);

    // Read all rows and output the first column in each row
    while (dr.Read())
    cmd1.ExecuteNonQuery();

    // Close connection
    conn.Close();




    Thanks in Advance!










    share|improve this question
























      0












      0








      0








      I am trying to fetch data from one table and insert it into another table on Postgres using C# on Visual Studio code.
      I keep getting this error:



      An unhandled exception of type 'Npgsql.NpgsqlOperationInProgressException' occurred in System.Private.CoreLib.dll: 'A command is already in progress:



      I am guessing this is happening because my code tries to use the connection as it is being used by the first query. I, however, do not know how to fix it.



      Here is my code below - Forgive any rookie mistakes, this is my first time ever using C# to do this.



      using System;
      using Npgsql;

      class Sample

      static void Main(string args)

      // Specify connection options and open an connection
      NpgsqlConnection conn = new NpgsqlConnection("Server=xxxxxxxx;User
      Id=xxxxxxx;" +
      "Password=xxxxxxx;Database=xxxxxxxx;");
      conn.Open();

      //Define select query
      NpgsqlCommand cmd = new NpgsqlCommand("select * from table1", conn);


      // Execute a query
      NpgsqlDataReader dr = cmd.ExecuteReader();

      //Define Insert query
      NpgsqlCommand cmd1 = new NpgsqlCommand("insert into table2
      values(dr[0],dr[1])", conn);

      // Read all rows and output the first column in each row
      while (dr.Read())
      cmd1.ExecuteNonQuery();

      // Close connection
      conn.Close();




      Thanks in Advance!










      share|improve this question














      I am trying to fetch data from one table and insert it into another table on Postgres using C# on Visual Studio code.
      I keep getting this error:



      An unhandled exception of type 'Npgsql.NpgsqlOperationInProgressException' occurred in System.Private.CoreLib.dll: 'A command is already in progress:



      I am guessing this is happening because my code tries to use the connection as it is being used by the first query. I, however, do not know how to fix it.



      Here is my code below - Forgive any rookie mistakes, this is my first time ever using C# to do this.



      using System;
      using Npgsql;

      class Sample

      static void Main(string args)

      // Specify connection options and open an connection
      NpgsqlConnection conn = new NpgsqlConnection("Server=xxxxxxxx;User
      Id=xxxxxxx;" +
      "Password=xxxxxxx;Database=xxxxxxxx;");
      conn.Open();

      //Define select query
      NpgsqlCommand cmd = new NpgsqlCommand("select * from table1", conn);


      // Execute a query
      NpgsqlDataReader dr = cmd.ExecuteReader();

      //Define Insert query
      NpgsqlCommand cmd1 = new NpgsqlCommand("insert into table2
      values(dr[0],dr[1])", conn);

      // Read all rows and output the first column in each row
      while (dr.Read())
      cmd1.ExecuteNonQuery();

      // Close connection
      conn.Close();




      Thanks in Advance!







      c# postgresql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 9:07









      kanatakikanataki

      8510




      8510






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You don't do it like this, but in a single command(is much more efficient):



          "INSERT INTO table2(t21, t22) SELECT t11m t12 FROM table1"



          Doing this will also solve the error "A command is already in progress".






          share|improve this answer























          • When I do this:NpgsqlCommand cmd = new NpgsqlCommand("insert into table2(col1,col2) select col1 col2 from table1", conn); cmd.ExecuteNonQuery(); I get 'External component has thrown an exception.'

            – kanataki
            Nov 13 '18 at 11:02












          • That error appears at the ExecuteNonQuery() step

            – kanataki
            Nov 13 '18 at 11:03











          • Error is too generic, you sould test the command in postgresql and see if everything is fine. Also show the code use used.

            – Victorqedu
            Nov 13 '18 at 11:38











          • It was a stupid mistake on my end. Didn't put a comma between table1 columns. Side effects of doing this while extremely tired. Works fine now, Thanks!

            – kanataki
            Nov 13 '18 at 11:55










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277357%2fhow-to-select-data-from-table-1-and-insert-into-table-2-in-postgresqlusing-c-sha%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You don't do it like this, but in a single command(is much more efficient):



          "INSERT INTO table2(t21, t22) SELECT t11m t12 FROM table1"



          Doing this will also solve the error "A command is already in progress".






          share|improve this answer























          • When I do this:NpgsqlCommand cmd = new NpgsqlCommand("insert into table2(col1,col2) select col1 col2 from table1", conn); cmd.ExecuteNonQuery(); I get 'External component has thrown an exception.'

            – kanataki
            Nov 13 '18 at 11:02












          • That error appears at the ExecuteNonQuery() step

            – kanataki
            Nov 13 '18 at 11:03











          • Error is too generic, you sould test the command in postgresql and see if everything is fine. Also show the code use used.

            – Victorqedu
            Nov 13 '18 at 11:38











          • It was a stupid mistake on my end. Didn't put a comma between table1 columns. Side effects of doing this while extremely tired. Works fine now, Thanks!

            – kanataki
            Nov 13 '18 at 11:55















          0














          You don't do it like this, but in a single command(is much more efficient):



          "INSERT INTO table2(t21, t22) SELECT t11m t12 FROM table1"



          Doing this will also solve the error "A command is already in progress".






          share|improve this answer























          • When I do this:NpgsqlCommand cmd = new NpgsqlCommand("insert into table2(col1,col2) select col1 col2 from table1", conn); cmd.ExecuteNonQuery(); I get 'External component has thrown an exception.'

            – kanataki
            Nov 13 '18 at 11:02












          • That error appears at the ExecuteNonQuery() step

            – kanataki
            Nov 13 '18 at 11:03











          • Error is too generic, you sould test the command in postgresql and see if everything is fine. Also show the code use used.

            – Victorqedu
            Nov 13 '18 at 11:38











          • It was a stupid mistake on my end. Didn't put a comma between table1 columns. Side effects of doing this while extremely tired. Works fine now, Thanks!

            – kanataki
            Nov 13 '18 at 11:55













          0












          0








          0







          You don't do it like this, but in a single command(is much more efficient):



          "INSERT INTO table2(t21, t22) SELECT t11m t12 FROM table1"



          Doing this will also solve the error "A command is already in progress".






          share|improve this answer













          You don't do it like this, but in a single command(is much more efficient):



          "INSERT INTO table2(t21, t22) SELECT t11m t12 FROM table1"



          Doing this will also solve the error "A command is already in progress".







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 10:29









          VictorqeduVictorqedu

          1277




          1277












          • When I do this:NpgsqlCommand cmd = new NpgsqlCommand("insert into table2(col1,col2) select col1 col2 from table1", conn); cmd.ExecuteNonQuery(); I get 'External component has thrown an exception.'

            – kanataki
            Nov 13 '18 at 11:02












          • That error appears at the ExecuteNonQuery() step

            – kanataki
            Nov 13 '18 at 11:03











          • Error is too generic, you sould test the command in postgresql and see if everything is fine. Also show the code use used.

            – Victorqedu
            Nov 13 '18 at 11:38











          • It was a stupid mistake on my end. Didn't put a comma between table1 columns. Side effects of doing this while extremely tired. Works fine now, Thanks!

            – kanataki
            Nov 13 '18 at 11:55

















          • When I do this:NpgsqlCommand cmd = new NpgsqlCommand("insert into table2(col1,col2) select col1 col2 from table1", conn); cmd.ExecuteNonQuery(); I get 'External component has thrown an exception.'

            – kanataki
            Nov 13 '18 at 11:02












          • That error appears at the ExecuteNonQuery() step

            – kanataki
            Nov 13 '18 at 11:03











          • Error is too generic, you sould test the command in postgresql and see if everything is fine. Also show the code use used.

            – Victorqedu
            Nov 13 '18 at 11:38











          • It was a stupid mistake on my end. Didn't put a comma between table1 columns. Side effects of doing this while extremely tired. Works fine now, Thanks!

            – kanataki
            Nov 13 '18 at 11:55
















          When I do this:NpgsqlCommand cmd = new NpgsqlCommand("insert into table2(col1,col2) select col1 col2 from table1", conn); cmd.ExecuteNonQuery(); I get 'External component has thrown an exception.'

          – kanataki
          Nov 13 '18 at 11:02






          When I do this:NpgsqlCommand cmd = new NpgsqlCommand("insert into table2(col1,col2) select col1 col2 from table1", conn); cmd.ExecuteNonQuery(); I get 'External component has thrown an exception.'

          – kanataki
          Nov 13 '18 at 11:02














          That error appears at the ExecuteNonQuery() step

          – kanataki
          Nov 13 '18 at 11:03





          That error appears at the ExecuteNonQuery() step

          – kanataki
          Nov 13 '18 at 11:03













          Error is too generic, you sould test the command in postgresql and see if everything is fine. Also show the code use used.

          – Victorqedu
          Nov 13 '18 at 11:38





          Error is too generic, you sould test the command in postgresql and see if everything is fine. Also show the code use used.

          – Victorqedu
          Nov 13 '18 at 11:38













          It was a stupid mistake on my end. Didn't put a comma between table1 columns. Side effects of doing this while extremely tired. Works fine now, Thanks!

          – kanataki
          Nov 13 '18 at 11:55





          It was a stupid mistake on my end. Didn't put a comma between table1 columns. Side effects of doing this while extremely tired. Works fine now, Thanks!

          – kanataki
          Nov 13 '18 at 11:55

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277357%2fhow-to-select-data-from-table-1-and-insert-into-table-2-in-postgresqlusing-c-sha%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo