-3
connectionString = @"server=localhost;database=" + lbDatabase.SelectedItem.ToString() + ";userid=root;password=;";

        string query = "SELECT order_id FROM orders WHERE id = 1";

        MySqlConnection connect = new MySqlConnection(connectionString);

        MySqlCommand cmd = new MySqlCommand(query, connect);

        reader = cmd.ExecuteReader();

Hello,

I have this problem when I run the following code. I get this not so very specific error:

An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll

Additional information: Connection must be valid and open.

If anyone could help me out I would really appreciate it,

Thank you.

6
  • 4
    Well the exception seems pretty clear. You don't have a valid, open connection. Look at your code - where do you think you're opening the connection? Try connect.Open()... Commented Feb 23, 2015 at 10:56
  • The connection is already opened by another methode. When I add connect.Open() it says the connection is already open. Commented Feb 23, 2015 at 11:00
  • Then there's code you haven't shown us, which makes it hard to help you... Commented Feb 23, 2015 at 11:00
  • pastebin.com/gHaHqxcr Commented Feb 23, 2015 at 11:02
  • No, please edit this question - it should be standalone. Commented Feb 23, 2015 at 11:14

2 Answers 2

0

The syntax for MySql Connectionstring is as follows:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Note that you are using 'userid' and 'password' instead of 'uid' and 'pwd'.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that your Connection String is Right .. Try This

connectionString = @"server=localhost;database=" + lbDatabase.SelectedItem.ToString() + ";userid=root;password=;";

    string query = "SELECT order_id FROM orders WHERE id = 1";

    MySqlConnection connect = new MySqlConnection(connectionString);


    MySqlCommand cmd = new MySqlCommand(query, connect);

    connect.Open();

    reader = cmd.ExecuteReader();

 /*Do Whatever You Want To Do Here */

connect.Close();

You Can Refer this Link for Reference. Exception: System.InvalidOperationException Trying to validate a login information

Hope This Helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.