Re: What's wring with this try-catch block?
- From: "HillBilly" <somebody@xxxxxxxxxxxxx>
- Date: Fri, 1 Aug 2008 14:10:34 -0500
I can set breakpoints all over the place --except-- on the try statement. I don't see anything unusual --except-- an the Error List states: The variable 'ex' is declared but never used noting if I change (Exception ex) to (Exception e) I get a different error about the variable already being used in the same scope.
I set breakpoints and I can read the Call Stack and the Locals window. I really don't know what to look for that indicates why the code in the try block will not execute. I can only assume the stupid error about ex which makes no sense to me may be the problem.
I can see the connection string and know its valid and functional. It may not be a best practice but I don't know any other way to determine when the database may be offline so I'm trying to force an exception I could easily catch if and when the database may be offline by using the testConnection.Open() method which will open and close if the database is online but should be caught by the catch block when offline.
This is so strange to me not even this code will allow the redirect in the try block to execute. The click event keeps executing the code in the catch LoginButton_Click-Caught-Exception...
protected void LoginButton_Click(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Clicked"));
if( 1 == 1 )
{
try
{
Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Try-Block"));
}
catch (Exception ex)
{
Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Caught-Exception"));
}
}
}
"rhaazy" <rhaazy@xxxxxxxxx> wrote in message news:0c814066-d310-47c4-bb42-dfbb427ab5c5@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jul 31, 11:51 pm, "HillBilly" <someb...@xxxxxxxxxxxxx> wrote:
This is freaking me out. I'm using Membership and trying to determine if the
database is online. The GetConnectionString( ) method returns a connection
string as expected but not when used in the try block.
I have the Open() and Close() methods commented out while using the
Response.Redirect to test.
The code in the try block as shown will --never execute-- but the code in
the catch block --always executes-- whether the database is online or not.
WTF???
protected void LoginButton_Click(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click"));
SqlConnection testConnection = new
SqlConnection(GetConnectionString("MyConnectionString"));
if (!String.IsNullOrEmpty(testConnection.ToString()))
{
try
{
//testConnection.Open();
//testConnection.Close();
Response.Redirect(ResolveUrl("~/homepage.aspx?MyConnectionString-Found"));
}
catch (Exception ex)
{
Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Caught-Exception"));
}
}
}- Hide quoted text -
- Show quoted text -
Right before your if statement have the app throw a messagebox with
the contents of the string:
MessageBox.Show(testConnection.ToString());
.
- Follow-Ups:
- Re: What's wring with this try-catch block?
- From: Jon Skeet [C# MVP]
- Re: What's wring with this try-catch block?
- References:
- What's wring with this try-catch block?
- From: HillBilly
- Re: What's wring with this try-catch block?
- From: rhaazy
- What's wring with this try-catch block?
- Prev by Date: Re: YHO about Try-Catch use.
- Next by Date: Re: YHO about Try-Catch use.
- Previous by thread: Re: What's wring with this try-catch block?
- Next by thread: Re: What's wring with this try-catch block?
- Index(es):
Loading