Welcome

You have reached the blog of Keith Elder. Thank you for visiting! Feel free to click the twitter icon to the right and follow me on twitter.

New Channel9 Show Code to Live Hits the Streets!

Posted by Keith Elder | Posted in .Net | Posted on 24-09-2007

image  It is good to see friends and people you respect bring something new to the developer community. Josh Holmes and Steve Loethen have done just that by creating a new Channel 9 show called “Code To Live“.  The show is different from other Channel 9 shows since it doesn’t showcase Microsoft employees talking about Microsoft technologies.  The show’s focus is on finding and showcasing developers who, well, code to live.  Sorry, I couldn’t resist the pun.  To quote the show:

Coding isn’t just a paycheck, it’s who we are! Tour the US with your hosts Steve Loethen and Josh Holmes as they interview fellow code warriors and get to the bottom of the coolest software in the world – yours.

imageThe first inaugural show actually has one of our team members on it, David Redding.  David has been getting into XNA a lot (outside of work of course) and since the HALO 3 launch is right around the corner the first show is covering XNA and Independent Gaming.  Check out the show and get it added to your RSS news reader.

 

 

 

 

 

Technorati tags:

Sync Services for SQL Server Compact Edition 3.5 in Visual Studio 2008

Posted by Keith Elder | Posted in Smart Clients, SQL Server Compact Edition | Posted on 23-09-2007

One of the new features in Visual Studio 2008 provides the ability to easily sync data from the server to the client and the client to the server using Sync Services for ADO.Net.  For developers supporting Winform and Smart Clients this is a much welcomed feature.   Developers will be able to take advantage of Sync Services in a variety of ways including a local store for off line data as well as caching data locally for speed.

When you deal with syncing of data as a developer the first thing that pops into mind is conflict resolution.  For those getting started with Sync Services we’ll look at how to setup sync services in a new project and then at some of the things you’ll need to know in order to handle conflict resolution within your applications.  All of the source code for this is available for download at the end of the article.

One Way Data Sync with Sync Services

In Visual Studio 2008 we have several new templates to get us started.  The one used for Sync Services is called “Local Database Cache”.  To get started with Sync Services open Visual Studio 2008 and create a new windows application.  Once you have your windows application created add a new item to your project and select the following template in the data category.

image

Essentially this file provides all the sync logic and we’ll use it later on to extend and insert our own logic for handling conflict resolution.  Other options can be extended here as well but more on that later.  Once the local database cache template is added to the solution a new screen to configure our data synchronization will be displayed.

image

From this screen we are going to select our database connection (or create one).  In this walk through we are going to leverage the Northwind database.  You can download this database from Microsoft if you do not already have it.  Once the Northwind database is setup create a connection to it.

image

The next step is to identify the tables we want to sync from the server to the client.  In the lower left corner of the configure data synchronization screen we are going to select the “add” button and choose our tables.

image

In the above example three tables were selected from the Northwind database:  Customers, Employees, and Shippers.  Note: If your tables do not use the built-in naming convention of “LastEditDate” or “CreationDate”  to compare the update or insert statements click the “New” button and specify the name.  For example, as a standard we typically use LastModifiedDate.  In this screen note that DateTime and TimeStamp are both supported.

image

Once the tables are configured the only options left are under the advanced button on the main screen.  This is where the project location is specified (which is how you move sync services to support WCF services) along with a few other options that should be self explanatory.

image

Once everything is configured press OK and the server database tables selected will be mirrored onto the client database you selected. 

image  

Once the data is synced into the local SQL server compact database the next screen shown allows us to build the data objects for the project.  Strong-typed datasets are the default option since most if not all of the other controls such as DataGridView, BindingSource, BindingNavigator and others work natively with this type.  It doesn’t mean another option like LINQ to Entities or something else couldn’t be used though.  In this walk through we’ll stick with the dataset.

image

If you are following along the project will now look like this:

image

