Question

I'm developing an app in C# (.NET3.5 Compact Framework) and I need to connect to a sql server occasionally over GPRS. However the connection closes after some time of inactivity or if the PDA is placed on the craddle.

I'm looking for a way to check and re-enable the connection automatically from the app.

I've seen several pages on the web suggesting using the ConnectionManager, the Opennet.CF framework or an approach based on a XML doc. None provide a full example.

My latest test is based on the code below.

Could you help me with some suggestion or piece of code?

Many thanks in advance,

Filipe

public static void setCM_ProxyEntriesHTTP(Guid Guid)
    {
        try
        {
            remCM_ProxyEntries(getCM_ProxyEntriesAtual());
            string wWap = "<wap-provisioningdoc>" +
            "<characteristic type=\"CM_ProxyEntries\">" +
            "<characteristic type=\"HTTP-{" + Guid.ToString() + "}\">" +
            "<parm name=\"ExtraInfo\" value=\"\" />" +
            "<parm name=\"Override\" value=\"\" />" +
            "<parm name=\"Username\" value=\"\" />" +
            "<parm name=\"Enable\" value=\"1\" />" +
            "<parm name=\"Type\" value=\"0\" />" +
            "<parm name=\"DestId\" value=\"{436EF144-B4FB-4863-A041-8F905A62C572}\" />" +
            "<parm name=\"SrcId\" value=\"{" + Guid.ToString() + "}\" />" +
            "</characteristic>" +
            "</characteristic>" +
            "</wap-provisioningdoc>";
            ConfigWrapper.ProcessXml(wWap);
        }
        catch { }
    }
Was it helpful?

Solution 2

Thank you josef! :)

I was able to check and connect using the following code:

 private static bool InternetAvailable2()
    {
        bool hasNet = false;
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://www.bing.com");
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.Proxy = null;
            //request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    hasNet = true;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message,"NetAvail2");
        }
    }

OTHER TIPS

Normally you do not need to care about the connection, as long as your request triggers the connection manager. So, before you do the SQL query or connection, do a http request on a known, reacable server. That http request will trigger the connection manager to open the connection. The connection manager will manage the connection.

If there is no data flowing for a longer period, connection manager (CM) will close the connection, except you configured an "always on" connection. 'Alwas on' can be configured with a wap provisioning xml file for the connection. A similar xml as you wrote (which is useless for your needs): http://msdn.microsoft.com/en-us/library/bb737329.aspx

The trick to trigger CM was needed in an app I wrote using ftp. I had to do a http request (to google.com) before I could open a ftp connection. FTP connection requests will NOT trigger CM, as sql requests will also not do.

So, in your code first do a http request and wait for or repeat until success, then issue your sql connection request. -OR- Configure an 'always on' connection (if you have a flat data rate contract with your internet provider).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top