Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

try catch block using vb in asp.net

This is probably a simple fix for anyone who knows more about this than I (and that is probably most).

The code below always does the finally part of this try catch block and doesn't appear to do the lines within Catch. What do I have coded wrong or what do I need to change to get this to work.

I get this message. The table 'tblEmployees' is already opened exclusively by another user, or it is already open through the user interface and cannot be manipulated programmatically.

I'm getting this since I have the table open in Access on purpose to throw the error to test the try catch block.

    Try
            objConnection.Open(strConnString)
        Catch ex As Exception
         
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " & ex.ToString
            txtSqlString.Text = strErrorMessage
            Exit Try
           
        Finally
           
            rstemp = objConnection.execute(strSqlCommand)
            GetRecordSet = rstemp
       
        End Try
0
tremak
Asked:
tremak
  • 7
  • 5
  • 5
  • +1
1 Solution
 
amar31282Commented:
try using this.


check if the connection objConnection is opened before opening here and was not closed.

If yes then close the connection there also
Try
            objConnection.Open(strConnString)
        Catch ex As Exception
         
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " & ex.ToString
            txtSqlString.Text = strErrorMessage
                  objConnection.Close();
            
        Finally
            
            rstemp = objConnection.execute(strSqlCommand)
            GetRecordSet = rstemp
        objConnection.Close();
        End Try

Open in new window

0
 
tremakAuthor Commented:
I have Exit Try in my Try block. Isn't that supposed to make it exit and not perform the code the the finally block?

I don't see how your suggested change will work
0
 
käµfm³d 👽Commented:
A Finally block will ALWAYS execute. There is no way to skip it, short of not having it at all.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
tremakAuthor Commented:
So, would there be a way to check the value of the exception and only execute if blanks?

For example,


Finally
            if ex = " "
                 rstemp = objConnection.execute(strSqlCommand)
                 GetRecordSet = rstemp
            end if
0
 
käµfm³d 👽Commented:
Actually, let me amend my previous statement:

You could have some kind of if statement within your Finally block. You could "skip" it that way.

For you situation, something like the following is probably what you are after:
Try
    objConnection.Open(strConnString)
Catch ex As Exception
         
    txtSqlString.Visible = True
    strErrorMessage = "error getting data. Exception was " & ex.ToString
    txtSqlString.Text = strErrorMessage
    objConnection.Close();
    
    Exit Function 'or Sub
End Try
            
rstemp = objConnection.execute(strSqlCommand)
GetRecordSet = rstemp
objConnection.Close();

Open in new window

0
 
amar31282Commented:
the catch block runs only if there happens any exception in try

so use like this

not a good practice but will work for you
Dim i as Integer = 0
 
Try
            objConnection.Open(strConnString)
i=1
        Catch ex As Exception
         
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " & ex.ToString
            txtSqlString.Text = strErrorMessage
              
        Finally
            if i = 1 Then
            rstemp = objConnection.execute(strSqlCommand)
            GetRecordSet = rstemp
        End If
objConnection.Close()
        End Try

Open in new window

0
 
käµfm³d 👽Commented:
No. Because the variable "ex" only exists within the Catch part of a Try/Catch/Finally.
0
 
amar31282Commented:
little extension
Dim i as Integer = 0
 
Try
            objConnection.Open(strConnString)
i=1
        Catch ex As Exception
         
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " &         ex.ToString
            txtSqlString.Text = strErrorMessage
             i=0 
        Finally
            if i = 1 Then
            rstemp = objConnection.execute(strSqlCommand)
            GetRecordSet = rstemp
Else
'Do Else part if want to
        End If
objConnection.Close()
        End Try

Open in new window

0
 
tremakAuthor Commented:
kaufmed. I don't see any if statement in your last posting
0
 
käµfm³d 👽Commented:
I know. I said you "could" do it...  I'm not sure you need to though. Would it be possible for you to post your entire function/sub to better assess what you are trying to accomplish?
0
 
