473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

}
Nov 19 '05 #1
3 16649
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();

}

Nov 19 '05 #2
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" <mb******@hotmail.com> wrote in message
news:68***********************@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();

}


Nov 19 '05 #3
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" <mb******@hotmail.com> wrote in message
news:68***********************@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();

}



Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Conorozzo | last post by:
I am able to create new sources using CreateEventSource. I can call GetEventLogs, I can delete sources, I can see if sources exist BUT, as soon as I try a "WriteEntry", I get an exception: ...
6
by: Tony | last post by:
Dear All, When I run an example program on the http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/data/datagrid1.src&file=VB\datagrid1.aspx&font=3 ...
2
by: Grant | last post by:
Hi, I keep getting the following error message: InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first. I have read up...
5
by: petro | last post by:
Hello all, My asp.net web application works on my machine but I get the following error on our test web server, There is only one oracle home on the test server. Does anyone know how to resolve...
4
by: dan | last post by:
The following exception has been thrown by a VB.NET program: "System.InvalidOperationException Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
0
by: nicomp | last post by:
I created a Web Service: I imported System.Data.SqlClient so I could access SQL server tables programmatically. The web service builds and deploys with no problems. When I try to add the...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
0
by: Elmo Watson | last post by:
I have a class - I Dim a SQLconnection, then, in Sub New, I instantiate it, so that it's available to me whenever I reference the class (correct?): Conn = New SqlConnection(msConn) Then, in my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.