Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can I have a second TRY inside the first TRY/CATCH in ASP.NET ???

Reply
Thread Tools

Can I have a second TRY inside the first TRY/CATCH in ASP.NET ???

 
 
bienwell
Guest
Posts: n/a
 
      05-27-2005

Hi all,

I'd like to use TRY, CATCH to open my database connection. I have 2 servers
that have the same table name "myTable". If the first connection to Server1
is failed, then the program will try to connect to Server2. Can I have the
secong Try inside the first Catch for this purpose ?. Please take a look on
my code and fix it if possible. Actually with this code, the database
connection with Server2 is opened after the first connection to Server1 is
failed.

Thanks in advance .


Dim myStr As String ="Select * from myTable"
Dim myConnection as ODBCConnection
Dim myCommand as ODBCCommand
Dim myRec as ODBCDataReader
Dim strConn as string =
"server=Server1;uid=UserID1;pwd=Password1;DSN=Serv er1"
Dim strConn as string =
"server=Server2;uid=UserID2;pwd=Password2;DSN=Serv er2"
Try
myConnection = New ODBCConnection(strConn)
myConnection.Open()
myCommand = New OdbcCommand(myStr, myConnection)
myCommand.ExecuteNonQuery()
myConnection.Close
Dim myDataSet as DataSet=GetDataSource()
myDataList.DataSource = myDataSet
myDataList.DataBind()
Catch Exc1 As Exception
Try
myConnection = New ODBCConnection(strConn2)
myConnection.Open()
myCommand = New OdbcCommand(myStr, myConnection)
myCommand.ExecuteNonQuery()
myConnection.Close
Dim myDataSet as DataSet=GetDataSource()
myDataList.DataSource = myDataSet
myDataList.DataBind()
Catch Exc2 As Exception
response.write("<BR>Database connection is failed ..")
End Try
End Try


 
Reply With Quote
 
 
 
 
Ben
Guest
Posts: n/a
 
      05-27-2005
Your syntax looks ok. Have you considered writing a method to return an
open connection? That way you're code doesn't get junked up with try catch
blocks all over the place?

"bienwell" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> Hi all,
>
> I'd like to use TRY, CATCH to open my database connection. I have 2
> servers
> that have the same table name "myTable". If the first connection to
> Server1
> is failed, then the program will try to connect to Server2. Can I have the
> secong Try inside the first Catch for this purpose ?. Please take a look
> on
> my code and fix it if possible. Actually with this code, the database
> connection with Server2 is opened after the first connection to Server1 is
> failed.
>
> Thanks in advance .
>
>
> Dim myStr As String ="Select * from myTable"
> Dim myConnection as ODBCConnection
> Dim myCommand as ODBCCommand
> Dim myRec as ODBCDataReader
> Dim strConn as string =
> "server=Server1;uid=UserID1;pwd=Password1;DSN=Serv er1"
> Dim strConn as string =
> "server=Server2;uid=UserID2;pwd=Password2;DSN=Serv er2"
> Try
> myConnection = New ODBCConnection(strConn)
> myConnection.Open()
> myCommand = New OdbcCommand(myStr, myConnection)
> myCommand.ExecuteNonQuery()
> myConnection.Close
> Dim myDataSet as DataSet=GetDataSource()
> myDataList.DataSource = myDataSet
> myDataList.DataBind()
> Catch Exc1 As Exception
> Try
> myConnection = New ODBCConnection(strConn2)
> myConnection.Open()
> myCommand = New OdbcCommand(myStr, myConnection)
> myCommand.ExecuteNonQuery()
> myConnection.Close
> Dim myDataSet as DataSet=GetDataSource()
> myDataList.DataSource = myDataSet
> myDataList.DataBind()
> Catch Exc2 As Exception
> response.write("<BR>Database connection is failed ..")
> End Try
> End Try
>
>



 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      05-27-2005
I would probably put each set of code in the try blocks in its own function.
Have the function return a boolean that says whether or not the function
suceeded. Inside the function is the try/catch, return True in the Try,
False in the catch.

You first call the first function. If it returns True, you are done.
Otherwise you call the second function. If it return True, you are done,
otherwise you display an error message.

In theory you can nest try/catch blocks all you want, but your code is going
to be come very difficult to read and debug.

"bienwell" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> Hi all,
>
> I'd like to use TRY, CATCH to open my database connection. I have 2
> servers
> that have the same table name "myTable". If the first connection to
> Server1
> is failed, then the program will try to connect to Server2. Can I have the
> secong Try inside the first Catch for this purpose ?. Please take a look
> on
> my code and fix it if possible. Actually with this code, the database
> connection with Server2 is opened after the first connection to Server1 is
> failed.
>
> Thanks in advance .
>
>
> Dim myStr As String ="Select * from myTable"
> Dim myConnection as ODBCConnection
> Dim myCommand as ODBCCommand
> Dim myRec as ODBCDataReader
> Dim strConn as string =
> "server=Server1;uid=UserID1;pwd=Password1;DSN=Serv er1"
> Dim strConn as string =
> "server=Server2;uid=UserID2;pwd=Password2;DSN=Serv er2"
> Try
> myConnection = New ODBCConnection(strConn)
> myConnection.Open()
> myCommand = New OdbcCommand(myStr, myConnection)
> myCommand.ExecuteNonQuery()
> myConnection.Close
> Dim myDataSet as DataSet=GetDataSource()
> myDataList.DataSource = myDataSet
> myDataList.DataBind()
> Catch Exc1 As Exception
> Try
> myConnection = New ODBCConnection(strConn2)
> myConnection.Open()
> myCommand = New OdbcCommand(myStr, myConnection)
> myCommand.ExecuteNonQuery()
> myConnection.Close
> Dim myDataSet as DataSet=GetDataSource()
> myDataList.DataSource = myDataSet
> myDataList.DataBind()
> Catch Exc2 As Exception
> response.write("<BR>Database connection is failed ..")
> End Try
> End Try
>
>



 
Reply With Quote
 
