-1

I have developed a .Net application that I want to share to users through shared file in the LAN that is managed through a Domain server. When the server is up and running its fine but when the server is shut down an error comes. I have access to sql server database in that server. When server is shut down it gives a fatal error and application stops. How to detect the error of server being down? I am connecting to the server through the following code:

Dim sql As String
Dim daCourseMaster As SqlDataAdapter

        Try
            sqlConn = New SqlConnection(connString)
            sqlConn.Open()

            sql = " ... "

            daCourseMaster = New SqlDataAdapter(sql, sqlConn)
            daCourseMaster.Fill(dsDataset, "table")
            dvRegister = New DataView
            dvRegister = dsDataset.Tables("courseRegisters").DefaultView
            dgvCourseRegister.DataSource = dvRegister


        Catch ex As Exception
            MsgBox(Err.Description)
        End Try

Now there might be different reasons why user can not login to the server: either the server name is wrong or user name or password is wrong or thebserver might be shut down! How to differentiate between these reasons to let the user know what might be the reason to failed login?

Hilal Al-Rajhi
  • 437
  • 6
  • 25
  • 49

2 Answers2

1

Lets say, you need to query your database. When you open connection and then when you execute your query, use Try-Catch Block and intercept SqlException. It has different codes, so you can test it for connectivity -related codes

Try

    Using conn as new SqlConnection(...)
        conn.Open()
        ....
    End Using
Catch (connEx as SqlException)
   ' sql - related error handling
Catch (ex as Exception)
   ' here handle all other exception
End Try

this way, you can let user know that connection has lost and "try again later" or something like this

T.S.
  • 18,195
  • 11
  • 58
  • 78
1

Use this code before the code that do the sharing (if the code in a Sub):

 If Not My.Computer.Network.Ping(" You Server Name or Server IP") Then Exit Sub

or (if the code in a Function):

 If Not My.Computer.Network.Ping(" You Server Name or Server IP") Then Exit Function
Ibra
  • 192
  • 11