Click here to Skip to main content
15,891,811 members
Please Sign up or sign in to vote.
4.80/5 (2 votes)
See more:
I have an VB.NET application. This Application is permanently resident in memory and minimized to the tray area because we use it very often. The problem comes in when we VPN into a client's site. These VPN connections trash all other network connections to and from our machines.

Then every time my VB.NET application refreshes its database, errors start occurring, because the machine has lost connectivity to the SQL server, however, these are not at the connection level, but only when the reader attempts to read.

I am checking for connection.open, but because of the SQL connection pooling, I just get a previous connection that was valid before the network was cut. Is there anyway to check if an actual SQL connection is valid, prior to re-issuing a connection from the pool?

I have created a separate app to test a solution to this. (below)
If there is no connection on the first attempt, I get an error, but if the first connection is valid and then I physically disconnect the LAN cable, I still get an alledged successful connection. I need to be sure that this connection is actually valid, or throw an exception that I can trap and handle prior to attemting transactions on the SQL server. If not, I will have to turn the pooling off, which will slow things down a bit :-(
C#
Imports System.Data.SqlClient
Public Class Form1
Public Conn As New SqlConnection

Private Sub OpenConnection
     if conn.state <> 1 then 'actually here the problem ***
        conn.open
     endif
End sub

End Class


*** - actually the connection already broken by disconnecting lan cable, but it shows OPEN
Posted
Updated 21-Aug-16 4:46am
v5

Hi,
For Broken link i hope the following condition will works,
VB
If conn.State = ConnectionState.Broken Then
 'Do your codes
End If


Regards,
BlueSathish
 
Share this answer
 
Comments
Kodeeswaran V Duraisamy 1-Nov-13 1:56am    
no, its not working - conn.state still shows open but when i read something it gives error! that's the question here.
I think I would just keep a variable that counts how many times the exception get hits after opening a connection. If it's the first time the exception has been hit, instead of erroring out it would try again. The second time it would assume the error was something else and show me the error message.

----------- Update ----------------

Here is roughly what I am talking about:
VB
Dim intErrorCount As Integer = 0
Try
    'Perform a read
Catch ex As Exception
    Select Case intErrorCount
        Case 0
            'Reopen the connection
        Case Else
            'Handle the error
            MsgBox("Error: " & ex.Message)
    End Select
    intErrorCount += 1
End Try


Why does this not work for you? What is happening when you try to access the DB and the connection isn't really open? It just errors out, right?
 
Share this answer
 
v2
Comments
Kodeeswaran V Duraisamy 30-Oct-13 0:43am    
i've update the question - pls go through it.
Kschuler 30-Oct-13 9:53am    
I updated my solution.
I use some trick to solve this by

VB
Private Sub OpenConnection
      Keep_Alive(conn)
End sub

Private Sub Keep_Alive(ByRef Conn As SqlConnection)
    On Error Resume Next
    Conn.Open()
End Sub
 
Share this answer
 
Change 192.168.1.125 to your IP

VB.NET
Imports System.Net
Imports System.Net.Sockets

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If My.Computer.Network.Ping("192.168.1.125") = True Then
            MsgBox("Ping echo")
        Else
            MsgBox("No ping echo")
        End If
    End Sub


    Private Function IsSQLServerAvailable(ByVal ipaddress As String, ByVal Port As String) As Boolean
        Try
            Dim tcpCli1 As New TcpClient
            tcpCli1.SendTimeout = 15
            tcpCli1.Connect(ipaddress, Port)
            tcpCli1.Close()
            tcpCli1 = Nothing
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If IsSQLServerAvailable("192.168.1.125", "1433") = True Then
            MsgBox("Tested OK")
        Else
            MsgBox("Tested Not OK")
        End If
    End Sub
End Class
 
Share this answer
 
Comments
Richard MacCutchan 21-Aug-16 10:58am    
This was answered almost 3 years ago. Please do not post in old questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900