Wednesday, March 28, 2012

System.InvalidOperationException: Connection must be valid and open

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.

--
Matt Berther
http://www.mattberther.com

> 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();
> }
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.

"Matt Berther" <mberther@.hotmail.com> wrote in message
news:6852859632389121357049404@.news.microsoft.com. ..
> 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.
> --
> Matt Berther
> http://www.mattberther.com
>> 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();
>>
>> }
>>
OH Good u found ur Error...
Enjoy
Patrick

"Alejandro Penate-Diaz" wrote:

> 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.
> "Matt Berther" <mberther@.hotmail.com> wrote in message
> news:6852859632389121357049404@.news.microsoft.com. ..
> > 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.
> > --
> > Matt Berther
> > http://www.mattberther.com
> >> 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();
> >>
> >> }
> >>
>

0 comments:

Post a Comment