bienwell
Guest
Posts: n/a
 
      05-27-2005
That's a good idea. I will try it. Thank you.


"Ben" <(E-Mail Removed)> wrote in message
news:8f681$42973a2a$d8445835$(E-Mail Removed)...
> Your syntax looks ok. Have you considered writing a method to return an
> open connection? That way you're code doesn't get junked up with try

catch
> blocks all over the place?
>
> "bienwell" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> >
> > Hi all,
> >
> > I'd like to use TRY, CATCH to open my database connection. I have 2
> > servers
> > that have the same table name "myTable". If the first connection to
> > Server1
> > is failed, then the program will try to connect to Server2. Can I have

the
> > secong Try inside the first Catch for this purpose ?. Please take a look
> > on
> > my code and fix it if possible. Actually with this code, the database
> > connection with Server2 is opened after the first connection to Server1

is
> > failed.
> >
> > Thanks in advance .
> >
> >
> > Dim myStr As String ="Select * from myTable"
> > Dim myConnection as ODBCConnection
> > Dim myCommand as ODBCCommand
> > Dim myRec as ODBCDataReader
> > Dim strConn as string =
> > "server=Server1;uid=UserID1;pwd=Password1;DSN=Serv er1"
> > Dim strConn as string =
> > "server=Server2;uid=UserID2;pwd=Password2;DSN=Serv er2"
> > Try
> > myConnection = New ODBCConnection(strConn)
> > myConnection.Open()
> > myCommand = New OdbcCommand(myStr, myConnection)
> > myCommand.ExecuteNonQuery()
> > myConnection.Close
> > Dim myDataSet as DataSet=GetDataSource()
> > myDataList.DataSource = myDataSet
> > myDataList.DataBind()
> > Catch Exc1 As Exception
> > Try
> > myConnection = New ODBCConnection(strConn2)
> > myConnection.Open()
> > myCommand = New OdbcCommand(myStr, myConnection)
> > myCommand.ExecuteNonQuery()
> > myConnection.Close
> > Dim myDataSet as DataSet=GetDataSource()
> > myDataList.DataSource = myDataSet
> > myDataList.DataBind()
> > Catch Exc2 As Exception
> > response.write("<BR>Database connection is failed ..")
> > End Try
> > End Try
> >
> >

>
>



 
Reply With Quote
 
bienwell
Guest
Posts: n/a
 
      05-27-2005
Thank you, Marina. I will keep up with your idea.

"Marina" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I would probably put each set of code in the try blocks in its own

function.
> Have the function return a boolean that says whether or not the function
> suceeded. Inside the function is the try/catch, return True in the Try,
> False in the catch.
>
> You first call the first function. If it returns True, you are done.
> Otherwise you call the second function. If it return True, you are done,
> otherwise you display an error message.
>
> In theory you can nest try/catch blocks all you want, but your code is

going
> to be come very difficult to read and debug.
>
> "bienwell" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> >
> > Hi all,
> >
> > I'd like to use TRY, CATCH to open my database connection. I have 2
> > servers
> > that have the same table name "myTable". If the first connection to
> > Server1
> > is failed, then the program will try to connect to Server2. Can I have

the
> > secong Try inside the first Catch for this purpose ?. Please take a look
> > on
> > my code and fix it if possible. Actually with this code, the database
> > connection with Server2 is opened after the first connection to Server1

is
> > failed.
> >
> > Thanks in advance .
> >
> >
> > Dim myStr As String ="Select * from myTable"
> > Dim myConnection as ODBCConnection
> > Dim myCommand as ODBCCommand
> > Dim myRec as ODBCDataReader
> > Dim strConn as string =
> > "server=Server1;uid=UserID1;pwd=Password1;DSN=Serv er1"
> > Dim strConn as string =
> > "server=Server2;uid=UserID2;pwd=Password2;DSN=Serv er2"
> > Try
> > myConnection = New ODBCConnection(strConn)
> > myConnection.Open()
> > myCommand = New OdbcCommand(myStr, myConnection)
> > myCommand.ExecuteNonQuery()
> > myConnection.Close
> > Dim myDataSet as DataSet=GetDataSource()
> > myDataList.DataSource = myDataSet
> > myDataList.DataBind()
> > Catch Exc1 As Exception
> > Try
> > myConnection = New ODBCConnection(strConn2)
> > myConnection.Open()
> > myCommand = New OdbcCommand(myStr, myConnection)
> > myCommand.ExecuteNonQuery()
> > myConnection.Close
> > Dim myDataSet as DataSet=GetDataSource()
> > myDataList.DataSource = myDataSet
> > myDataList.DataBind()
> > Catch Exc2 As Exception
> > response.write("<BR>Database connection is failed ..")
> > End Try
> > End Try
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
can't get the text from a div, neither when I try to use innerHTML or when I try to walk the DOM Jake Barnes Javascript 2 04-07-2006 02:42 AM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
Try, Try, Try, again... Rick12N4@netscape.net Computer Support 3 01-29-2005 04:02 PM
Re: ProcessPostData Second Try - Can I force PostData to be processed myself John Saunders ASP .Net 0 07-23-2003 11:11 AM



Advertisments