473,788 Members | 2,868 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 2171
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
14855
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
2509
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
5899
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 understand the subtle details or use the obscure features of either language
22
3770
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 requested class. But WMP has no problem at all playing c:\windows\system32\ ALSNDMGR.WAV. That goes...
5
7388
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 proceedure on it?
34
2815
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 I'd like to know how it is handled by python. I am using it like this:
16
3752
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 for C (although they say C++ can do all the jobs of C).
1
2404
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 palindromes using the algorithm below (see 3. on the next page) and allows the user to try this...
1
1717
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 .htaccess file is: php_value include_path "/bioconstore/lib:/plate-latest"
5
1741
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
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7518
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.