INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Connection to MySql error

Connection to MySql error

(OP)
Hi guys. I have a form where I have some labels, checkboxes and a button. For connection to MySql I have a class named ConnConfig and functions for open and closing connection(if needed, closeConn(), openConn()). So, I have a function which extracts from database some data:

CODE -->

private void select()
        {
            using (ConnConfig.getConnection())
            {
                MySqlCommand cmd = new MySqlCommand(dataA, ConnConfig.getConnection());
                cmd.CommandType = CommandType.Text;
// Connection must be valid and open.
using (MySqlDataReader rdra = cmd.ExecuteReader()) { try { while (rdra.Read()) { label2.Text = rdra["question"].ToString(); label3.Text = rdra["answer1"].ToString(); label4.Text = rdra["answer2"].ToString(); label5.Text = rdra["answer3"].ToString(); r1 = (int)rdra["option1"]; r2 = (int)rdra["option2"]; r3 = (int)rdra["option3"]; } } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); } finally { ConnConfig.closeConn(); } } } }
This function is called first on form load. I also call her in button1_Click function.
So, the problem is this: If I acces form, it tells me that 'Connection must be valid and open.', at DataReader from select() function. Then, if I open the connection above 'using (MySqlDataReader rdra = cmd.ExecuteReader())' line and run the script, everything is ok, but if I click on button1 I receive: 'The connection is already open', at the line where I opened connection.(ConnConfig.connOpen())

CODE -->

private void select()
        {
            using (ConnConfig.getConnection())
            {
                MySqlCommand cmd = new MySqlCommand(dataA, ConnConfig.getConnection());
                cmd.CommandType = CommandType.Text;
ConnConfig.connOpen(); // error: The connection is already open.
using (MySqlDataReader rdra = cmd.ExecuteReader()) { try { while (rdra.Read()) { label2.Text = rdra["question"].ToString(); label3.Text = rdra["answer1"].ToString(); label4.Text = rdra["answer2"].ToString(); label5.Text = rdra["answer3"].ToString(); r1 = (int)rdra["option1"]; r2 = (int)rdra["option2"]; r3 = (int)rdra["option3"]; } } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); } finally { ConnConfig.closeConn(); } } } }

RE: Connection to MySql error

(OP)
Anybody?

RE: Connection to MySql error

I have no idea what your returns but have you tried

CODE --> c#

using (var conn = ConnConfig.getConnection())
{
	conn.Open(); // or .open()
	...
} 

RE: Connection to MySql error

(OP)
getConnection() returns true or false. If connection is on, returns true, else is trying to connect.

RE: Connection to MySql error

Quote (DPaul1994)

getConnection() returns true or false
so basically, you are creating an instance of MySqlCommand via

CODE

MySqlCommand cmd = new MySqlCommand(dataA, true); 
I think you need to get the MySqlConnection object and pass that instead of true to the MySqlCommand constructor

RE: Connection to MySql error

(OP)
I solved it, thank you :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close