Posts

Convert serialzed JSON date string to a Javascript Date() object

When an DateTime object in C# is converted into a JSON serialized object for a web page, the format of the string looks like this in the JSON object: /Date( )/ See this for more details on why this format was chosen. This is all well and good, but how do you use that? A similar question and answer can be found here , but the solution posted there misses the possibility of a negative int. To expand just a bit on the solution posted there, we'll account for negative numbers: var someDate = new Date(+jsonDate.replace(/\/Date\((-?\d+)\)\//gi, "$1")); The three key things to note here are: 1. This method does not use eval(). eval can be a very dangerous thing to use since it will execute any javascript, so generally we try to avoid using it altogether. This is why we use JSON.parse, for example, instead of eval. 2. The "+" operator is a type converter in javascript that converts the string to an int. 3. The "-?" because the date value c...

Cascading DropDowns w/ JQuery ViewState Problem - use the UpdatePanel w/ triggers instead

I spent a lot of time trying to resolve a DOM issue I was having with a cascading drop downs setup on my page.  After mucking around with it for quite a while, I was able to figure out that it wasn't a direct effect of my code, but seemed to be a result of some DOM corruption that was happening from a plugin I was using or something else (that would be very time consuming to uncover). I was making a web service (POST) call to update the 2nd drop down list... if ($(someDDObject).val().length > 0) { someGlobalVariable = $(someDDObject).val(); ExecuteCallback('DoneChanged|' + someGlobalVariable); } The ExecuteCallBack is tied to a server-side async, non-static function that would parse the input and return the updates. A nice clean way to get server-side data without a postback, right? Right...mostly. The problem came in trying to update the client side data. In the callback function on the client, I would process the data like so: function CallBackResul...

IE8 Developer Toolbar is in the taskbar but cannot be seen/moved?

I scoured the internet for the solution to this problem and eventually wound up in the registry after I found a tip for a related problem. If you hit F12 and can not see the developer tools window, although you can see it in the taskbar, do the following: 1. Make sure IE 8 is not running. 2. Using regedit, go to HKCU/Software/Microsoft/Internet Explorer/IEDevTools and delete the 'Windows Position' key. 3. Restart IE 8 and hit F12. No restart of your computer is required and reinstalling IE 8 alone will not fix this issue. Voila!

Programming tidbit - be careful with the null coalescing operator on strings

One of my favorite shortcuts in programming with C# is the null coalescing operator, better known as '??'. var something = aString ?? "somethingelse"; Is a shortcut to... var something = aString == null ? "somethingelse" : aString; However, it is important to note that it does not take care of the empty string case, which is also vital to check for... var something = string.IsNullOrEmpty(aString) ? "somethingelse" : aString; Unless you can absolutely guarantee that a string will either be null or have a value, the '??' operator then becomes useless (sadly) for strings. EDIT:  As of .NET 3.5, you cannot overload this operator, so I would not recommend using this operator with strings, unless an empty string is acceptable.

10 Best Practices for Converting a Datasets-based App to LINQ to SQL

While experiencing the major headache of converting our existing web app to use LINQ to SQL instead of datasets (b/c we'll be switching to a Silverlight front-end using WCF services), I started making a list in my head of all the things I found really helped me get this conversion done as quickly as possible. Although the pain was unavoidable, it was greatly lessened by following some simple guidelines. The guidelines I have below are mix of LINQ specific guidelines, and generic data access layer guidelines. First, understand that we are a Microsoft shop, completely reliant on stored procs. With this in mind, it should be needless to say (but I'll say it anyway) that I really didn't have to/get to do too much LINQ, as in LINQ queries. The guidelines I will outline below are centered around this idea. 1. Put your entire database into one dbml. As far as I was able to determine, it is not possible to do cross-context LINQ queries. Even if you are using nothing but store...

How discoverable and usable is your PERL module?

I am working on using the IMAPI2 COM component in one of our products for an improved data burning experience. After exhaustively searching for anyone's C# port of the COM error codes laid out by Microsoft, I came up dry. There is a C++ header file included with the Windows SDK, but that's doesn't help me in C# where I wanted to use an enum (uint). There are 85+ error codes and I also wanted to easily link them to resource file descriptions of the error for localization purposes and it would mean a very tedious import of each enum as the lookup for the string, coupled with 85+ copy/pastes. Sound like a job for PERL? Well, I agree. I haven't used PERL for several months and saw this as an opportunity to pull out my Swiss Army Knife of programming. I am posting this to show how little code there is, and yet how long it took me to get it working (took my about 3 1/2 hours to get the script to run perfectly). Does 3 1/2 hours sound like an incredibly long amount o...

35x Improved T-SQL LevenShtein Distance Algorithm...at a cost

At work, we noticed a considerable performance hit using a T-SQL implementation of the Levenshtein algorithm created by Michael Gilleland found here . It seems to me that his approach was to replicate the C-code algorithm found in Wikipedia in T-SQL rather than taking a step back and re-conceptualizing the algorithm from a T-SQL standpoint. Feel free to correct me, but it see ms that the algorithm focues on three things to find the shortest distance between two strings (what it takes to make them the same): How many character insertions are necessary How many character deletions are necessary How many character substitutions are necessary These three traits are necessary to accommodate mis-aligned strings because of typos. If the algorithm is not designed with these constraints, one missing letter will result in inaccurate reporting because it throws the indexing off. The C-code example in Wikipedia uses a 2-D matrix approach, which is a natural, compact, and performant fit for ...