System.InvalidOperationException: Connection must be valid and open

  • Thread starter Thread starter Alejandro Penate-Diaz
  • Start date Start date
A

Alejandro Penate-Diaz

Hi.

I am using this function tu execute updates to a MySql database from a sql
string, but I am getting a System.InvalidOperationException: Connection
must be valid and open.

Database queries work fine, but I cant update the source. Am I missing
something?

Thanks, Alejandro.



public static bool updateSource(string sql)

{

MySqlConnection conn = new MySqlConnection(myConnectionString);

MySqlCommand command = new MySqlCommand(sql,conn);

command.CommandType = CommandType.Text;

try

{

command.ExecuteNonQuery();

return true;

}

catch (Exception)

{return false;}

finally

{

conn.Close();

}
 
Hello Alejandro Penate-Diaz,

Exactly what the error message says... You need to have a valid *and* open
connection.

Try adding conn.Open() before the command.ExecuteNonQuery() method.
 
Heyy, thanks!! Problem was that I was like copy-paste and didn't realize
that DataAdapter.Fill actualy does that for me but in this case I have to do
it by myself. ;-)

Thanks a lot!
Alejandro.
 
OH Good u found ur Error...
Enjoy
Patrick

Alejandro Penate-Diaz said:
Heyy, thanks!! Problem was that I was like copy-paste and didn't realize
that DataAdapter.Fill actualy does that for me but in this case I have to do
it by myself. ;-)

Thanks a lot!
Alejandro.
 
Back
Top