views:

462

answers:

4

I have coded my own tiny static DAL class for .NET Compact Framework and local SQL Server that does just what I need; GetTable(SQL,params), Exec(SQL,params), ExecScalar(SQL,Params) etc. I wanted to do everything by SQL so that's how I rolled, if there are better ways to handle SQL Connection in C# -except ORM- please let me know. Here's how I give public access to SQLConnection object under my DAL Class:

    public static SqlConnection Conn()
    {
        if (ConnStr == "")
            Defaults();
        try
        {

            if (conn == null)
                conn = new SqlConnection(ConnStr);
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
                Execute("SET DATEFORMAT DMY",null);
            }
            return conn;
        }
        catch
        {
            System.Windows.Forms.MessageBox.Show("Unable to connect to DB");
        }
        return null;
    }

The application I'm coding is being used on mobile Windows CE 5.0. The problem is, when user gets disconnected from wireless network then reconnects, if (conn.State != ConnectionState.Open) line doesn't get executed and application gives a general database exception, nothing else. How do I fix this?

Edit: Turns out exception is raised by functions such as GetTable(). I can handle it using try-catch in each function, but unfortunately in Compact Framework every sql error is named as "SqlException". How do I differ connection problems and query problems?

+1  A: 

Can you use the System.Net.NetworkInformation.NetworkInterface class to check if the network is available?

You can check for a network connection in .NET 2.0 using GetIsNetworkAvailable():

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

To monitor changes in IP address or changes in network availability use the events from the NetworkChange class:

System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged

As per previous SO answer: C# - How do I check for a network connection

Mitch Wheat
Unfortunately not, .NET Compact Framework doesn't seem to support System.Net.NetworkInformation.
Ertugrul Tamer Kara
+1  A: 

You could catch the SqlException and check the ErrorCode property to see if it was an error related to a loss of connection (i.e. error message would be something like "cannot connect to server" and that has a specific error code).

tomlog
If I'm not mistaken it's not possible to get any information / inner exceptoin / errorcode of database errors on Compact Framework.
Ertugrul Tamer Kara
It seems you're right about the ErrorCode not being support in .NET CF, but you should be able to use the InnerException, Errors and Number properties to get the desired information.
tomlog
I'm still unable to get any information regarding error even if I debug when mobile device is attached.
Ertugrul Tamer Kara
+1  A: 

If you are dealing with mobile applications you will never be able to rely on network access.

A better approach is to do all of you access to a local database (SQL CE, Sqlite), then synchronize the changes (.Net Synchronization Services will help with that) with the main database.

Whether that will work with the amount of data for your application is another story.

Chris Brandsma
The problem with this approach is all of mobile devices and SQL Server on network has to be synchronized all the time, except when a mobile device gets out of wifi range - which only happens unintentionally.
Ertugrul Tamer Kara
I'm not trying to say your are wrong, but if you do have to be synchronized all the time, yours would be the first case I've seen where that was actually true (and I hear it a lot).
Chris Brandsma
No offense taken. It's a POS application where desktop computers and PDAs works synchronised through SQL Server, for example once PDA posts a request desktop client may update it after few mins and likewise.
Ertugrul Tamer Kara
+1  A: 

I have fixed it by using new SqlConnection for each query instead of sharing one static SqlConnection for everything.

Ertugrul Tamer Kara