Wednesday, March 28, 2012
System.InvalidOperationException: Connection must be valid and open
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
>
>
System.InvalidOperationException: Connection must be valid and open
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();
> >>
> >> }
> >>
>
Tuesday, March 13, 2012
System.Security.SecurityException: That assembly does not allow partially trusted callers.
I developed a web site using .net 2.0, mysql 4.0 and IIS 5.0. The server is hosted by a third party.
I am using the MySql.Data.dll library version 5.0.7.0 to connect to the database. I did not create any custom assembly and I am using the MySqlData objects directly from the code:
using MySql.Data.MySqlClient;
MySqlConnection conn;
MySqlDataAdapter data_retriever;
DataTable TextDataTable;
TextDataTable = new DataTable();
data_retriever = new MySqlDataAdapter(last_query_executed.Value.ToString(), conn);
data_retriever.Fill(TextDataTable);
This is the error I get:
System.Security.SecurityException: That assembly does not allow partially trusted callers.
[SecurityException: That assembly does not allow partially trusted callers.]
search_search.BindData() +0
search_search.Page_Load(Object sender, EventArgs e) +30
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6978
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +213
System.Web.UI.Page.ProcessRequest() +86
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +18
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.search_search_aspx.ProcessRequest(HttpContext context) +4
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +303
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
My level of knowledge of .NET and Visual Studio is not advanced.
Do you have any suggestions?
Thanks,
Christian
Here is a post on the issue. It seems the current server configuration does not allow partial trusted callers.
http://bloggingabout.net/blogs/rick/archive/2006/04/07/11929.aspx -- this may not really apply becaus eyour site is hosted by a 3rd party
http://forums.mysql.com/read.php?38,95478,96748#msg-96748 -- has a link to download a new version of MySql assembly allowing partial trust. Not sure it will work, mixed results
http://forums.asp.net/t/999456.aspx -- has some other solutions. One of which is using a ODBC connection to MySql instead.
http://dev.mysql.com/tech-resources/articles/dotnet/ --see for ODBC driver details
I tried to recompile the library. However, I get a compilation error after I add the following line in the Assemplyinfo.cs:
[assembly: AllowPartialllyTrustedCallers]
Any ideas?
Christian
What error did you get? Also post your AssemblyInfo.cs file.
I fixed this problem, I was missing the following line:
using
System.Security;
however, now i am getting the error:
Unable to connect to any of the specified MySQL hosts.