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

Discussion in 'ASP .Net' started by bienwell, May 27, 2005.

  1. bienwell

    bienwell Guest

    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=Server1"
    Dim strConn as string =
    "server=Server2;uid=UserID2;pwd=Password2;DSN=Server2"
    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
     
    bienwell, May 27, 2005
    #1
    1. Advertising

  2. bienwell

    Ben Guest

    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" <> wrote in message
    news:%...
    >
    > 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=Server1"
    > Dim strConn as string =
    > "server=Server2;uid=UserID2;pwd=Password2;DSN=Server2"
    > 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
    >
    >
     
    Ben, May 27, 2005
    #2
    1. Advertising

  3. bienwell

    Marina Guest

    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" <> wrote in message
    news:%...
    >
    > 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=Server1"
    > Dim strConn as string =
    > "server=Server2;uid=UserID2;pwd=Password2;DSN=Server2"
    > 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
    >
    >
     
    Marina, May 27, 2005
    #3
  4. bienwell

    bienwell Guest

    That's a good idea. I will try it. Thank you.


    "Ben" <> wrote in message
    news:8f681$42973a2a$d8445835$...
    > 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" <> wrote in message
    > news:%...
    > >
    > > 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=Server1"
    > > Dim strConn as string =
    > > "server=Server2;uid=UserID2;pwd=Password2;DSN=Server2"
    > > 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
    > >
    > >

    >
    >
     
    bienwell, May 27, 2005
    #4
  5. bienwell

    bienwell Guest

    Thank you, Marina. I will keep up with your idea.

    "Marina" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%...
    > >
    > > 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=Server1"
    > > Dim strConn as string =
    > > "server=Server2;uid=UserID2;pwd=Password2;DSN=Server2"
    > > 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
    > >
    > >

    >
    >
     
    bienwell, May 27, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    8
    Views:
    4,190
    Xenos
    Aug 20, 2004
  2. iftekhar
    Replies:
    2
    Views:
    392
    iftekhar
    Jan 25, 2006
  3. AB
    Replies:
    2
    Views:
    619
    Marcus Kwok
    Jun 2, 2006
  4. Stuart Redmann
    Replies:
    5
    Views:
    496
    Stuart Redmann
    Dec 14, 2007
  5. Savvoulidis Iordanis
    Replies:
    7
    Views:
    572
    Göran Andersson
    Jul 20, 2008
Loading...

Share This Page