We started this whole process by using the “Local Database Cache” template.  We now have a local SQL server compact edition database in our project called Northwind.sdf that will hold the local cache of our data.  We also have the NorthwindCache.sync file that will provide the syncing ability and lastly we have a strong-typed dataset which has our customers, employees and shippers tables.

So we can see the sync in action drag the Customers table from the Data Sources window onto the default form provided and add a button that will trigger the sync in the BindingNavigator.  The end result should look something like this:

image

To get the code we need for the click event of the form double click the NorthwindCache.sync file and press the link in the bottom right corner of the form called “Show Code Example..”.  This form will display:

image

Press the “Copy Code to the Clipboard” and close this form and the configure data synchronization form.   Paste the code into the click event of the sync button in the form.  The code is fairly simple since it creates an instance of our sync agent and then calls sync.  You’ll notice there is a TODO comment in the code that is pasted.   We need to add a line of code that will merge in the changes into our instance of the NorthwindDataSet.   Add this line to the sync click event.

this.northwindDataSet.Customers.Merge(this.customersTableAdapter.GetData());

If you are following along we can now run our application.  When it launches if we press the “Sync” button at the top of your form it will provide us a quick look at the options provided by the SyncStatistics object.  Pulling the syncStats object into a quick watch window should look like this:

image

Remember there is nothing to sync at this point since a sync was already performed after we added the NorthwindCache.sync file to our project.  It is also important to note that at this point we only have one way sync capability.  In other words, if we change the data locally in the database it will never make it back to the server.  If you are looking at a way to get data locally and cache it with a client / server model this is as far as you need to go with sync services.  At this point local data will get synced with the latest changes from the server.  We can easily add a timer to our application and have it sync every hour or based on network connectivity. 

Bi-directional Sync With Sync Services

Although bi-directional syncing isn’t an option we can turn on yet in the sync designer we can enable it with one line of code.  In your project right click the NorthwindCache.sync file and click view code.  This will create a new file that will be a partial class that we can use to extend the sync agent with our own logic.   In the OnInitialized method we are going to add the following line to enable bi-directional syncing on the customers table.

namespace SqlCESyncServices {
    
    public partial class NorthwindCacheSyncAgent {
        
        partial void OnInitialized(){
            this.Customers.SyncDirection = Microsoft.Synchronization.Data.SyncDirection.Bidirectional;
        }
    }
}

With this one line, any changes we make to the local data will now be sent back up to the server and any changes from the server will be sent back down to the client.  It is at this point when changes are allowed to be made locally and on the server we have to handle conflicts. 

Handling Conflicts with Sync Services

To handle our conflicts we are going to continue to flesh out the above file.  Remember this code isn’t affected by the designer so we can customize this to suit the business logic needs of our application.  The first thing we are going to do is add a new partial class to our code behind file for our sync agent.  For clarification we are going to be adding this code into this file.

image

If this file is missing simply right click NorthwindCache.sync and click “View Code”.  This file will be generated.

We are going to take advantage of two features of C#.  One is partial classes to handle our conflicts, the other is a newer feature called partial methods.  The reason the partial method is needed is our NorthwindCache.Designer.cs file already has a parameterless constructor in the object we need to extend.  In order for us to work around this the OnInitialized() method is marked as partial in the class which allows us to wire up our own events. 

In the NorthwindCache.cs code behind file, add a new partial class called NorthwindCacheServerSyncProvider.  In this class we are going to implement the OnInitialized() partial method so we can wire up our events to handle conflicts (and various other things).  The main event we care about for conflict resolution is the ApplyChangeFailed event.  Our class stub will look something like this starting out.

    public partial class NorthwindCacheServerSyncProvider
    {
        partial void OnInitialized()
        {
            this.ApplyChangeFailed += new System.EventHandler<Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs>(NorthwindCacheServerSyncProvider_ApplyChangeFailed);
        }

        void NorthwindCacheServerSyncProvider_ApplyChangeFailed(object sender, Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs e)
        {
            // handle conflicts here
        }
    }

In the ApplychangeFailed event we have a lot of options.  We have the client changes along with the server changes available to us to do some really fancy merging of data along with different actions we can take based on the the conflict.  We can chose to continue, retry, or retry and force write actions as seen in the below screen shot.

