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: Collection was modified; enumeration operation may not e
hi all,
i am getting the following error when i am trying to move the ltems form listbox1 to listbox2
"System.InvalidOperationException: Collection was modified; enumeration operation may not execute"
ForEach itemAs ListItemIn Listbox1.ItemsIf item.SelectedAndNot Listbox2.Items.Contains(item)Then
Dim newItemAsNew ListItem(item.Text, item.Value)Listbox2.Items.Add(newItem)
listbox1.Remove(newitem)
EndIf
Next
thanks.
You can't modify the listbox1 item collection while you are iterating over it using "for each". Wait until you add everything to listbox2, and then call Clear() on listbox1.System.InvalidOperationException: Application is restarting
I am getting the following exception in an ASP.NET
application.
Exception Details: System.InvalidOperationException:
Application is restarting.
I have been to the following Support page
http://support.microsoft.com/default.aspx?kbid=312592
and disabled the Realtime monitoring. But, I was not able
to solve the issue.
Any suggestions are greatly appreciated.
Thanks a lot in advance,
Regards
HariHi,
There are vast spectrums of situations that can cause application
restarting. I suggest that you find first the line that causes
application to restart. Start with simple application to check out if
this problem is application specific. If this is the case and other
application doesnt cause recycling you should find the application line
that cause recycling.
Natty Gur[MVP]
blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Thanks Natty for your prompt reply. Unfortunately I don't
have the real access to the code at the moment and I am
kind of new to ASP.NET. But, I understand the technology
well.
The following is the exception I was getting when this
error occurs: Please have a look if it makes sense. Any
advice from your side is greatly respected.
************************************************** *******
Source=System.Web
Message=Application is
restarting.
StackTrace= at
System.Web.Compilation.CompilationMutex.WaitOne()
+282
at System.Web.Compilation.CompilationLock.GetLock()
+13
at System.Web.UI.TemplateParser.GetParserCacheItem()
+64
at
System.Web.UI.TemplateControlParser.CompileAndGetP arserCac
heItem(String
virtualPath, String inputFile, HttpContext context)
+115
at
System.Web.UI.TemplateControlParser.GetCompiledIns tance
(String
virtualPath, String inputFile, HttpContext context)
+36
at System.Web.UI.PageParser.GetCompiledPageInstance
(String
virtualPath, String inputFile, HttpContext context) +43
at System.Web.UI.PageHandlerFactory.GetHandler
(HttpContext context,
String requestType, String url, String path) +14
at System.Web.HttpApplication.MapHttpHandler
(HttpContext context,
String requestType, String path, String pathTranslated,
Boolean
useAppConfig)
at System.Web.MapHandlerExecutionStep.Execute()
+78
at System.Web.HttpApplication.ExecuteStep
(IExecutionStep step,
Boolean& completedSynchronously) +173
AllException=System.InvalidOperationException:
Application is
restarting.
at System.Web.Compilation.CompilationMutex.WaitOne
()
at System.Web.Compilation.CompilationLock.GetLock
()
at System.Web.UI.TemplateParser.GetParserCacheItem
()
at
System.Web.UI.TemplateControlParser.CompileAndGetP arserCac
heItem(String
virtualPath, String inputFile, HttpContext
context)
at
System.Web.UI.TemplateControlParser.GetCompiledIns tance
(String
virtualPath, String inputFile, HttpContext
context)
at System.Web.UI.PageParser.GetCompiledPageInstance
(String
virtualPath, String inputFile, HttpContext
context)
at System.Web.UI.PageHandlerFactory.GetHandler
(HttpContext context,
String requestType, String url, String
path)
at System.Web.HttpApplication.MapHttpHandler
(HttpContext context,
String requestType, String path, String pathTranslated,
Boolean
useAppConfig)
at System.Web.MapHandlerExecutionStep.Execute
()
at System.Web.HttpApplication.ExecuteStep
(IExecutionStep step,
Boolean&
completedSynchronously)
************************************************** *******
Any advice on this is greatly appreciated
Thanks you very much
Best Regards
Hari
>--Original Message--
>Hi,
>There are vast spectrums of situations that can cause
application
>restarting. I suggest that you find first the line that
causes
>application to restart. Start with simple application to
check out if
>this problem is application specific. If this is the
case and other
>application doesn't cause recycling you should find the
application line
>that cause recycling.
>Natty Gur[MVP]
>blog : http://weblogs.asp.net/ngur
>Mobile: +972-(0)58-888377
>
>*** Sent via Developersdex http://www.developersdex.com
***
>Don't just participate in USENET...get rewarded for it!
>.
Hi,
Nothing special with excaption stuck. it's regular operations that
called while loading a page. try to delete the temporary directory of
your application under
""C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\Tempo rary ASP.NET
Files\YourApplicationName".
Natty Gur[MVP]
blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Hi,
Nothing special with excaption stuck. it's regular operations that
called while loading a page. try to delete the temporary directory of
your application under
""C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\Tempo rary ASP.NET
Files\YourApplicationName".
Natty Gur[MVP]
blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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();
> >>
> >> }
> >>
>
System.InvalidOperationException: Timeout expired when calling a ASP.NET service
I get this error when calling an ASP.NET service. Any idea what this is and how to prevent it?
System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
Well I do see someone put recycle threads at 1740 minutes...which is normally 20minutes. Do you think that could cause the problem?
No. That's not the problem. If you're leaking connections you must fix that. The default recycle is set at 1740 iirc for IIS 6, not 20 minutes. You do not want to recycle every 20 minutes! That's like getting a bigger bucket to scoop out the water of your boat instead of fixing the holes. It'll work for a time, but sooner or later you'll sink anyway.
Fix the code.
I can see where the SQL connections were not closed. So I added the close code as well. But these are one time hits to a service so I see no reason why the worker processes could not be recycled sooner and save on any memory of any "hung" threads.
If you have hung threads you should ensure that they do not hang. Killing the entire worker process is sort of like than swatting flies with a sledgehammer, you'll mostly miss and in most cases cause more harm in the process of trying than was originally caused by the problem. (This seems to be my day for analogies... ;-) Depending on process recycling to fix problems that should be fixed in code is, well, just simply bad. In the best case you'll have less performance than you'd get otherwise (this may not be a concern by itself, if you have capacity to spare but...) in the worst case, which is the common case, you'll get an unstable site that sometimes works, and sometimes do not. Also - if you're using sessions stored in process, the users will loose these sessions at the time of the recycle - in effect, every 20 minutes all the currently active users will loose their session. That's not so good.
If you have anything cached - you loose it. You'll have to reload the entire AppDomain. All in all - worker process recycling is pretty costly. Don't use it as a way to hide the bugs in your code!
You should engineer your site to be able to run forever. You may for example check this link:http://www.asp.net/faq/AspNetAndIIS6.aspx - note that automatic process recycling is not recommended! (There are some various views on this subject, but you'll find no-one recommending it every 20 minutes for any reason!)
Reactive process recycling, i.e. as a response to an error condition, is ok since it increases the application up-time - but should always be accompanied by an alarm, so that the root cause can be eliminated.
I just talked with the guy that wrote the code causing the problem as I still close connections and try and manage memory like an old ASP programmer. But he and I attended classes on asp.net where Microsoft reps told us there is no need to close SQL connections or do any of this stuff as asp.net as automatic garbage collection and automatically cleans itself up from all of this.
But what this shows is, we are right back to the point where a users bad code can take down the entire web server...not a great feeling.
Don't know what the Microsoft reps were taking, but unfortunately they were wrong and you are right in more ways than one.
Garbage collection does remove the risk for memory leaks, unfortunately it introduces the twofold problems of indeterminite response time (you can't know when a garbage collection may be required, thus cannot guarantee a minimum response time which makes the whole platform more or less useless for anything approaching real-time scenarios) and memory fragmentation in the large object heap in the end leading to out of memory conditions (one of the few reasons you may in fact have to recycle).
Garbage collection does not solve the problem of unmanaged resources, nor does it solve the problem of untimely shortage of some resources. The database pool connections will in fact be reclaimed at some point by garbage collection, but shortage of such a resource does not trigger a forced gc to that will ensure any loose ones to be reclaimed - causing the effects you noted.
So - If an object implements IDisposable, you're stuck with managing it and ensuring it's timely disposal just like in the old days. But there is the nice 'using' statement to help you with that chore... ;-)