Saturday, September 8, 2012

Creating an Occasionally Connected Smart Device Application a working example with emulator

  In this blog I plan on going thru how to create an occasionally connected smart device application and get it working correctly. I found this example Smart Device Example link to explain how to create and connect to the emulator. The problem is this is not a complete and working example, so after spending several days just trying to get this to work I will try to go back thru what I did. I will start with this example with my added changes and present a working version of this example.
  The first issue I ran into when trying to create this project is which Visual Studio version should I use. After searching thru the web from the best that I can see I the newest version that still supports winCE is VS2008, so I started with this version. The first step is to make sure you install all the prerequisites as listed below:

Prerequisites


To complete this walk through you must:
   One mistake I made was to not install all of the preequisites, since I noticed I already had MSQL Server Comact 3.5 installed. You must make sure you install the Service pack 1 for this or you will get connection errors when you try to do your remote connection with the compact framework. Mine would blow up on the client side of code listed below:

Microsoft.Synchronization.Data.

  This would give the following cryptic error unhandled Message="TargetInvocationException" and I had to seach lots of articles to finally figure this out. Once I added service pack 1 that error went away. 
  After fixing that I then got a client error in Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize(); for the following ArgumentException error on the client side.
   The first thing to do is see if IE can connect to the WCF service to ensure it is still working. To do this I set theMiddleTierServiceLibrary as the startup project and then select the start without debugging option in the Debug menu. This will bring up the deployment window where I will just select cancel and then in the deployment errors click continue. Figure 1 shows the WCF screen and from here I just copy the http://10.83.4.96:8080/NorthwindCacheSyncService/ from the WC F test client and paste it in IE where you should see the page as listed in figure 2. From this I now know that the WCF service is working and a client can connect to it.
 





Figure 1


Figure 2

    Once I know a client can connect I then set SmartDeviceSyncClient as the start up project. Then I just select start debugging from the Debug menu option. Once the code runs I will get the ArgumentException error  so I ignore the error and let the code run. My next step is to try to connect thru IE on the PocketPC emulator with the saem address as listed above. As Shown below you can see that connection files and I cannot connect to the WCF service from the PocketPC.  




Figure 3

    No back to reading more articles on this problem and I come up with this. How is the PocketPC able to establish a connection to the service. Some how it has to have a enet connection and know how to resolve it to an IP address. So the way to do this is in VS IDE Tools->Device Emulator Manager. Once this is selected it bring up a panel with all the emulation devices. On this screen I selected window Mobile 5.0 and right click on it. The right click option and select connect.



Figure 4

Once the device is connected you should see the phone and active sync screens pop up and establish a connect. Now the PocketPC has a ENET connect thru active sync and it should be able to connect to WCF service. But in my case using  http://10.83.4.96:8080/NorthwindCacheSyncService/ still did not connect with the same connection error. So the next thing I tried was to use ppp_peer:8080 as the connection name as this is the default name for the emulated device and behold it was able to connect. Great now I know I can connect on ppp_peer with IE, but what about the client application.




Figure 5




   After running the Smart Device Client code I was still getting a argument error and from this I see that it cannot connect to the WCF service thru IP address directly. So for a test I inserted strHostName = Convert.ToString(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]); into the Sync() function just to see what the address was that it returned. The address returned was 192.168.55.101 and this was what I was expecting. Then I just let the code run and magic it now started to work.
string


Figure 6



  So from this I am not sure what caused it to start working, but I tested it several times and it seams to run and connect correctly now. I would assume that forcing a DNS look up made the look up table instantiate the connection. I will test this further in another article, but for not it works.


ServerSyncProviderProxy syncProxy =new Microsoft.Synchronization.Data.ServerSyncProviderProxy(svcProxy);

Thursday, June 14, 2012

Regular Expressions for Date/Time Validation

On one of my projects I was asked to validate the date/time input into a textbox. So my solution was to use the built in Validator controls in VS2008. Since this project is based on 2008 I will use that for the IDE on this project.

The first step for this project was to select the RegularExpressionValidator from the toolbox under Validation tab. Once I dragged it to the project all I had to do was add the ControlToValidate, some text on error and then add the ValidationExpression. This is where it got a little tricky as I assumed all I had to do was find someone that already had a sample that worked and add it. Here is a link that has some good examples on validation.Self-Validating-Text-Box




The problem was for the examples I found none of them seamed to work correctly. So then I had to dive into Regular Expressions and try to figure out how to make the one I downloaded work.

Here is a link on using regular expressions:The-30-Minute-Regex-Tutorial. After reading this I then downloaded Expresso and installed it on my computer. From here I could then use Expresso to test and validate my expressions.

Here is the expression I found on the internet:

\A(?:1[0-2]|0[1-9])[/.-](?:3[01]|[12][0-9]|0[1-9])[/.-][0-9]{4}[ \t]+(?:1[0-2]|0[1-9])[:.](?:1[0-9]|0[1-9])[:.][0-5][0-9][ \t]+(?:AM|PM)

This seamed to work fine on my first test case, so I put it in my code and of course it did not work. Therefore I then put it into the Expresso tool and started to run test cases.


  

