Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connection to MySql error

Status
Not open for further replies.

DPaul1994

Programmer
Mar 9, 2015
46
0
0
RO
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;
[indent]            // Connection must be valid and open.[/indent]
                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;
[indent]              ConnConfig.connOpen(); // error: The connection is already open.[/indent]
                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();
                    }
                }
            }
        }
 
I have no idea what your returns but have you tried

Code:
using (var conn = ConnConfig.getConnection())
{
	conn.Open(); // or .open()
	...
}
 
getConnection() returns true or false. If connection is on, returns true, else is trying to connect.
 
DPaul1994 said:
getConnection() returns true or false
so basically, you are creating an instance of MySqlCommand via
Code:
MySqlCommand cmd = new MySqlCommand(dataA, [COLOR=#CC0000]true[/color]);
I think you need to get the MySqlConnection object and pass that instead of true to the MySqlCommand constructor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top