2 Replies - 16735 Views - Last Post: 07 July 2011 - 12:17 AM Rate Topic: -----

#1 Omniabsence   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-July 11

C# and Npgsql - The connection is not open

Posted 06 July 2011 - 10:48 PM

I have installed npgsql and started a project which includes the .dll. I have a function that is able to read from the database successfully. However, when trying to input data in to a table of the Database, I get the following error:

System.InvalidOperationException: The connection is not open".

The code is very basic, pretty much copy pasted from the npgsql documentation - Click a button, open a connection to Postgres DB and input data in to a table. The error is occurring on the line in which the command is actually run (line 25 below).

private void button2_Click(object sender, EventArgs e)
        {
            //Get values from Textboxes
            int age = Int32.Parse(idBox.Text); //convert so that it goes into DB as int.
            Int32 rows = 0;//Rows updated, used later.
            string name = wordBox.Text;

            //Connection info get.
            string details = String.Format("Server={0};Port={1};" + "User Id={2};Password={3};Database={4};",
            hsotBox.Text, postBox.Text, userBox.Text, passBox.Text, dbBox.Text);

            //Query input get.
            string instructions = "INSERT INTO fin (name, age) VALUES ('" + name + "', " + age + ");";

            //Open connector using details.
            NpgsqlConnection connector = new NpgsqlConnection(details);
            connector.Open();

            //Create command from query.
            NpgsqlCommand query = new NpgsqlCommand(instructions);

            try
            {
                //Run command and input number of rows updated into rows.
                rows = query.ExecuteNonQuery(); //FAILS ON THIS LINE
                MessageBox.Show(rows + " rows have been updated");
            }
            finally
            {
                connector.Close();
            }
        }


Notes:
1. Using the try-finally block, I get the Visual Express error: The Connection is not open.
2. Using the try-catch block to a messageBox, the program loads but I get "System.InvalidOperationException: The connection is not open" when clicking the button.
3. With no try block at all, I get error 1.

Any help with this would be greatly appreciated. I am new to programming and this particular problem is proving to be a very frustrating wall.

Is This A Good Question/Topic? 0
  • +

Replies To: C# and Npgsql - The connection is not open

#2 wiero   User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: C# and Npgsql - The connection is not open

Posted 07 July 2011 - 12:11 AM

you should use
connector.CreateCommand()



or
NpgsqlCommand query = new NpgsqlCommand(instructions);
query.Connection = connector;


When you created command it did't know anything about connection.
Was This Post Helpful? 2
  • +
  • -

#3 Omniabsence   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-July 11

Re: C# and Npgsql - The connection is not open

Posted 07 July 2011 - 12:17 AM

View Postwiero, on 07 July 2011 - 12:11 AM, said:

you should use
connector.CreateCommand()



or
NpgsqlCommand query = new NpgsqlCommand(instructions);
query.Connection = connector;


When you created command it did't know anything about connection.


Thanks alot! This has solved my problem, much appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1