473,545 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GetConnectionSt ring( ) 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.Redire ct 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_Cli ck(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k"));

SqlConnection testConnection = new
SqlConnection(G etConnectionStr ing("MyConnecti onString"));
if (!String.IsNull OrEmpty(testCon nection.ToStrin g()))
{
try
{
//testConnection. Open();
//testConnection. Close();
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?M yConnectionStri ng-Found"));
}
catch (Exception ex)
{
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Caught-Exception"));
}
}
}

Aug 1 '08 #1
16 2142
Why not simple set a breakpoint behind this part of code and see what it is?

testConnection. ToString())
"HillBilly" <so******@somew here.comschreef in bericht
news:ur******** ******@TK2MSFTN GP03.phx.gbl...
This is freaking me out. I'm using Membership and trying to determine if
the database is online. The GetConnectionSt ring( ) 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.Redire ct 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_Cli ck(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k"));

SqlConnection testConnection = new
SqlConnection(G etConnectionStr ing("MyConnecti onString"));
if (!String.IsNull OrEmpty(testCon nection.ToStrin g()))
{
try
{
//testConnection. Open();
//testConnection. Close();

Response.Redire ct(ResolveUrl(" ~/homepage.aspx?M yConnectionStri ng-Found"));
}
catch (Exception ex)
{

Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Caught-Exception"));
}
}
}
Aug 1 '08 #2
On Jul 31, 11:51*pm, "HillBilly" <someb...@somew here.comwrote:
This is freaking me out. I'm using Membership and trying to determine if the
database is online. The GetConnectionSt ring( ) 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.Redire ct 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_Cli ck(object sender, EventArgs e)
{
* * // Redirect proves this test works
* * //Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k"));

* * SqlConnection testConnection = new
SqlConnection(G etConnectionStr ing("MyConnecti onString"));
* * if (!String.IsNull OrEmpty(testCon nection.ToStrin g()))
* * {
* * * * try
* * * * {
* * * * * * //testConnection. Open();
* * * * * * //testConnection. Close();
* * * * * * Response.Redire ct(ResolveUrl(" ~/homepage.aspx?M yConnectionStri ng-Found"));
* * * * }
* * * * catch (Exception ex)
* * * * {
* * * * * * Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Caught-Exce*ption"));
* * * * }
* * }

}- 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());
Aug 1 '08 #3
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_Cli ck-Caught-Exception...