So with a date/time of 06/13/2012 02:11:10 AM it worked fine, but when I changed the date/time to 06/13/2012 02:31:10 AM it did not work. So from here I needed to dive into regular expressions to see why it was not working. The first step I took was to review the Regex Analyzer on the right screen of Espresso. This will explain what the commands are doing and as listed below I went thru each on of them.
 
\A - Beginning of string
 Date
(?:1[0-2]|0[1-9]) 
  • () - grouping
  • ?:  match : 0 or 1 times
  • 1[ looking for 1
  • [0-2] range of 0 to 2
  • 0[ looking for 0
       This will match the month forcing it to be 1 to 12

[/.-] - Match [] operator so this will match / . -

        This will match the /

(?:3[01]|[12][0-9]|0[1-9])
  • () - grouping
  • ?:  match : 0 or 1 times
  • 3[ looking for 3
  • [01] - match 01
  • [12] match 12
  • [0-9] range of 0 to 9
  • 0[ looking for 0
  • [1-9] range of 1 to 9
       This will match the day forcing it to be 0 to 31

[/.-]  Match [] operator so this will match / . -

       This will match the /

[0-9]{4} - any class from 0-9 repeat 4 times

     This will match the year to 2012

[ \t]+ match \t tab with any repetitions

     This will match any tabs

Time

(?:1[0-2]|0[1-9])
  • () - grouping
  • ?:  match : 0 or 1 times
  • 1[ looking for 1
  • [0-2] range of 0 to 2
  • 0[ looking for 0
  • [1-9] range of 1 to 9
        This will match 1 and force it to 0 to 2 and 0 to 1 to 9. Which will work for the first test case but not anthing greater then 1.

[:.] - any character in this class


(?:1[0-9]|0[1-9])

  • () - grouping
  • ?:  match : 0 or 1 times
  • 1[ looking for 1
  • [0-9] range of 0 to 9
  • 0[ looking for 0
  • [1-9] range of 1 to 9
        This will match only 1 from 0 to 9 and 0 from 1 to 9. 

[:.]- any character in this class


[0-5][0-9]
  • [0-5] - match 0 to 5
  • [0-9] - match 0 to 9
       This will match the data to the range 00 to 59

[ \t]+ match \t tab with any repetitions
     This will match any tabs

(?:AM|PM) - Match AM or PM
      This will get the AM or PM

So from this break down I can see a couple of my problems in the time.

  1. (?:1[0-2]|0[1-9]) I need this to go from 0 to 5
  2. (?:1[0-9]|0[1-9]) I need this to also go from 0 to 5
  3. [ \t]+  I need to also handle space
  4. [/.-]  I only want to match /
Below I have listed my new changes. I ran thru testing on expression and it appears to work. Hopefully this will help some else out on implementing this functionaly.
\A(?:1[0-2]|0[1-9])[/](?:3[01]|[12][0-9]|0[1-9])[/][0-9]{4}[ \s]+(?:1[0-2]|0[1-9])[:.](?:[1-5][0-9]|0[1-9])[:.][0-5][0-9][ \s]+(?:AM|PM)




 Also you can easily use regex in CSharp to validate or filter data just like grep. All you need to do is add using System.Text.RegularExpression and then call RegEx.IsMatch(...) to evaluate the data of interest.

Monday, June 4, 2012

There were deployment errors. Continue! Missing popup

For this blog I will go thru how to enable the delpoyment errors measage box if it is not showing in VC2008.


I ran into this problem on one of my PC's while testing a WCF service for a smart device. When I configure this device to deploy I will select cancel and then hit yes to continue to test out the WCF interface. Below is a snapshot of the WCFTest client.




Well for one of my PC's this was working, but for the other I would just get a deployment error. In order to enable this option got to tools->options->Projects and Solutions->Build and Run->On Run, when build or deplyment errors occur: Prompt to Launch.







Thursday, June 16, 2011

Silverlight developer version

I was working on testing a Silverlight web with WCF I created I wanted to run the page in debug mode. When I tried to run it I got the error Silverlight Debug version not installed. After some quick we searching I found the installer for it from Microsoft and installed it. This enabled me to run and step thru the code in debug mode. Which is a nice feature to help in debugging and understanding the code.

Telerik Controls

I have been working with Telerik controls for the past month on my consulting business. Currently I still have not made my descision if they are worth purchasing or not. When I first started to use them I though they were great. Now I think they are great if your data happens to be in thier definition of a cookie cutter format. If your data meets that then they work great and you get web reports up and operational quickly. The flip side of this is if your data is not meeting the cookie cutter format, which most of mine will not. The you have to dig into their API and figure out how to manipulate the data to make it work. This is were you start to see the features they do not support that I could easly do in a datagrid. Such things as a easy way to sort a table that is not using sqlDataSource. I would almost prefer for them to just let me access a datagrid instead of their table for displaying data as an option. Do not get me wrong they do have a lot of options, but I just need to get something up and running quickly to meet my customers timeline and do not want to get a PHD in their controls to be able to use them.