Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - What's wring with this try-catch block?

Reply
 
Old 07-31-2008   #1 (permalink)
HillBilly


 
 

What's wring with this try-catch block?

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"));
}
}
}


My System SpecsSystem Spec
Old 08-01-2008   #2 (permalink)
Cor Ligthert[MVP]


 
 

Re: What's wring with this try-catch block?

Why not simple set a breakpoint behind this part of code and see what it is?

testConnection.ToString())


"HillBilly" <somebody@xxxxxx> schreef in bericht
news:urnrWn48IHA.4088@xxxxxx
Quote:

> 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"));
> }
> }
> }
My System SpecsSystem Spec
Old 08-01-2008   #3 (permalink)


Vista Business x64
 
 

Re: What's wring with this try-catch block?

I'd have to look this up - I know a Server.Redirect that occurs within the try of a try..catch block throws a thread termination exception. Perhaps Response.Redirect does as well. To fix it, just use Response.Redirect(url,false) instead, or you can throw an extra chunk between your try and catch blocks:

catch (System.Threading.ThreadAbortException ex)
{
//Do nothing }

My System SpecsSystem Spec
Old 08-02-2008   #4 (permalink)
HillBilly


 
 

Re: What's wring with this try-catch block?


"breitak67" <guest@xxxxxx-email.com> wrote in message
news:191e700f3beb17bd2df5d7fe780b4959@xxxxxx-gateway.com...
Quote:

>
> I'd have to look this up - I know a Server.Redirect that occurs within
> the try of a try..catch block throws a thread termination exception.
> Perhaps Response.Redirect does as well. To fix it, just use
> Response.Redirect(url,false) instead, or you can throw an extra chunk
> between your try and catch blocks:
>
> catch (System.Threading.ThreadAbortException ex)
> {//Do nothing }
>
>
> --
> breitak67
Thank you, that's correct as I'm learning. How would you determine if the
database is offline or not?

My System SpecsSystem Spec
Old 08-02-2008   #5 (permalink)


Vista Business x64
 
 

Re: What's wring with this try-catch block?

You try to connect to it - if it fails, you inform the user you can't get to the database. You can catch specific error types and check the error details in your try.catch block.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
After wring CD appears blank Vista hardware & devices
Is try-catch block a way to prevent a crash? .NET General
Catch-all PowerShell
How to TRY and CATCH PowerShell
I Catch VI Vista hardware & devices


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46