Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to tell if on internet or not and switch to local SQL db if not

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have an app on my tablet PC. its written in ASP.net 4.5 using vb.net
When I am not able to connect to the internet I would like it to use the local database.
How is the easiest way to determine if it can conenct to the WEB database on a hosted server or not, then switch to the local connection?

Code:
If  can connect to web db then
 Public gblConnectionString = ConfigurationManager.ConnectionStrings("WEBConnectionString").ConnectionString
Else
    'Public gblConnectionString = ConfigurationManager.ConnectionStrings("LocalConnection").ConnectionString
End if

DougP
 
Create an HTTPWebRequest object and try to hit a website. Look for the HTTPResponse that comes back.
 
Ok so I got this
but what am I looking for?
If I use myReq.ContentLength I get -1 whether its a valid URL or not
for Instance I used something that does not exist
"Your requested host "somejunkyplace.com" could not be resolved by DNS."
and still got -1.
Code:
 Dim myReq As HttpWebRequest = _
            WebRequest.Create("[URL unfurl="true"]http://www.mysite.com/")[/URL]

    If myReq.????? = something then
        ' we are conencted
   Else
       'we are not
   end if

DougP
 
Ok I got this to work. to test it put in both a "REAL" and invalid WEB site.
Code:
 Public Function AreWeOnLine() As Boolean
        Dim OnLineStatus As Boolean
        Try
            Dim myReq As HttpWebRequest = _
               WebRequest.Create("[URL unfurl="true"]http://www.ValidWEBSite.com/")[/URL]
            myReq.Method = "Get"
            Dim res As HttpWebResponse = myReq.GetResponse()
            Dim sr As Stream = res.GetResponseStream()
            Dim sre As StreamReader = New StreamReader(sr)
            Dim s As String = sre.ReadToEnd()
            Debug.Print(s)
            OnLineStatus = True
        Catch ex As Exception
            OnLineStatus = False
        End Try
        Return OnLineStatus
    End Function

DougP
 
That is a clever way to check if no site can be reached.
You can also check if the httpWebResponse isNot Nothing andalso httpWebResponse.ContentLenght >0

There is also a StatusCode and StatusDescription on the response object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top