protected void LoginButton_Cli ck(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic ked"));

if( 1 == 1 )
{
try
{
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Try-Block"));

}
catch (Exception ex)
{
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Caught-Exception"));
}
}

}
"rhaazy" <rh****@gmail.c omwrote in message
news:0c******** *************** ***********@s50 g2000hsb.google groups.com...
On Jul 31, 11:51 pm, "HillBilly" <someb...@somew here.comwrote:
This is freaking me out. I'm using Membership and trying to determine if
the
database is online. The GetConnectionSt ring( ) 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.Redire ct 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_Cli ck(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k"));

SqlConnection testConnection = new
SqlConnection(G etConnectionStr ing("MyConnecti onString"));
if (!String.IsNull OrEmpty(testCon nection.ToStrin g()))
{
try
{
//testConnection. Open();
//testConnection. Close();
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?M yConnectionStri ng-Found"));
}
catch (Exception ex)
{
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Caught-Exce*ption"));
}
}

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

Aug 1 '08 #4

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.Redire ct does as well. To fix it, just use
Response.Redire ct(url,false) instead, or you can throw an extra chunk
between your try and catch blocks:

catch (System.Threadi ng.ThreadAbortE xception ex)
{//Do nothing }
--
breitak67
Aug 2 '08 #5
HillBilly <so******@somew here.comwrote:
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_Cli ck-Caught-Exception...

protected void LoginButton_Cli ck(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic ked"));

if( 1 == 1 )
{
try
{
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Try-Block"));

}
catch (Exception ex)
{
Response.Redire ct(ResolveUrl(" ~/homepage.aspx?L oginButton_Clic k-Caught-Exception"));
}
}
}
Response.Redire ct works by setting something and then throwing an
exception (by default). If you look at the exception which is being
thrown, I susect you'll find it's a ThreadAbortExce ption. Look at the
documentation for Response.Redire ct for more information.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 2 '08 #6

"breitak67" <gu***@unknow n-email.comwrote in message
news:19******** *************** *******@nntp-gateway.com...
>
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.Redire ct does as well. To fix it, just use
Response.Redire ct(url,false) instead, or you can throw an extra chunk
between your try and catch blocks:

catch (System.Threadi ng.ThreadAbortE xception ex)
{//Do nothing }
--
breitak67
Thank you, that's correct as I'm learning. How would you determine if the
database is offline or not?

Aug 2 '08 #7
<snip />
>
Response.Redire ct works by setting something and then throwing an
exception (by default). If you look at the exception which is being
thrown, I susect you'll find it's a ThreadAbortExce ption. Look at the
documentation for Response.Redire ct for more information.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aha! I'll certainly study that thank you but still, what do you do to
determine if the database is offline? I thought I'd convert the code into a
method called before logging in or any other attempt to access the database.
But if its not even online then what?

Aug 2 '08 #8
HillBilly <so******@somew here.comwrote:
Response.Redire ct works by setting something and then throwing an
exception (by default). If you look at the exception which is being
thrown, I susect you'll find it's a ThreadAbortExce ption. Look at the
documentation for Response.Redire ct for more information.

Aha! I'll certainly study that thank you but still, what do you do to
determine if the database is offline? I thought I'd convert the code into a
method called before logging in or any other attempt to access the database.
But if its not even online then what?
Trying to connect (and executing some basic operation) seems reasonable
to me - but unconnected to the problem you're seeing. I suggest you
extract the desired functionality (basically an "online/offline"
boolean check) into a separate method - that way you'll be catching
just the database exception there, rather than both the database
exception *and* the exception from the redirect.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 2 '08 #9

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.
--
breitak67
Aug 2 '08 #10

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

Similar topics

125
14575
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
11
2479
by: Sims | last post by:
Hi, I have a function that looks something like .... void Function( std::string &oldval ) { int iSome_size = xxx; // get some size char *tmp = NULL; tmp = new char;
72
5755
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to...
22
3720
by: MLH | last post by:
I have some audio help files that play fine from within Access 97 and Access 2.0. Both are running on a Windows XP box. But I do not know what program plays the files. If I click Start, Run and type in c:\MyApp\MyHelp\Help1.wav, Windows Media Player tries unsuccessfully to play the wav file. It err's saying ClassFactory cannot supply the...
5
7377
by: Peter | last post by:
Team VB.net, I have multiple programs that need a tool to count lines in a text files. I have been solving this problem through writing proceedures but I hate cutting and pasting code. Is there an easy way of making a "module" or object that I would be able to pass a string variable "c:\MyTextFile" to and then have it run this...
34
2774
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what happens if the number of lines is huge? I have a very big file (4GB) I want to read in, but I'm sure there must be some limitation to readlines and...
16
3713
by: Matthew Zhou | last post by:
I am a students learning programming, and want to do some software projects to practice myself. However, no one will only use one language to make all the tasks done. And every languages has its strong and relatively weak side. So, what about C? Many friends of mine suggest me move to C++ or Java. But I there must be some places reserved...
1
2387
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be great. I know its sorta long, but maybe you only have to few the first few paragraphs. Problem: Write a program in C that generates number...
1
1708
by: Sean128 | last post by:
Hi, I hope someone can help me - this seems like it should be a really simple problem but I've been having trouble trying to solve it all week. I have hosted my website on a Linux server in 1 and 1 that I can not modefiy the Apache server. They told me I need to create the .htaccess file. The code that I put in my Biocon folder in my...
5
1724
by: Max Ivanov | last post by:
Hi all! When and where I should use try-except-finally statement? What is the difference between: -------- try: A... except: B.... finally: C...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.