tremakAuthor Commented:
Function GetRecordSet(ByVal objConnection As Object) As Object
         
        Dim strSqlCommand As String
        Dim rstemp As Object
        Dim strConnString As String
        Dim strErrorMessage As String = ""
        Dim bolErrorOccurred As Boolean = False
       
        strSqlCommand = txtSqlString.Text
        objConnection = Server.CreateObject("adodb.connection")
        rstemp = Server.CreateObject("adodb.recordset")
        strConnString = pubConnProvider & pubConnSource
        objConnection.ConnectionTimeout = 5
        Try
            objConnection.Open(strConnString)
        Catch ex As Exception
            bolErrorOccurred = True
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " & ex.ToString
            txtSqlString.Text = strErrorMessage
            Exit Try
           
        Finally
            If bolErrorOccurred Then
                GetRecordSet = Nothing
            Else
                rstemp = objConnection.execute(strSqlCommand)
                GetRecordSet = rstemp
            End If
           
        End Try
    End Function
0
 
amar31282Commented:

Function GetRecordSet(ByVal objConnection As Object) As Object
         
        Dim strSqlCommand As String
        Dim rstemp As Object
        Dim strConnString As String
        Dim strErrorMessage As String = ""
        Dim bolErrorOccurred As Boolean = False
        
        strSqlCommand = txtSqlString.Text
        objConnection = Server.CreateObject("adodb.connection")
        rstemp = Server.CreateObject("adodb.recordset")
        strConnString = pubConnProvider & pubConnSource
        objConnection.ConnectionTimeout = 5
        Try
            objConnection.Open(strConnString)
bolErrorOccurred = False
        Catch ex As Exception
            bolErrorOccurred = True
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " & ex.ToString
            txtSqlString.Text = strErrorMessage
            Exit Try
            
        Finally
            If bolErrorOccurred Then
                GetRecordSet = Nothing
            Else
                rstemp = objConnection.execute(strSqlCommand)
                GetRecordSet = rstemp
            End If
            
        End Try
    End Function

Open in new window

0
 
tremakAuthor Commented:
amar31282:

What did you change? I don't see anything different
0
 
käµfm³d 👽Commented:
Without "if" statement:
 Function GetRecordSet(ByVal objConnection As Object) As Object
         
        Dim strSqlCommand As String
        Dim rstemp As Object
        Dim strConnString As String
        Dim strErrorMessage As String = ""
        Dim bolErrorOccurred As Boolean = False
       
        strSqlCommand = txtSqlString.Text
        objConnection = Server.CreateObject("adodb.connection")
        rstemp = Server.CreateObject("adodb.recordset")
        strConnString = pubConnProvider & pubConnSource
        objConnection.ConnectionTimeout = 5
        Try
            objConnection.Open(strConnString)
        Catch ex As Exception
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " & ex.ToString
            txtSqlString.Text = strErrorMessage
            GetRecordSet = Nothing
            Exit Function
        End Try
 
        GetRecordSet = rstemp
    End Function
	 

Open in new window

0
 
käµfm³d 👽Commented:
Forgot a line, sorry:
 Function GetRecordSet(ByVal objConnection As Object) As Object
         
        Dim strSqlCommand As String
        Dim rstemp As Object
        Dim strConnString As String
        Dim strErrorMessage As String = ""
        Dim bolErrorOccurred As Boolean = False
       
        strSqlCommand = txtSqlString.Text
        objConnection = Server.CreateObject("adodb.connection")
        rstemp = Server.CreateObject("adodb.recordset")
        strConnString = pubConnProvider & pubConnSource
        objConnection.ConnectionTimeout = 5
        Try
            objConnection.Open(strConnString)
        Catch ex As Exception
            txtSqlString.Visible = True
            strErrorMessage = "error getting data. Exception was " & ex.ToString
            txtSqlString.Text = strErrorMessage
            GetRecordSet = Nothing
            Exit Function
        End Try
 
        rstemp = objConnection.execute(strSqlCommand)
        GetRecordSet = rstemp
    End Function

Open in new window

0
 
käµfm³d 👽Commented:
Make sure you are using the last code I posted and not the one you marked solution as I forgot to include line 24 in the first posting.
0
 
amar31282Commented:
bolErrorOccurred = False in try block
0
 
Kelvin McDanielSr. Developer/ConsultantCommented:
Is this an ASP.NET 1.0/1.1 app?
-If so then great.
-If not then you're not using the current constructs or best practices which would make this much simpler.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now