Detecting wirless network status changes in .NET.

I developed a winform application using VB.NET. The application is deployed in a machine that is connected to a wireless network. The car is in a car (moving object).

The application has DataGridViewdownloaded data from MSSQL Server (on a remote computer). Data is updated every 5 seconds.

I used the event NetworkAvailabilityChangedto determine the status of the network. If the network is available, I retrieve the data from the table.

The code:

AddHandler NetworkChange.NetworkAvailabilityChanged, AddressOf NetworkStateChangeHandler

Public Sub NetworkStateChangeHandler(ByVal sender As Object, 
                                     ByVal e As NetworkAvailabilityEventArgs)
        If e.IsAvailable = True Then           
            g_bNetworkAlive = True            
        Else           
            g_bNetworkAlive = False
        End If
End Sub

private Sub GetData()
    If g_bNetworkAlive = True
        'code to get the data from table
    End If
End Sub

Question:

If the car movers out of the out of the network, the event NetworkAvailabilityChangeddoes not start. therefore, it throws the following error every 5 seconds, and the application crashes.

A network-related or instance-specific error occurred while establishing 
a connection to SQL Server. The server was not found or was not accessible.

: Ping SQL Server 5 , . .

. Wi-Fi, NetworkAvailabilityChanged event is fired. , .

- ?

+4
4

, .

HTTP- - -, , Wi-Fi . ManagedWifi API, , .

//Create object at the beginning of your application 
WlanClient wlanClient = new WlanClient();


//You this code to check wifi availability wherever you need
foreach (WlanInterface _interface in wlanClient.Interfaces)
{
    If (_interface.CurrentConnection.wlanAssociationAttributes.dot11Ssid.SSID!=null)
   {
      // You are connected to wifi
   }
}

EDIT: dll, . Dll Voila! .

+2

Private Sub GetData()
    If My.Computer.Network.IsAvailable AndAlso g_bNetworkAlive
        ' code to get the data from table

    End If
End Sub
+1

You can also check if you have an internet connection using WebRequest, for example:

Private Sub GetData()

    If HasInternet AndAlso g_bNetworkAlive Then
        'code to get the data from table
    End If

End Sub

Public Shared Function HasInternet As Boolean

    Return Not (HttpGet("http://www.google.com/") = "Error")

End Function

Public Shared Function HttpGet(url As String) As String
    Dim request As WebRequest = WebRequest.Create(url)
    request.Method = "GET"
    Try
        Dim response As WebResponse = request.GetResponse()
        Dim dataStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()
        Return responseFromServer
    Catch ex As Exception
        Return "Error"
    End Try
End Function

And you can check the sql connection using the following function:

Private Function IsDatabaseConnected() As Boolean
    Try
        Using sqlConn As New SqlConnection("YourConnectionString")
                sqlConn.Open()
                Return (sqlConn.State = ConnectionState.Open)
        End Using
    Catch e1 As SqlException
        Return False
    Catch e2 As Exception
        Return False
    End Try
End Function
+1
source

I would just use it try..catch, so if you get an exception, you can know, based on its identifier, why the code failed, and decide what to do next, and finally, you can execute another code regardless of whether it is retrieved or not

Private Sub GetData()
    Try 
        'code to get the data from table

    Catch ex As Exception
        ' Show the exception message.
        MessageBox.Show(ex.Message)

        ' Show the stack trace, which is a list of methods 
        ' that are currently executing.
        MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
    Finally 
        ' This line executes whether or not the exception occurs.
        MessageBox.Show("in Finally block")
    End Try
End Sub
+1
source

All Articles