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:
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] // 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();
}
}
}
}
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();
}
}
}
}