image

One example as to how this might be handled is to create a screen that displays both sets of data so the end user can make the choice which one they prefer and or merge the data from both records.  Obviously this will require a lot of thought and time to create but here is a sample that shows the Region on the client and the server are different and caused a conflict. 

image

Here is the code that was added into the ApplyChangeFailed event. 

   public partial class NorthwindCacheServerSyncProvider
    {
        partial void OnInitialized()
        {
            this.ApplyChangeFailed += new System.EventHandler<Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs>(NorthwindCacheServerSyncProvider_ApplyChangeFailed);
        }

        void NorthwindCacheServerSyncProvider_ApplyChangeFailed(object sender, Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs e)
        {
            // handle conflicts here
            DataTable clientChanges = e.Conflict.ClientChange;
            DataTable serverChanges = e.Conflict.ServerChange;

            CustomerConflictResolution frmConflict = new CustomerConflictResolution();
            frmConflict.clientBindingSource.DataSource = clientChanges;
            frmConflict.serverBindingSource.DataSource = serverChanges;
            frmConflict.ShowDialog();

        }
    }

One would need to keep going in this direction and resolve conflicts based on the data being resolved and the business logic surrounding that data.  It may be the last record edited wins or it may be that you need to merge data from one to another. 

As we can see in this walk through, Sync Services with SQL Server Compact Edition 3.5 adds a lot of new and much needed functionality to Visual Studio 2008.  I suspect that due to the ease at which data can be cached locally and synced a lot of developers will embrace this ability first.  If you are writing Smart Clients do investigate the ability of Sync Services to run under WCF.  It is supported and can be accomplished by moving the sync agent to another project (which is why I mentioned WCF in the advance screen above).   Happy syncing! 

 

Fishing Trip of The Year – Salmon Fishing on Lake Huron Almost Here

Posted by Keith Elder | Posted in General | Posted on 21-09-2007

image A few months ago I wrote about my rekindled love for fishing.  In a few days I will be going on the ultimate fishing trip to Michigan where the great Salmon Hunt of 2007 will take place.  I know you’ve seen the Discovery Channel where they show the salmon running up the rivers and how big they are.  Well that’s where I’ll be fishing next week except we’ll be in a boat on Lake Huron since it is illegal to catch the salmon in the river.  However, while they are in the lake before going up the river, they are fair game.

About eight guys from work and myself will be heading to Rogers City, MI on September 27th to kick things off.  This is one fishing trip I have been waiting on for several months and as the days inch closer my dreams of catching 30lb Salmon are about to come true.  Here is a map for those that like myself had no idea Rogers City even existed. Frabill Pack-A-Pole 7000

 With a six hour flight and then a five hour car ride I had to purchase a fishing rod holder for the trip.  I didn’t want my rods and reels to get busted during travel.  I went to the local sporting goods store here (Big Bucks) and picked up a Frabill Pack-a-pole model 7000 hard case.  I haven’t traveled with it yet so I can’t give you a review of it, but it does hold four rods and reels and has a place to put tackle. 

FrogTogg Since I am in Mississippi, the biggest shock on this trip is going to be the change in weather.  I know it is going to get cold, especially at night and the possibility of rain is pretty high.  Knowing this I picked up a Frog Togg Pro Sports Suite just for the occasion.  Even if I don’t catch fish, I am still planning on staying dry. 

Since this is my first trip to ever go salmon fishing, I am relying heavily upon my co-workers who have all gone multiple times.  I was especially curious about what they used for bait.  Turns out they mainly use 4-5 inch suspending crank baits.  I picked up several Smithwick Suspending Rogue five inch lures at the local sporting good store as well. 

Smithwick Blue Luminescent Smithwick1
Smithwick Tiger Roan Smithwick2

According to legend the Tiger Roan is suppose to be unbeatable during the day and the glow in the dark one is unbeatable at night.  I don’t care as long as I catch fish!  When I get back to civilization I hope to have enough pictures to choke a good mule so stay tuned!

