locked
Unhandled exception has occurred in your application RRS feed

  • Question

  • Hello,
    I am running my application and at random times when it is unable to connect to the database I get an error saying  "A Transport-level error has occured when receiving results from the server. (provider, TCP Provider, error: 0- The specified network name is no longer available.)

    How can I stop this pop up from coming up? It comes up every once in a while and I want to disable that.
    Thursday, November 26, 2009 12:23 AM

Answers

  • So it sounds like you have a spotty network connection and your query fails occasionally due to the down times.  You didn't mention if the code that calls this method is in a Try... Catch block so I'll assume its not.  I think the first thing to try is to place this code within such an error handling block so that you have the power to control what happens in your program when the connection fails.  My revised version of code below shows that -- but you may want to find out what the specific exception type is for that error and add a separate catch block just for that exception (place it above the "ex" block because it is more specific).  That way you can catch other exceptions (i.e. DataException) and be able to handle those differently.

    Also note that I have three Using blocks in my example.  I tend not to use class or global scope variables for data operations because there is really no noticable performance hit (other than with very intensive data collection) and you get the assurance that all objects have been closed properly.  Speaking of which...  you have a method that opens the connection, but I don't see where you close it.  This would be very bad if there is no such code.  The Using block will resolve that question.

     
        Private connString As String = "<your connection string here>"
    
        Public Function OrderByDate() As DataSet
    
            Try
                Using sqlCmd As New SqlCommand
                    Using conn As New SqlConnection(connString)
    
                        '* Configure command object
                        sqlCmd.CommandText = "OrderByDate"
                        sqlCmd.CommandType = CommandType.StoredProcedure
                        sqlCmd.Connection = conn
    
                        Dim dsDS As New DataSet
                        Using daDA As New SqlDataAdapter(sqlCmd)
                            conn.Open()
                            daDA.SelectCommand = sqlCmd
                            daDA.Fill(dsDS, "Orders")
                        End Using
    
                        Return dsDS
                    End Using
                End Using
            Catch ex As Exception
                'Handle the network issue exception here...
                Return Nothing
            End Try
    
        End Function
     
     
     
     
     
     
    Thursday, November 26, 2009 1:04 PM

All replies

  • Can you provide a short code example of the code that is generating this exception?  are you ising a Try... Catch block?  If not, then this would catch the exception before you get the run time error.
    Thursday, November 26, 2009 12:31 AM
  • I have the following code running every minute or so.
     Public Function OrderByDate()
    
            Using sqlCmd As New SqlCommand
    
                OpenCon()
    
                '* Configure command object
                sqlCmd.CommandText = "OrderByDate"
                sqlCmd.CommandType = CommandType.StoredProcedure
                sqlCmd.Connection = sqlCon
    
                Dim dsDS As New DataSet
    
                '* Bind results
                daDA.SelectCommand = sqlCmd
                daDA.Fill(dsDS, "Orders")
    
                Return dsDS
    
            End Using
    
    
        End Function
    
    • Marked as answer by TheSQLNewb Thursday, November 26, 2009 1:13 AM
    • Unmarked as answer by TheSQLNewb Thursday, November 26, 2009 1:13 AM
    Thursday, November 26, 2009 12:56 AM
  • So it sounds like you have a spotty network connection and your query fails occasionally due to the down times.  You didn't mention if the code that calls this method is in a Try... Catch block so I'll assume its not.  I think the first thing to try is to place this code within such an error handling block so that you have the power to control what happens in your program when the connection fails.  My revised version of code below shows that -- but you may want to find out what the specific exception type is for that error and add a separate catch block just for that exception (place it above the "ex" block because it is more specific).  That way you can catch other exceptions (i.e. DataException) and be able to handle those differently.

    Also note that I have three Using blocks in my example.  I tend not to use class or global scope variables for data operations because there is really no noticable performance hit (other than with very intensive data collection) and you get the assurance that all objects have been closed properly.  Speaking of which...  you have a method that opens the connection, but I don't see where you close it.  This would be very bad if there is no such code.  The Using block will resolve that question.

     
        Private connString As String = "<your connection string here>"
    
        Public Function OrderByDate() As DataSet
    
            Try
                Using sqlCmd As New SqlCommand
                    Using conn As New SqlConnection(connString)
    
                        '* Configure command object
                        sqlCmd.CommandText = "OrderByDate"
                        sqlCmd.CommandType = CommandType.StoredProcedure
                        sqlCmd.Connection = conn
    
                        Dim dsDS As New DataSet
                        Using daDA As New SqlDataAdapter(sqlCmd)
                            conn.Open()
                            daDA.SelectCommand = sqlCmd
                            daDA.Fill(dsDS, "Orders")
                        End Using
    
                        Return dsDS
                    End Using
                End Using
            Catch ex As Exception
                'Handle the network issue exception here...
                Return Nothing
            End Try
    
        End Function
     
     
     
     
     
     
    Thursday, November 26, 2009 1:04 PM
  • Hey there,
    I will try to add the try and catch in there to see if it solves the problem.  The reason I don't close the connection is that I am connecting to it every minute in which if I close it, I would have to call it up every minute which used to load up an error before because I am opening it / closing it too often.
    Thursday, November 26, 2009 5:06 PM
  • Hello,
    I am running my application and at random times when it is unable to connect to the database I get an error saying  "A Transport-level error has occured when receiving results from the server. (provider, TCP Provider, error: 0- The specified network name is no longer available.)

    How can I stop this pop up from coming up? It comes up every once in a while and I want to disable that.

    for you to avoid open and close all the time try to use it:

    If command.Connection.State = ConnectionState.Open Then
                    CnEt.Close()
                Else
                    If command.Connection.State = ConnectionState.Closed Then
                        CnEt.Open()
                    End If
                End If

    Don't judge me, just Upgrade me. Thanks!
    Thursday, November 26, 2009 8:24 PM
  • I think it may be a mistake to not be closing your connection.  Once every minute is an eternity in computer time and I seriously doubt there are any performance issues arising from this practice.  All best practices will say to close the connection immediately after using it (with a few rare exceptions) because it is a resource that shoudl be used sparingly.  Sql Server is optimized to handle a connection opening and closing frequently - handled through its connection pooling logic. 

    At the very least you could keep the instance of the connection alive but open and close it when needed -- the State property is designed for this type of action.
    Friday, November 27, 2009 12:53 PM