BusinessRx Community

Dedicated to the advancement of software, technology and the people who devote their lives to it.

Welcome to BusinessRx Community Sign in | Join | Help
in Search

BusinessRx Reading List

These blog entries are written by industry experts and leaders. We consider this content to be a good read for any software developer or web technologist.

September 2007 - Posts

  • Rails Migrations

    Even if you want nothing to do with Rails you should take a look at Rails Migrations because it is the best solution I have found to such a common development problem. The problem being how to make updates to your various databases in a sane manner.

    Start with the quick screencast. Then check out this nice cheat sheet.

    I know a couple of people are trying to figure out how to do something like this in .NET, if I had more time I would give it a try as well.

    -James

  • RadioShift

    Now that I am free of the corporate firewall I am looking for more and more ways to get music online. I gave up on Rhapsody because their new client sucks and they jacked up their price. I tried last.fm but for some reason every channel I pick runs out of content in like 10 minutes. I have Sirius subscription which can be cool. (Left of Center and Bloomberg Radio are my favorite channels)

    Picture 2-1

    I also recently came across Radioshift which is a cool application for the Mac which lets you record internet radio and listen to it on your time. For instance I just set it up to record a show called the "Indie Rock Show" on Z106 in Bloomington. It's on sundays from 7:00-8:00PM. I have never heard of the station or the show, but I look forward to checking it out tomorrow while working.

    -James

  • 4GuysFromRolla.com Turned Nine Years Old this Month

    Hard to imagine, but 4GuysFromRolla.com turned nine years old this month. The first article I authored was titled, Using ActiveX Controls on Your Web Page and looked at creating an ActiveX using Visual Basic 5.0 and deploying it on a web page via the <object> HTML element. It was published on September 16th, 1998. To put it another way, I've been writing articles and FAQs and tutorials on Microsoft web technologies on, at minimum, a weekly basis, for nearly one-third of my entire life. Scary.

    The site was originally started with three college buddies who covered non-web development topics, but by 2000 they had all moved on to other projects. 4Guys was sold to Internet.com (now JupiterMedia) back at the height of the dot com bubble, but I have remained the main contributor and editor for the site. Back in August of 2001 I wrote a short piece on the history of the site, which is still pertinent today seeing as not much has changed about the site since then. I keep pumping out articles every week, just like I did back in 2001.

    I wonder if I'll still be doing this when 4Guys turns 10, or 15, or 25. Who knows. If you would have asked me in 1998 if I thought I'd still be writing web technology articles nine years later I would have thought the idea highly unlikely, perhaps a bit absurd. But the younger you are, the harder it is to have an understanding or appreciation of the scale of time.

    In any event, it's been a great nine years and I look forward to the next nine. I hope you have found and continue to find the articles on 4Guys to be instrutive, useful, interesting, timely, and helpful.

    Happy Programming!

  • My Karamasoft UISuite Whitepaper

    ASP.NET ships with a variety of essential Web controls - the TextBox, the DropDownList, the GridView, and the like. While these controls are fine and dandy for simple scenarios, they quickly become obsolete for more complex ones. Consider collecting date values. The ASP.NET toolbox includes a TextBox and a Calendar control, either of which can be used independently to capture date values, but a better user interface involves a textbox integrated with a pop-up calendar. Similarly, ASP.NET's TextBox control is great for collecting plain text input, but falls short if you need users to be able to enter formatted text.

    Microsoft has made it clear that they plan on including only the basic controls in the ASP.NET toolbox and have left more advanced controls to the third-party market. There are a variety of third-party ASP.NET control vendors, from small one-man operations providing a single product, to larger companies that include a suite of controls. One such vendor that I've written about before on this blog and in my Toolbox column in MSDN Magazine is Karamasoft. I recently wrote a whitepaper for Karamasoft that provides an overview of UISuite, a control suite that encompasses ten UI-related controls. The whitepaper, Let UISuite Do Your Dirty Work [PDF], showcases how quick and easy UISuite's components make building and implementing powerful user interfaces.

    UISuite contains the gamut of canonical ASP.NET controls - there's a rich text editor, a menu, a date picker, and so on - but what most impressed me was the UltimateSearch component. Searching the contents of a website involves crawling and indexing content and displaying a search user interface. Microsoft provides Index Server to assist with searching a website's file system, but setting up and configuring Index Server can be challenging, especially when serving your website from a hosted environment. UltimateSearch makes adding search a breeze. Just drop the UltimateSearch assembly in the /bin directory and specify the indexing and crawling settings in a .config file and you're off and running. You don't need to register any component on the server; there's no configuration required (outside of that one .config file); you don't need to setup a Windows Service or schedule any tasks through Windows Scheduler; you don't need to have access to a database as the search index is stored in a file within your web application. In short, if you can FTP files to your website, you can use UltimateSearch.

    What's more, UltimateSearch includes a Web control that you can drop onto a page to display a search user interface. With a few property settings you can enable advanced search UI features, such as auto-suggest and spell checking. No code necessary. It really is amazing how easy it is to add search to your site with UltimateSearch. Ok, ok, enough gushing about UltimateSearch.

    In closing, if you are interested in learning more about UISuite or are in the process of evaluating third-party controls for your ASP.NET application, check out my whitepaper.

    Happy Programming!

  • IIS 7.0 Hits RC0 - Lots of cool new IIS7 Extensions Also Now Available

    One of the products that my team builds that I am most proud of is IIS 7.  IIS 7 is a *major* update of our web-server stack, and introduces a significantly new and improved extensibility, configuration, and administration architecture.  I've blogged about some of its features in the past here and here

    Doing a major re-architecture on a mature product is never easy.  Doing so on one that runs more than 40% of the web servers on the Internet is especially daunting.  The final product, though, is fantastic - and delivers an incredibly flexible, scalable, and robust server architecture that is going to enable us to-do really exciting things going forward. 

    Earlier this week we shipped the RC0 build of Windows Server 2008 and IIS 7.0.  You can learn more about it and download the RC0 version from Mai-lan's blog post here.

    IIS 7.0 Extensibility (and why it is cool)

    One of the major changes we made to IIS 7.0 was to make the system radically more extensible than previous versions of the web-server.  This extensibility applies both to the core HTTP processing engine, as well as to the configuration system, health monitoring system, and admin tool architecture.  You can now write managed .NET classes to cleanly extend all of these sub-systems.

    This is great from a customer perspective (since you can now replace and extend anything in the system).  It also enables us to easily ship web-server extensions and additions in a very agile way on the web.  Starting this week you are going to begin to see a number of really cool, fully-supported, free features start to be delivered this way.  Below are a few of them available this week:

    IIS 7.0 FTP Publishing Service

    We are delivering a new modern FTP service implementation that runs on IIS 7.0.  This FTP service offers a much improved administration and configuration experience (it uses the same web.config model as IIS7 and ASP.NET, and integrates into the IIS7 admin tool).  It now supports FTP over SSL, as well as UTF8 and IPv6.

    The FTP server enables you to host both FTP and web content from the same web-site on IIS7 (just add a FTP binding to your existing web-site to enable it).  It now provides support for virtual host names, which means you can host multiple FTP sites on the same IP address.  It also has built-in user-isolation support, which makes it perfect for shared hosting scenarios.

    The FTP server's authentication system is now pluggable - which means you can add your own username/password store to manage logins.  Best of all, you can use the existing ASP.NET Membership Provider model to plug-in your own credential system for logins and user management.

    You can learn more about the FTP Server here and download it here (x64 here).

    IIS 7.0 Media Pack Bit-Rate Throttling Module

    One of the challenges when hosting large videos and audio files on the Internet is that bandwidth costs can be expensive.  What is worse is that you often end up having to pay for users to download videos that aren't fully watched.  Specifically, web-servers are by default designed to download content files as fast as possible.  So if a user visits your site and starts watching a 50MB video on it, the web-server will by default try and transmit the 50MB file as quickly as possible to them.  If the user closes the browser half-way through watching the video, you will end up having to pay for the remaining 25MB of content they had finished downloading - but which they never actually watched.

    The IIS 7.0 Media Pack Bit-Rate throttler provides a much more cost-effective way to host video and audio on standard web-servers.  When a browser (using a plug-in like Silverlight, Flash, Windows Media Player, iTunes, etc) requests a media file, IIS7 will automatically detect the encoding bit-rate of the file and determine how many bytes per second the player needs to receive to play it continuously. 

    IIS7 will then "burst" enough of the content (by default 20 seconds of the video/audio file) to ensure that the client player can start playing the video, and won't ever run into a buffering delay.  IIS7 then automatically slows down the file transmission to equal the encoded bit-rate (so if the video is encoded at 400kbs, IIS7 will burst 20 seconds of this, and then slow the remaining transmission to 400kbs of content).  If the user closes the browser on the video while it is playing, IIS7 will automatically detect the connection was dropped and avoid sending any more of the content (saving you the remaining bandwidth costs).  This module works today with Silverlight, Flash and Windows Media Player - no code changes are required in the players to enable it.

    You can install the IIS7 Media Pack Bit-Rate Throttler on both Vista and Windows Server 2008.  You can learn more about it here and download it here (x64 here).

    IIS 7.0 Remote Manager Administration Tool

    IIS 7.0 ships with a significantly improved administration tool experience.  The admin tool is entirely written in managed code (using Windows Forms), and supports remotely administering a web-server over HTTP based web-services (allowing you to use the admin tool to manage servers remotely in a hosted environment through proxy servers).  The administration tool supports configuring both standard IIS settings, as well as ASP.NET ones (for example: you can use the tool to remotely manage ASP.NET membership/roles). 

    Earlier this week we shipped a standalone installation of the IIS 7.0 admin tool that you can use to remotely manage Windows Server 2008 based IIS7 web-servers from Windows XP, Windows Server 2003, and Windows Vista client machines. 

    You can learn more about the Remote Admin Tool and download it here (x64 here).

    FastCGI Support for IIS 5.1 and IIS 6.0

    IIS 7.0 ships with built-in support for FastCGI - which is a high performance alternative to using CGI for web-server extensibility.  Many popular web frameworks (in particular PHP) use this to integrate with web-servers.  In addition to having FastCGI support built-in to IIS 7.0, this week we also shipped a FastCGI ISAPI extension that works on existing IIS 5.1 and IIS 6.0 web servers. 

    You can learn more about the FastCGI extension here, and download it here.

    Summary

    The extensibility architecture of IIS7 is going to enable us to continually enhance and ship more features that run on top of it in the months ahead.  I'll be blogging more soon about some of these additional extensions (including about a new new wickedly cool automated web deployment system that supports rolling out versioned web applications across servers - including to remote web servers, and across web-server farms).  

    I think you are going to find IIS7 a really exciting web application server to use.  To learn more about it, make sure to visit www.iis.net, and sign-up for the upcoming free IIS7 web-casts here.

    Hope this helps,

    Scott

    P.S. IIS7 is currently running all of the www.microsoft.com web servers (read their blog post on why they love it). 

  • Silverlight Event Tokens

    Events are easy to add and remove when using the CLR and C#. Just sprinkle some += and -= in the right places and the runtime does the rest. Silverlight programming with JavaScript is a bit different, however, and although the addEventListener and removeEventListener appear similar to a DOM element's event registration APIs, these two methods have a twist.

    As an example, let's say we wanted to listen to the MediaElement's BufferingProgressChanged event, but we want to stop listening once buffering has exceeded 50%. The following code does work:

    function onLoad(rootElement)
    {
        
    var mediaElement = rootElement.findName("_media");
        mediaElement.addEventListener(
    "BufferingProgressChanged",
                                      
    "onBufferProgress");
    }

    function onBufferProgress(sender, eventArgs)
    {      
        
    // .. do something

        if(sender.BufferingProgress > 0.50)
        {
            sender.removeEventListener(
    "BufferingProgressChanged",
                                        
    "onBufferProgress");
        }
    }

    .. but this code will never unsubscribe from the event:

    function onLoad(rootElement)
    {
        
    var mediaElement = rootElement.findName("_media");
        mediaElement.addEventListener(
    "BufferingProgressChanged",
                                       onBufferProgress);
    }

    function onBufferProgress(sender, eventArgs)
    {      
        
    // .. do something

        if(sender.BufferingProgress > 0.50)
        {
            sender.removeEventListener(
    "BufferingProgressChanged",
                                        onBufferProgress);
        }
    }

    When not using a quoted string to specify the event handler, you have to save the value returned from addEventListener. The return value is a token that is "just an integer value to track the order in which handlers were added for each object-event combination" (read MSDN for other subtleties in the event registration methods).

    A proper unsubscribe looks like the following:

    var token;

    function onLoad(rootElement)
    {
        
    var mediaElement = rootElement.findName("_media");
        token = mediaElement.addEventListener(
                              
    "BufferingProgressChanged",
                               onBufferProgress);
    }

    function onBufferProgress(sender, eventArgs)
    {      
        
    // .. do something

        if(sender.BufferingProgress > 0.50)
        {
            sender.removeEventListener(
    "BufferingProgressChanged",
                                        token);
        }
    }
  • Mice, Usability, and Silverlight

    There is no mouse double-click event on UIElements in Silverlight.

    Some people think this is an odd omission. Silverlight's cousin WPF can fire a double-click event, so why not Silverlight? Even JavaScript can catch double-click events inside a web page (although in true web fashion, the exact behavior depends on the combination of browser version, operating system, and the current phase of the moon – see Jan Wolter's Javascript Madness: Mouse Events for all the gory details).

    You can still detect a double click using a mouse up event handler and a timestamp - but should you? The omission of a double-click event wasn't because of a technical limitation – the omission is a deliberate design decision. Silverlight is built for the web, and many consider double-clicking on the web to be a usability no-no.

    Here is what Peter Chng has to say:

    Take this comment by about the new Yahoo! Photos site; the user laments about the interface requiring a double-click to open a full-size image rather than just a traditional single-click as on most other websites. (The double-click detection is done via JavaScript)

    I'll admit that while I was first confused by this action, I thought little of that user's comment - I mean, how hard can it be to learn a simple action like that? But, after some more thinking, I've come to agree with the comment - breaking the pattern of how a user navigates on a website is not a good idea, even if it's done to try to make your website feel more like a desktop application.

    The New York Times website includes a feature that launches a search (in a pop-up window) when you double click a word inside an article. Digital Inspiration believes this behavior is user unfriendly, while Dion thinks the feature is a bug.

    You might also have noticed the only mouse button events in Silverlight are LEFT mouse buttons events. Macs don't have a right mouse button, and my guess is they never will (adding one now would be like admitting defeat). Exposing a right-click event could only invite more usability problems.

    Silverlight has some wonderful potential. Just remember to innovate – don't aggravate.

  • More on Conditional Compilation in ASP.NET

    Phil dug up an old post of mine on conditional compilation, but defining a constant in web.config didn't appear to work for him. I didn't see anything wrong with his approach, so I downloaded the solution and did some spelunking.

    With this page ...

    <%@ Page Language="C#" 
        
    CompilerOptions="/d:QUUX" %>
    ...
        <div>
           <% #if BAZ %>
             BAZ in the aspx file.
          <%
    #endif %>
          <%
    #if QUUX %>
             QUUX in the aspx file.
          <%
    #endif %>
        
    </div>
    ...

    ... and this web.config ...

    <system.codedom>
       <
    compilers>
          <
    compiler
             language="c#;cs;CSharp" extension=".cs"
             compilerOptions="/d:BAZ"
             type="Microsoft.CSharp.CSharpCodeProvider, System,
                 Version=2.0.0.0, Culture=neutral,
                 PublicKeyToken=b77a5c561934e089
    " />
       </
    compilers>
    </
    system.codedom>

    ... then the page behaves as if only the QUUX is defined.

    To understand the scenario I added <compilation debug="true"> to the web.config. Debug settings leave behind a .cmdline file in the temporary ASP.NET files directory. The .cmdline file contains the exact commands to invoke the C# compiler, and the abbreviated form looked like this:

    ...
    /t:library /utf8output
    /D:DEBUG /debug+ /optimize- /nowarn:1659;1699
    /d:QUUX  
    ...

    I went in thinking the compilerOptions would be additive, but after a smack on the forehead, I realized the compiler options in the @ Page directive override the web.config compiler options. Remove the compilerOptions attribute from the @ Page directive and BAZ becomes defined.

    The behavior does seem to follow the principle of least surprise, even if it did catch us off guard.

  • September 23rd Links: VS, ASP.NET, ASP.NET AJAX, Silverlight, WPF

    Here is the latest in my link-listing series.  Also check out my ASP.NET Tips, Tricks and Tutorials page for links to popular articles I've done myself in the past.

    Visual Studio

    ASP.NET

    • Debugging Script: Dumping out ASP.NET Session Contents: Tess Ferrandez from the ASP.NET support team has an excellent post that details how to use the windbg debugger to dump out real-time information about the current state within the ASP.NET in-process session store (# of sessions, what their current timeout is, and the memory size of the objects within each session).  She has a great script you can just run "as-is" in this article, as well as an in-depth discussion about how it works.  Very useful if you are ever trying to figure out why your application is consuming a lot of memory, and suspect it might be session state related.

    • Facebook.NET: Earlier this summer Nikhil Kothari from the ASP.NET team posted a really cool framework for developing ASP.NET based Facebook applications.  You can learn more about it from his blog post here, and download it from his CodePlex project here.  Steve Trefethen last week posted a VS starter kit for Facebook.NET which makes it even easier to get started.

    ASP.NET AJAX

    • New ASP.NET AJAX Control Toolkit Released: David Anson blogs about the update to the ASP.NET AJAX Control Toolkit that was released on Friday.  It contains a bunch of bug fixes and improvements.  The .NET 3.5 version of the control toolkit also now adds reference tags for the toolkit JavaScript files - which means you can now take advantage of VS 2008's JavaScript Intellisense support with them.

    • Animating the ModalPopupExtender: A cool post from Matt Berseth that walks through how to create a custom ModalPopupAnimationExtender control that adds animation support for ModalPopup scenarios.

    • ASP.NET AJAX In Action Book: Alessandro, David and Rama Krishna's ASP.NET AJAX in Action book is now showing up on book shelves.  This is a great technical book that covers ASP.NET AJAX very well.

    Silverlight

    • Halo 3 Game Guide: Halo 3 ships this week, and no doubt many people will find themselves afflicted with a sudden illness that requires them to stay home one day this week.  To help get you ready, watch one of the High Definition Halo3 Videos here (for a lower resolution video click here), and browse the Halo3 Game Guide here.  Both are built with Silverlight 1.0. 

    WPF and Expression Blend

    Hope this helps,

    Scott

  • Nested Selects in LINQ to SQL

    Consider the following diagram to track the votes in a contest. Each voter can register only one vote (a score from 1 to 4) for each contest entry.

    For any given voter, you'll want to present a screen showing every entry in the competition, along with the score assigned to the entry by the voter (if a score exists). To fetch the data with SQL, I'd write something like:

    SELECT E.EntryID, E.EntryName, E.EntryDescription, @VoterID AS VoterID,
           (SELECT V.Score
            FROM   Votes AS V
            WHERE (V.VoterID = @VoterID) AND (V.EntryID = E.EntryID)) AS Score
    FROM Entries AS E

    The query should yield a resultset like the following:

    ...
    EntryID EntryName VoterID Score 13 EntryA 1 4 14 EntryB 1 2 15 EntryC 1 NULL ...

    I thought the query would be difficult to express in LINQ, but I stumbled into a solution just by keeping a "SQL" mindset:

    var q = from e in Entries
             
    select new
             
    {
                   EntryID = e.EntryID,
                   EntryName = e.EntryName,
                   EntryDescription = e.EntryDescription,
                   VoterID = voterID,
                   Score = (
    int?)(from v in e.Votes
                                 
    where v.VoterID == voterID
                                 
    select v.Score).FirstOrDefault()
    };

    The LINQ generated SQL looks amazingly similar to the hand generated SQL, which is a comforting sight!

  • October's Toolbox Column Online

    My Toolbox column in the October 2007 issue of MSDN Magazine is avaiable online. The October issue examines three products:

    • DatabaseSpy - a rich database and query management tool that works with a wide variety of databases.
    • FileZilla - a feature-rich, open-source FTP client. Developers often need to FTP files to or from servers, and FileZilla is a free and powerful tool. The FileZilla project also includes an open-source FTP server implementation.
    • NCache - a framework for creating and managing a cache store distributed over multiple computers.

    This month's issue did not include a book review.

    As always, if you have any suggestions for products or books to review for the Toolbox column, please send them into toolsmm@microsoft.com.

  • Tip/Trick: Automating Dev, QA, Staging, and Production Web.Config Settings with VS 2005

    One of the questions I get asked fairly regularly is: "how can I can easily change different configuration settings in my web.config file based on whether my application is in a dev, qa, staging or production mode?"  The most common scenario for this is one where an application uses different database connection-strings for testing and production purposes.

    It turns out you can easily automate this configuration process within the Visual Studio build environment (and do so in a way that works both within the IDE, as well as with command-line/automated builds).  Below are the high-level steps you take to do this.  They work with both VS 2005 and VS 2008.

    1. Use ASP.NET Web Application Projects (which have MSBuild based project files)
    2. Open the VS Configuration Manager and create new "Dev", "QA", "Staging" build configurations for your project and solution
    3. Add new "web.config.dev", "web.config.qa", and "web.config.staging" files in your project and customize them to contain the app's mode specific configuration settings
    4. Add a new "pre-build event" command to your project file that can automatically copy over the web.config file in your project with the appropriate mode specific version each time you build the project (for example: if your solution was in the "Dev" configuration, it would copy the web.config.dev settings to the main web.config file).

    Once you follow these steps, you can then just pick the mode your solution is in using the configuration drop-down in the VS standard toolbar:

    The next time you build/run after changing the configuration mode, VS will automatically modify your application's web.config file to pick up and use the web.config settings specific to that build configuration (so if you select QA it will use the QA settings, if you select Deploy it will use the Deploy settings).

    The benefit with this approach is that it works well in a source control environment (everyone can sync and build locally without having to make any manual changes on their local machines).  It also works on a build server - including with scenarios where you are doing automated command-line solution builds.

    To learn more about the exact steps to set this up, please read the Managing Multiple Configuration File Environments with Pre-Build Events post that Scott Hanselman published earlier tonight.  Also check out my ASP.NET Tips, Tricks, and Gotchas page for other ASP.NET Tips/Tricks recommendations.

    Hope this helps,

    Scott

  • Meiraware Business Launches

    Meiraware Business

    My good friend Jayme Davis just launched the beta version of Meiraware Business, an online invoicing application. I have been testing it for him over the last couple of months and I am pretty impressed. I am currently working on getting Paychex to take over my payroll, when that is complete I won't need Quickbooks anymore and I can start using this application for invoicing and payments.

    Check it out. It's completely free during beta testing (Jayme has assured me there will be a free version after beta testing too)

    -James

  • The new C# ?? null coalescing operator (and using it with LINQ)

    One of the subtle (but cool) new language features in the VS 2008 version of C# is the ?? "null coalescing" operator.  This provides a nice, terse way to check whether a value is null, and if so return an alternate value.

    Simple Example Usages

    Several folks have blogged about the ?? operator in the past - read here, here, here, and here for some previous examples on how to use it.  Simply put, the ?? operator checks whether the value provided on the left side of the expression is null, and if so it returns an alternate value indicated by the right side of the expression.  If the value provided on the left side of the expression isn't null, then it returns the original value. 

    For example, let's assume we have a string variable "message".  We could check whether message was null, and return an alternate value using the code below:

    Because the "message" variable above wasn't null, the "result" variable is assigned the original "hello world" message value. 

    In the code snippet below, however, message is a null value, and so the ?? operator will return the alternate value we've provided:

    The ?? operator works for both reference types and value types.  For example, below we are checking whether the nullable integer "number" variable is null.  Because it isn't, the result will be the original value (55):

    If "number" is null, then result is assigned the value 0:

    Using the ?? operator with LINQ

    Last month I wrote a blog post that covered using the new LINQ to XML support in .NET 3.5.  One of the "gotchas" you often need to deal with when handling raw XML are cases where XML shapes are irregular and/or missing elements/attributes.  The ?? operator can be very useful in these scenarios.

    For example, let's consider a scenario where we have an XML file or feed with the following contact data:

    We could write the below LINQ to XML code in C# to open the XML file and retrieve back a sequence of anonymous type objects with "Name", "Title", "Email" and "YearsAtCompany" properties, and then databind the results to an <asp:gridview> control on a page:

    Notice above how I'm using the explicit conversion operator support on the XElement class to retrieve strongly typed values from the XML.  This enables me to just cast the c.Element("YearsAtCompany") value to an int - and it will then automatically convert the string value to an integer for me, and type the "YearsAtCompany" property on my anonymous type to be an int.

    When we run the above code snippet, we'll then get a nice Gridview listing of our contacts:

     

    This explicit conversion support works great when the <YearsAtCompany> element is always defined.  But it will throw a runtime error in the case where we have a <Contact> that is missing a <YearsAtCompany> sub-element:

    One way to fix this is to modify our LINQ to XML query so that we indicate that YearsAtCompany is a nullable integer.  We can do this by changing the explicit cast to be (int?) instead of (int):

    This enables our query to execute cleanly and not raise any errors.  Instead a null value will be assigned to the YearsAtCompany property if no <YearsAtCompany> element is present in the XML. 

    When we run the application and databind the results to the Gridview, you can see the YearsAtCompany column for our third contact is empty as a result (since the value is null):

    But what if we didn't want a missing XML value to result in a null integer value - but instead just indicate a value of 0? 

    Well.... that is where we can use the new ?? operator support.  We can just modify the LINQ to XML query like below to indicate a default value of 0 if no element is present:

    This indicates that if the <YearsAtCompany> element is missing in the XML, and the result would otherwise be null, instead assign a value of 0.  Notice in the intellisense above how C# automatically detects that this means that the YearsAtCompany property on the new anonymous type will never be null - and so it marks the property to be of type (int) instead of (int?). 

    And now when we run the page we'll see a value of 0 show up in our third row instead of a blank value:

    Summary

    You can use the new C# ?? operator in all types of scenarios.  Hopefully the LINQ to XML scenario above provides some interesting food for thought on how you can use it with any LINQ scenario (including LINQ to SQL, LINQ to XML, LINQ to Objects, LINQ to SharePoint, etc).  I think you'll find it cool and pretty useful.

    Hope this helps,

    Scott

  • VS 2008 Support to Treat HTML, CSS and JScript Validation issues as warnings instead of errors

    One of the features that VS 2005 added was support for HTML source validation.  This enabled you to validate your page markup against different HTML schemas and standards (XHTML Transitional/Strict, HTML 4.01, various browser types, etc).  You could also use the HTML validation features to detect missing link, image or style references in your markup.

    For example, if you have an HTML hyperlink in your page that points to a local file that doesn't exist, or if your markup uses (or misuses) HTML or CSS content, you'll by default see red squiggles in the source editor:

     

    And if the html document is open you'll by default see these issues listed in the error list:

     

    Note: if you don't want HTML validation support at all you can optionally turn off the html validation feature.

    Treating HTML, CSS and JScript Validation Issues as Warnings Instead of Errors

    One of the common requests with VS 2005 has been to not have these html markup issues added to the error list, but instead have them listed in the warnings list (which you can toggle to hide/show).  This way you can have your error list only contain VB/C# code-related issues, and not be cluttered with a lot of markup problems.

    Mikhail Arkhipov from the Visual Studio Web Tools team recently posted about how support for this was just checked-in to a post Beta2 build of VS 2008.  Specifically, you can now bring up the Tools-Options dialog and configure HTML, CSS and JavaScript issues to be treated as warnings instead of errors (I believe this is also now the default out of the box):

    This should make it easier (and cleaner) when working on large markup files.

    Hope this helps,

    Scott

  • Lessons Learned

    I still have plans to post about my two year project that wrapped up this summer, I keep starting a post and then trashing it. There was so much going on it's hard to know where to start. I will do it eventually, especially after being inspired by some great posts from Michael Eaton.

    Michael Eaton has done a great job writing about the nightmare project he was on (actually with the same company as mine). Check out Part 1, Part 2, Part 3, Part 4, Part 5, and a bonus follow up.

    -James

  • September 16th Links: ASP.NET, ASP.NET AJAX, IIS7, Visual Studio, Silverlight

    Here is the latest in my link-listing series.  Also check out my ASP.NET Tips, Tricks and Tutorials page for links to popular articles I've done myself in the past.

    ASP.NET

    • Debugging Script: Dumping out Current and Recent ASP.NET Requests: Tess Ferrandez from the ASP.NET support team has an excellent post that details how to use the windbg debugger to dump out real-time information about what requests ASP.NET is currently processing on the server (super useful when investigating failures).  She has a great script you can just run "as-is" in this article, as well as an in-depth discussion about how it works.
    • Freezing GridView Column Headers with CSS and a Control Adapter: Matt Berseth has a cool article on how you can use CSS to implement a "frozen columns headers" feature with the standard <asp:gridview> control.  Note that this type of scenario is much easier with the new <asp:listview> control in .NET 3.5, which enables you to customize all of the html markup emitted.

    ASP.NET AJAX

    • Update for the iPhone: Matt Gibbs blogs about a fix you can apply to ASP.NET AJAX to address a change in the iPhone 1.01 patch that impacts how regular expressions are parsed in the iPhone Safari browser.  Matt's fix enables you to continue targeting iPhone users with your ASP.NET AJAX applications.

    Visual Studio

    • XML to Schema Inference Wizard for Visual Studio 2008: Scott Hanselman has a great blog post about a new item template wizard that makes it really easy to automatically infer an XML schema from a XML file or XML snippet.  He then shows how you can use the cool new LINQ to XML features in VB9 to get automatic LINQ to XML intellisense inside VS 2008 with it.

    IIS 7.0

    • Ten Reasons why IIS 7.0 Rocks: The Microsoft.com team has been running the entire www.microsoft.com site on IIS7 and Windows Server 2008 the last few months.  This blog post from them lists their top 10 favorite improvements in IIS7.

    Silverlight

    • Ink Recognition and Silverlight: Loren has a really cool sample built with Silverlight that demonstrates how you can use the ink pen recognition support within Silverlight to enable tablet or mouse based Google searches using a Silverlight application.  A very cool mashup.
    • Silverlight Spy Tool: Silverlight Spy is a small WinForms application capable of inspecting Silverlight 1.0 applications.  You can use it to navigate a XAML object explorer of a running Silverlight application, trace out messages from it, and enable easier debugging of it.
    • HtmlTextBlock control for Silverlight: David Anson has published a really cool control for Silverlight 1.1 that provides an easy way to take HTML input and display it using the text/graphics stack within Silverlight.
    • Silverlight Dev Camp Chicago: Kevin Marshall is organizing an upcoming Silverlight DevCamp event in Chicago on September 28th-29th.  You can attend completely for free.

    Hope this helps,

    Scott

  • My Slides/Samples from MIX:UK - Building Silverlight Apps with .NET and Building ASP.NET Apps with VS 2008

    The last two days I've been speaking at the the MIX:UK conference that was held this week in London.  We had a sold out crowd of 500 people come to learn more about some of the new Microsoft web technologies.

    I gave 5 talks at the conference, including the Keynote and 4 breakout talks: Building Silverlight Applications with .NET (Part 1 and 2), and Building ASP.NET 3.5 Applications with VS 2008 (Part 1 and 2).  I also had the chance to participate on 2 panels (including a really fun one where we sat around and drank tea, ate biscuits, and discussed technology trends).

    Below are the slides + demos from my talks if you are interested in downloading them.

    Building Silverlight Applications with .NET (Part 1 and 2)

    This two part session drilled into how to build Silverlight 1.1 applications using .NET.  I really like the slide + sample approach of this talk (I first gave it at MIX:Hungary in June), and think it provides a really good way to learn the programming concepts of Silverlight 1.1 using .NET.

    • Click here to download the slides from the talk. 
    • Click here to download the samples from the talk.

    Note: All samples are built using the current Silverlight 1.1 Alpha and VS 2008 Beta2 with the Silverlight Tools Alpha Installed. 

    Building ASP.NET Applications using VS 2008 and .NET 3.5 (Part 1 and 2)

    This two part session was primarily a demo driven talk that shows off many of the new improvements for ASP.NET with .NET 3.5 and VS 2008. 

    • Click here to download the slides from the talk.
    • Click here to download the samples from the talk (these require VS 2008 Beta2).

    To learn more about VS 2008 and .NET 3.5 Beta2 for web development, please also check out these recent blog articles of mine that cover some of its features in more detail:

    General:

    VS 2008:

    ASP.NET in .NET 3.5:

    LINQ to SQL:

    New Language Features:

    One last note...

    I usually get at least 4-5 emails a week from people asking whether they can re-use some of my slides/samples for their own presentations (or books).  My answer to this is always "absolutely!"  Please do not worry about asking for permission on this - you can always re-use any of the content from my talks and blog postings (and feel free to re-use slides and decks wholesale).  I write this content to help people learn how to use .NET - so you helping spread the information is pure goodness from my perspective (my blog is technically not part of my official job, rather just a side hobby I enjoy and find useful).

    Hope this helps,

    Scott

  • September's Toolbox Column Online

    My Toolbox column in the September 2007 issue of MSDN Magazine is avaiable online. The September issue examines three products:

    • ASPxperience Suite - a suite of 18 ASP.NET server controls designed to enhance your web application's aesthetics and usability by presenting information in interesting and attractive ways.
    • FTP for .NET 2.0 - a comprehensive API for connecting, uploading, and downloading files to and from an FTP server.
    • DotImage Photo Pro - a powerful and easy-to-use imaging API with a number of imaging controls for WinForms and Web Forms.

    This month's issue reviewed Pro SQL Server 2005 Database Design and Optimization, by Louis Davidson, Kevin Kline, and Kurt Windisch. Here is an excerpt from the review:

    The scalability, extensibility, and maintainability of a database are directly related to the quality of its design. As such, it is important that the developers creating the data model for a data-driven application have a solid understanding of the principles of good design. Pro SQL Server 2005 Database Design and Optimization, by Louis Davidson, Kevin Kline, and Kurt Windisch (Apress, 2006), introduces readers to these principles and illustrates how to implement them in SQL Server™ 2005.

    As always, if you have any suggestions for products or books to review for the Toolbox column, please send them into toolsmm@microsoft.com.

  • Comments Back On

    As I blogged about earlier, I turned off comments this summer because I was going 'off the grid' for much of the summer and didn't want comment spam to accumulate. We're back from our summer of travel, so comments have been re-enabled. The quantity of blog posts here will also pick up as I get back into the swing of things.