Word Press, Apache, and Linux Contribute to Majority of Internet Blogger Spam

Posted by Keith Elder | Posted in Internet, Linux | Posted on 07-09-2007

If you have a blog as do I, one of the things you enjoy looking at is your PingBacks or TrackBacks.  PingBacks and TrackBacks were created as a nice way for bloggers to who know when other community members linked back to their articles.  Essentially they work like this.  Let’s say I write an article on my blog.  Another blogger reads it and links to it in one of their blog articles.  When a viewer on their blog clicks on my article link, a comment gets added to my blog article linking back to their site.  This is called a TrackBack.

For the original author this provides a way to keep track of who is commenting off line or linking to his/her information.  This has worked really well until the porn and drug industry figured out they could post unwilling information to thousands upon thousands of blog sites for free.  Spammers and hackers have literally taken this feature away from bloggers like myself by automating TrackBacks of web cams, sex toys, and so on.  Surprisingly the mortgage industry has yet to catch on. 🙂 

There are some spam systems out there that fight this which plug into several blog packages.  For example Akismet API is one the blog software I use (SubText) comes with.  Akismet is actually powered by Word Press (another popular blog software package written in PHP) and does a fair, not great job, of stopping TrackBack spam.  I say fair because I wind up still having to clean this junk from my comment logs.  To make matters worse if a blogger doesn’t clean this stuff out then it will count against him or her in the long run on search engine rankings.  A lot of bloggers have given up and turned TrackBacks off all together.  This is a shame because the feature is really useful.  This brings us to our question.  Where is this stuff coming from then?

The results may astonish you.  The same blog engine that is suppose to help you fight TrackBack spam is the very one that is creating the spam!  One Hundred  percent of what comes through to my site that is considered spam TrackBacks comes from one of three things:  Compromised Word Press blogs, an Apache Server, or a server running Linux.  Notice I said 100%, not 99% or 95%.  I can attribute each spam TrackBack to one or the other.  Don’t believe me?  Then let’s look at some examples.  Here is a screen shot to show you what I mean.  Below are the last four spam TrackBacks I received that were not filtered by Akismet.

Let’s look at what Netcraft says these domains are running below.

image

image

image

image

University of Alaska Fairbanks is obviously a distance learning community site and it has public_html folders enabled for user accounts.  In this example the user account idesign has been compromised since we see several hidden directories, “.psy” and “.xml”.   For those not in the know, if you EVER see a URL like /.something/ don’t click on it (ever wonder where http://www.slashdot.org gets it name? now you know).  This is a hidden folder on Unix servers.   The reason it is hidden is because when you type “ls” to get a directory listing the command doesn’t show you hidden folders that start with a dot.  It is a way hackers hide information on Unix systems from users.  This is one thing all the TrackBacks have in common.  The first URL is cut off but trust me, there is a folder start with a dot in the URL. 

The other interesting thing to note is three of the four URLs are generated from Word Press.  The “/wp-content/” folder gives this away since all word press folders typically start with the letters wp.  Out of the four URLs listed, three of them are Word Press and they all are running the Apache web server and two of the three unique domains are reportedly running Linux. 

Obviously the most ironic thing about this is the same blog software that is trying to help stop TrackBack spam is the same software that is creating the majority of it.  Thank you Word Press.

Google Reader Adds Search

Posted by Keith Elder | Posted in Internet | Posted on 06-09-2007

One of the key, very key things I had been wanting Google Reader to add was the ability to search.  Today I was looking up something and without even thinking about typed in a keyword into a search field located at the top of Google Reader and pressed the search key.

Wait a minute, that wasn’t there earlier!  Sure enough I then searched for “google reader search” and found this.  Quite funny, almost took me back there for a second.  Anyway, thank you Google Reader devs for adding such a much needed feature.  Now that you have that done please don’t rest just yet, add the ability to save searches so I don’t have to read every single Engadget article for Xbox or HDTV or whatever I’m interested in during a given week.  Make it so.

image