<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Far Reach Blog &#187; Silverlight &#8211; Technical</title>
	<atom:link href="http://blog.farreachinc.com/category/silverlight/silverlight-technical/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.farreachinc.com</link>
	<description>Far Reach Technologies Blog</description>
	<lastBuildDate>Thu, 17 May 2012 17:39:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Silverlight 5 Supported Through 2021</title>
		<link>http://blog.farreachinc.com/2011/12/15/silverlight-5-supported-2021/</link>
		<comments>http://blog.farreachinc.com/2011/12/15/silverlight-5-supported-2021/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 16:48:17 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight - Business]]></category>
		<category><![CDATA[Silverlight - Technical]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Microsoft Silverlight]]></category>
		<category><![CDATA[MIcrosoft Silverlight 5]]></category>
		<category><![CDATA[Silverlight 5]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=1143</guid>
		<description><![CDATA[We’ve written in the past about Microsoft® SilverlightTM as a viable and essential platform for building line of business applications.  After promising a new version by the end of the year, Microsoft released Silverlight 5 on December 9.   There are a &#8230; <a href="http://blog.farreachinc.com/2011/12/15/silverlight-5-supported-2021/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/12/15/silverlight-5-supported-2021/' addthis:title='Silverlight 5 Supported Through 2021 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/12/silverlight_5.jpg"><img class="alignleft size-full wp-image-1144" title="silverlight_5" src="http://blog.farreachinc.com/wp-content/uploads/2011/12/silverlight_5.jpg" alt="MS Silverlight 5" width="218" height="231" /></a>We’ve written in the past about Microsoft<sup>®</sup> <a title="Microsoft Silverlight" href="http://www.microsoft.com/SILVERLIGHT" target="_blank">Silverlight</a><sup>TM</sup> as <a title="Far Reach Silverlight Blog Post" href="http://blog.farreachinc.com/category/silverlight/silverlight-business/" target="_blank">a viable and essential platform for building line of business applications</a>.  After promising a new version by the end of the year, Microsoft released <a title="MS Silverlight" href="http://www.microsoft.com/silverlight/" target="_blank">Silverlight 5</a> on December 9.   There are a couple of excellent blog posts offering an overview of Silverlight 5’s new features <a title="Silverlight 5 Features" href="http://www.silverlight.net/learn/overview/what%27s-new-in-silverlight-5/silverlight-5-release-overview" target="_blank">here</a> and <a title="Silverlight 5 Features" href="http://timheuer.com/blog/archive/2011/04/13/whats-new-in-silverlight-5-a-guide.aspx" target="_blank">here</a>.</p>
<p>Rumors this is the final version of Silverlight have been circulating, which Microsoft has neither confirmed nor denied.   These rumors have caused some to speculate Silverlight is on the way out.  Microsoft, however, released the news they will <a title="Silverlight 5 Supported Through 2021" href="http://support.microsoft.com/gp/lifean45#sl5" target="_blank">support Silverlight 5 through 2021</a> and, as they do with all Major Tool versions, will provide a minimum of 12 months’ notification prior to the end of support.  This seems to indicate Silverlight 5 is indeed the last version, but we don’t think it means Silverlight is doomed.</p>
<p>Has our view on the viability of Silverlight changed?  We continue to see <a title="Far Reach Silverlight Development" href="http://www.farreachinc.com/Silverlight-Development" target="_blank">Silverlight as our tool of choice</a> when it comes to Line of Business applications.  We feel pretty secure about Silverlight moving forward, used in the right situations,  given the fact it will be supported through 2021 and also knowing third party vendors (like <a title="Telerik Silverlight Products" href="http://www.telerik.com/" target="_blank">Telerik</a>) will continue to provide products for Silverlight.</p>
<p>What are your thoughts on the viability of Silverlight moving forward?   Do you plan to continue developing with Silverlight?  Let us know your plans.</p>
<p>&nbsp;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F12%2F15%2Fsilverlight-5-supported-2021%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/12/15/silverlight-5-supported-2021/' addthis:title='Silverlight 5 Supported Through 2021 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/12/15/silverlight-5-supported-2021/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Storing Files in SQL Server using WCF RIA Services and Silverlight – Wrap up</title>
		<link>http://blog.farreachinc.com/2011/07/08/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-wrap-up/</link>
		<comments>http://blog.farreachinc.com/2011/07/08/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-wrap-up/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 19:52:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight - Technical]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[WCF RIA Services]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=640</guid>
		<description><![CDATA[I recently completed a three part series which described how you can create a Silverlight LOB application that stores and displays documents using FILESTREAM Storage in SQL Server 2008. Here are the articles for your reference. Configuring FILESTREAM in your &#8230; <a href="http://blog.farreachinc.com/2011/07/08/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-wrap-up/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/07/08/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-wrap-up/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight – Wrap up ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I recently completed a three part series which described how you can create a Silverlight LOB application that stores and displays documents using FILESTREAM Storage in SQL Server 2008. Here are the articles for your reference.</p>
<ol>
<li><a href="http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1">Configuring FILESTREAM in your database and WCF RIA Services setup</a>.</li>
<li><a href="http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/">Uploading and Saving files to the database from a Silverlight LOB application</a>.</li>
<li><a href="http://blog.farreachinc.com/2011/06/26/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3/">Viewing files stored in the FILESTREAM from a Silverlight LOB application </a></li>
</ol>
<p>I received a few requests to share source code so I decided to pull together a solution which contains the code from the three articles listed above.  As a bonus I am including another tip &#8211; how to display the image within your Silverlight application.</p>
<p><strong>Displaying a saved image in your Silverlight application</strong></p>
<p>Here are the steps involved to get our image to display in our application.</p>
<ol>
<li>Add an Image control to your XAML.</li>
<li>Add a SelectionChanged event handler to our Grid</li>
<li>Add a method in our ViewModel to retrieve the File object based on a provided DocumentId.</li>
<li>Add code in our View to render the File object as an image in our Image control.</li>
</ol>
<p>1. Our XAML:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>Image x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;imgViewer&quot;</span> Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;100&quot;</span> Height<span style="color: #008000;">=</span><span style="color: #666666;">&quot;100&quot;</span> <span style="color: #008000;">/&gt;</span></pre></div></div>

<p>2. Add a SelectionChanged event handler on our gridview control.  First the XAML:</p>

<div class="wp_syntax"><div class="code"><pre class="charp" style="font-family:monospace;">SelectionChanged=&quot;rgvDocuments_SelectionChanged&quot;</pre></div></div>

<p>How the handler in code:</p>

<div class="wp_syntax"><div class="code"><pre class="charp" style="font-family:monospace;">private void rgvDocuments_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    if (rgvDocuments.SelectedItem != null)
    {
        imgViewer.Source = null;
        AccountDocument accountDocument = (AccountDocument) rgvDocuments.SelectedItem;
        ViewModel.GetDocumentFile(accountDocument.DocumentID);
    }
}</pre></div></div>

<p>3. Our ViewModel changes to retrieve the File object. Remember, when we bound the grid we bound to the AccountDocuments EntitySet, which included the Document object, but not the File object. We did this for performance reasons. We only retrieve the File when we need it, one File record at a time.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler DocumentLoaded<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _documentId <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> Document SelectedDocument <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> GetDocumentFile<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> documentId<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    _documentId <span style="color: #008000;">=</span> documentId<span style="color: #008000;">;</span>
    var batch <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DomainContextLoadBatch<span style="color: #008000;">&#40;</span>DocumentLoadComplete<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    batch<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>Context<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDocumentWithFileByIdQuery</span><span style="color: #008000;">&#40;</span>documentId<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DocumentLoadComplete<span style="color: #008000;">&#40;</span>DomainContextLoadBatch batch<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>batch<span style="color: #008000;">.</span><span style="color: #0000FF;">FailedOperationCount</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//foreach (var op in batch.FailedOperations)</span>
            <span style="color: #008080; font-style: italic;">//    HelperFunctions.ProcessException(new Exception(op.Error.Message, op.Error.InnerException), &quot;ProspectDetailsViewModel.cs&quot;);</span>
            DataLoadError<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//Got the data - notify the view</span>
            SelectedDocument <span style="color: #008000;">=</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Documents</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>f <span style="color: #008000;">=&gt;</span> f<span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentID</span> <span style="color: #008000;">==</span> _documentId<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FirstOrDefault</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            DocumentLoaded<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">throw</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>4. Add code in our view to fill our Image object from the returned File object. This code example assumes we wired up the DocumentLoaded event handler when the view was initialized.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #6666cc; font-weight: bold;">void</span> ViewModel_DocumentLoaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">try</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ViewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedDocument</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> imageBytes <span style="color: #008000;">=</span> ViewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedDocument</span><span style="color: #008000;">.</span><span style="color: #0000FF;">File</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentFile</span><span style="color: #008000;">;</span>
                    var bitmapImage <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BitmapImage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    var stream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #008000;">&#40;</span>imageBytes<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    bitmapImage<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSource</span><span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    imgViewer<span style="color: #008000;">.</span><span style="color: #0000FF;">Source</span> <span style="color: #008000;">=</span> bitmapImage<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Notice that we simply take our byte array from our File object and use it to create a BitmapImage. This can then be assigned as the Source of the Image. Cool stuff!</p>
<p>Here is the complete solution for you to download.  It comes as-is and with no warranties, but I hope it is useful.  One other thing regarding the source code &#8211; I use <a title="Telerik Silverlight Controls" href="http://www.telerik.com/products/silverlight.aspx">Telerik Silverlight Controls</a> in my projects and this code sample assumes you have these installed.  You can install them for free as a trial basis.</p>
<p><a onclick="javascript: _gaq.push(['_trackPageview', '/userfiles/sqldocuments.zip']);" href="http://www.farreachinc.com/userfiles/sqldocuments.zip">SQLDocuments.zip</a></p>
<p><a onclick="javascript: _gaq.push(['_trackPageview', '/userfiles/sqldocuments.zip']);" href="http://www.farreachinc.com/userfiles/SQLDocuments_Tables.zip">SQLDocuments_Tables.zip</a></p>
<p>Thanks!</p>
<p>Chris Rouw, Far Reach Technologies</p>
<p>MCTS Silverlight 4, Developer</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F07%2F08%2Fstoring-files-in-sql-server-using-wcf-ria-services-and-silverlight-wrap-up%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/07/08/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-wrap-up/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight – Wrap up ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/07/08/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-wrap-up/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Storing Files in SQL Server using WCF RIA Services and Silverlight – Part 3</title>
		<link>http://blog.farreachinc.com/2011/06/26/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3/</link>
		<comments>http://blog.farreachinc.com/2011/06/26/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 02:02:58 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight - Technical]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=604</guid>
		<description><![CDATA[This is the third of three articles which will describe how you can create a Silverlight LOB application that stores and displays documents using FILESTREAM Storage in SQL Server 2008. Configuring FILESTREAM in your database and WCF RIA Services setup. Uploading &#8230; <a href="http://blog.farreachinc.com/2011/06/26/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/06/26/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight – Part 3 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This is the third of three articles which will describe how you can create a Silverlight LOB application that stores and displays documents using FILESTREAM Storage in SQL Server 2008.</p>
<ol>
<li><a href="http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1">Configuring FILESTREAM in your database and WCF RIA Services setup</a>.</li>
<li><a href="http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/">Uploading and Saving files to the database from a Silverlight LOB application</a>.</li>
<li>Viewing files stored in the FILESTREAM from a Silverlight LOB application (<em><strong>this article</strong></em>).</li>
</ol>
<p>The purpose of this article is to show how you can display documents stored via<br />
FILESTREAM in SQL Server in a Silverlight application or ASP.NET website.</p>
<p>In the <a href="http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/">previous article</a> we setup a gridview control which contained a listing of documents filtered by the selected folder (see image below).  This provided the means to allow users to upload and store documents.  For the average user, a quick <a title="online backup" href="https://www.yousendit.com/" target="_blank">online backup</a> client will be a better route.</p>
<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/folders-with-doc-grid3.png"><img class="alignnone size-full wp-image-607" title="folders with doc grid" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/folders-with-doc-grid3.png" alt="" width="640" height="134" /></a></p>
<p>Now that we have documents stored in our database it is time to provide a way to<br />
display them back to our users.  Here are the requirements for our application.</p>
<ol>
<li>Only authorized users can view documents.</li>
<li>A user can open multiple documents at once, in separate windows.</li>
<li>Documents can be viewed via our Silverlight administrative application.</li>
<li>Documents can be viewed via our public-facing website.</li>
</ol>
<p>Given these requirements our approach was to create an ASP.NET page to handle<br />
displaying documents.  This page will perform the following tasks to display a document.</p>
<ol>
<li>Validates that the user is authenticated.</li>
<li>Reads the querystring to get the document id (PK) and document guid.</li>
<li>Reads the Document record via the provided document id.  The guid is then used to further validate that we have the correct document.  This two prone approach reduces the risk of allowing a user entering random document ids to try to view documents which do not belong to them.</li>
<li>If a valid Document record is found, the page validates that the authenticated user has sufficient authority to view the Document record.  This is done by looking up user roles and applying other security rules.  For example, users in account 100 should only be able to see documents tied to account 100.</li>
<li>If the user has access to view the document then we can display the document to the user.  The page determines the type of document and writes out the<br />
document to the browser window.</li>
</ol>
<p>Here is the code demonstrating the above tasks.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Page_Load<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span>
    <span style="color: #008000;">&#123;</span>
        lblError<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//Make sure the user is authenticated</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">User</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Identity</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsAuthenticated</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">int</span> documentID <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> documentGUID <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//Get the Document Id</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Request<span style="color: #008000;">.</span><span style="color: #0000FF;">QueryString</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;ID&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Helpers<span style="color: #008000;">.</span><span style="color: #0000FF;">isNumeric</span><span style="color: #008000;">&#40;</span>Request<span style="color: #008000;">.</span><span style="color: #0000FF;">QueryString</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;ID&quot;</span><span style="color: #008000;">&#93;</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">NumberStyles</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Integer</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    documentID <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>Request<span style="color: #008000;">.</span><span style="color: #0000FF;">QueryString</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;ID&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//Use the GUID as a second line of security so people can't just type in document IDs to view documents.</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Request<span style="color: #008000;">.</span><span style="color: #0000FF;">QueryString</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DocID&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                documentGUID <span style="color: #008000;">=</span> Request<span style="color: #008000;">.</span><span style="color: #0000FF;">QueryString</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DocID&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>documentID <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&amp;&amp;</span> documentGUID <span style="color: #008000;">!=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                MyDomainService service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyDomainService<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                MyApplication<span style="color: #008000;">.</span><span style="color: #0000FF;">RIALibrary</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Document</span> doc <span style="color: #008000;">=</span> service<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDocumentById</span><span style="color: #008000;">&#40;</span>documentID<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>doc <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>doc<span style="color: #008000;">.</span><span style="color: #0000FF;">guid</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>documentGUID<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>validateDocumentSecurity<span style="color: #008000;">&#40;</span>doc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                            processDocument<span style="color: #008000;">&#40;</span>doc<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            lblError<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;You do not have security to view this document.&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ThreadAbortException</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//do nothing</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Helpers<span style="color: #008000;">.</span><span style="color: #0000FF;">ProcessException</span><span style="color: #008000;">&#40;</span>ex, <span style="color: #666666;">&quot;Page_Load - Document.aspx&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">throw</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Write out the document based on the type of document</span>
<span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> processDocument<span style="color: #008000;">&#40;</span>Document doc<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ClearContent</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">AppendHeader</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;content-length&quot;</span>, doc<span style="color: #008000;">.</span><span style="color: #0000FF;">File</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentFile</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">string</span> pathLower <span style="color: #008000;">=</span> doc<span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.pdf&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/pdf&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.xlsx&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.xls&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/vnd.ms-excel&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.docx&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/vnd.openxmlformats-officedocument.wordprocessingml.document&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.doc&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/msword&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.zip&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/zip&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.gif&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;image/gif&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.tiff&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;image/tiff&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.bmp&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;image/bmp&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.png&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;image/png&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.htm&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;text/html&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;text/plain&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pathLower<span style="color: #008000;">.</span><span style="color: #0000FF;">EndsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;.msg&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">AddHeader</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;content-disposition&quot;</span>, <span style="color: #666666;">&quot;attachment; filename=test.msg&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;image/jpeg&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">BinaryWrite</span><span style="color: #008000;">&#40;</span>doc<span style="color: #008000;">.</span><span style="color: #0000FF;">File</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DocumentFile</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Flush</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Thread</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In the processDocument method you will notice the different file extensions that we support.  Your application could have a different list, but the concept is the same.  Another key point in the code is where we write out the DocumentFile via BinaryWrite.</p>
<p><strong>Bonus &#8211; Storing and Retrieving Outlook messages</strong></p>
<p>One cool thing we found was that we were able to drag an email message from Outlook to be stored in our application.  Drag a message from Outlook to your desktop, then drag onto the drop area in the Silverlight application and the document is stored.  To view the message write out a Header (instead of ContentType) indicating that the file should be opened as an attachment.  The document opens up in Outlook as an email (with attachments) &#8211; very cool!</p>
<p>The nice thing about using an ASP.NET page to display documents is that you can launch the page from both Silverlight and ASP.NET.</p>
<p>That&#8217;s it!  I hope you found this series to be useful. I welcome questions and comments.  Thanks!</p>
<p>Chris Rouw, Far Reach Technologies</p>
<p>MCTS Silverlight 4, Developer</p>
<p>&nbsp;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F06%2F26%2Fstoring-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/06/26/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight – Part 3 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/06/26/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-3/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Storing Files in SQL Server using WCF RIA Services and Silverlight &#8211; Part 2</title>
		<link>http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/</link>
		<comments>http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 03:50:36 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight - Technical]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[WCF RIA Services]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=569</guid>
		<description><![CDATA[This is the second of three articles which will describe how you can create a Silverlight LOB application that stores and displays documents using FILESTREAM Storage in SQL Server 2008. 1. Configuring FILESTREAM in your database and WCF RIA Services &#8230; <a href="http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight &#8211; Part 2 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This is the second of three articles which will describe how you can create a Silverlight LOB application that stores and displays documents using FILESTREAM Storage in SQL Server 2008.</p>
<p>1. <a title="Storing Files in SQL Server using WCF RIA Services and Silverlight - Part 1" href="http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1">Configuring FILESTREAM in your database and WCF RIA Services setup</a>.<br />
2. Uploading and Saving files to the database from a Silverlight LOB application (<strong><em>this article</em></strong>).<br />
3. Viewing files stored in the FILESTREAM from a Silverlight LOB application.</p>
<p>The purpose of this article is to show how you can setup a Silverlight application to allow users to drag and drop files on the application and have them automatically uploaded and saved via WCF RIA Services and the FILESTREAM in SQL Server.</p>
<p>Here is a picture of the screen design in our Silverlight application.</p>
<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/folders-with-doc-grid2.png"><img class="alignnone size-full wp-image-571" title="folders with doc grid" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/folders-with-doc-grid2.png" alt="" width="640" height="127" /></a></p>
<p>A few things to note:</p>
<ul>
<li>A treeview control is used to display the folders</li>
<li>A grid control displays the documents for the selected folder</li>
<li>A rectangle control is used for the drop area</li>
<li>Documents can be viewed by clicking on the document title.  We will discuss how this is done in part 3.</li>
<li>An icon is used to display the file type of the document.  The extension of the file dictates which icon to use.</li>
</ul>
<p>Here is the XAML for the drop area (I am omitting the rest of the XAML so we can focus on the drag/drop functionality).</p>
<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/imagedrop_xaml.png"><img class="alignnone size-full wp-image-577" title="imagedrop_xaml" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/imagedrop_xaml.png" alt="" width="597" height="306" /></a></p>
<p>You will notice a couple of things about this code.</p>
<ul>
<li>The property &#8220;AllowDrop&#8221; is set to &#8220;True&#8221;</li>
<li>The Drop event is wired up (ImageDrop_Drop) to fire when files are dropped in the rectangle. This event will start the upload/save process.</li>
<li>The DragEnter and DragLeave events are also wired up. I find it is good for the user experience to  visually indicate something when hovering over the drop area.</li>
</ul>
<p>Okay, now let&#8217;s look at the events we just wired up. The first two events, DragEnter and DragLeave are used to change the opacity of the rectangle. You can do whatever you want<br />
here, but I prefer the subtleness of the opacity change.</p>
<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/imagedrop_events.png"><img class="alignnone size-full wp-image-580" title="imagedrop_events" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/imagedrop_events.png" alt="" width="498" height="168" /></a></p>
<p>Next is the Drop event. There are lots of moving parts here, so I&#8217;ll show the code<br />
and then walk you through.</p>
<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/imagedrop_drop.png"><img class="alignnone size-full wp-image-579" title="imagedrop_drop" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/imagedrop_drop.png" alt="" width="578" height="313" /></a></p>
<p>Here is the UploadImages method:</p>
<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/uploadimages.png"><img class="alignnone size-full wp-image-581" title="uploadimages" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/uploadimages.png" alt="" width="492" height="412" /></a></p>
<p><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/uploadimages2.png"><img class="alignnone size-full wp-image-582" title="uploadimages2" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/uploadimages2.png" alt="" width="679" height="609" /></a></p>
<p>Some things to note about the Drop event and supporting code:</p>
<ul>
<li>Multiple documents can be dropped at once. A Queue is a great tool to handle this.</li>
<li>We are using a class, UploadedDocument, to store information about each file being processed.  This will eventually be stored in the Document and File tables in SQL.</li>
<li>The file name and extension are pulled from FileInfo to be stored in the Document table. The extension property will be used to drive the file type icon in the grid.</li>
<li>The actual document is then streamed into the DocumentBuffer property, which will later be stored in the File table.</li>
<li>Once all of the UploadedDocument records are created then we create our objects in our ViewModel to be saved via WCF RIA Services.</li>
<li>For each file we create a File object, Document object, and AccountDocument object. The AccountDocument object is then tied to our main Account object. What we are doing here is adding a document to my Account.</li>
</ul>
<p>The new objects are saved via WCF RIA Services and the databound grid is refreshed showing the file(s) which were added to the selected folder.</p>
<p>That&#8217;s it!  We now have documents being saved in our SQL database from our Silverlight application.</p>
<p>The third part of this series will show how to use a OpenFile Dialog to select a file(s) to be uploaded/saved as well as how to display the documents back to the user.</p>
<p>&nbsp;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F06%2F15%2Fstoring-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight &#8211; Part 2 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/06/15/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-2-2/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Storing Files in SQL Server using WCF RIA Services and Silverlight &#8211; Part 1</title>
		<link>http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1/</link>
		<comments>http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 13:42:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight - Technical]]></category>
		<category><![CDATA[FILESTREAM]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[WCF RIA Services]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=531</guid>
		<description><![CDATA[We have worked on several Silverlight Line of Business applications that require storing documents and files in a secure environment. There are several ways to accomplish this but one approach that has been successful for us is to store the &#8230; <a href="http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight &#8211; Part 1 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>We have worked on several <a href="http://www.farreachinc.com/Silverlight-Development">Silverlight Line of Business applications</a> that require storing documents and files in a secure environment. There are several ways to accomplish this but one approach that has been successful for us is to store the documents using FILESTREAM Storage in SQL Server 2008.</p>
<p>This is the first of three articles which will describe how you can create a Silverlight LOB application that stores and displays documents using FILESTREAM Storage in SQL Server 2008.</p>
<p>1. Configuring FILESTREAM in your database and WCF RIA Services setup.<br />
2. Uploading and Saving files to the database from a Silverlight LOB application.<br />
3. Viewing files stored in the FILESTREAM from a Silverlight LOB application.</p>
<p><strong>Configuring FILESTREAM in you database</strong></p>
<p>The first thing I would recommend is to read about FILESTREAM. Here is a <a title="FILESTREAM White Paper" href="http://msdn.microsoft.com/en-us/library/cc949109(v=sql.100).aspx" target="_blank">white paper </a>which describes FILESTREAM. Okay, now that you read the entire white paper we are ready to roll!</p>
<p><strong>Setting up your database</strong></p>
<p>Your database needs to enable FILESTREAM on the instance of the SQL Server Database Engine. Here is an <a href="http://msdn.microsoft.com/en-us/library/cc645923.aspx" target="_blank">article which describes this process</a>.</p>
<p>Now that the FILESTREAM is enabled for the server you need to configure your database. Here is an <a href="http://www.mssqltips.com/tip.asp?tip=1850" target="_blank">article that walks you through this process</a>. The basic steps include:</p>
<p>1. Create a Filegroup of type FILESTREAM</p>
<div id="attachment_537" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMgroup.png"><img class="size-medium wp-image-537" title="FILESTREAMgroup" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMgroup-300x237.png" alt="Configuring the FILESTREAM group" width="300" height="237" /></a><p class="wp-caption-text">Configuring the FILESTREAM group</p></div>
<p>2. Create a File for the new FILESTREAM group</p>
<div id="attachment_535" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMfiles.png"><img class="size-medium wp-image-535" title="FILESTREAMfiles" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMfiles-300x208.png" alt="Configuring the FILESTREAM file" width="300" height="208" /></a><p class="wp-caption-text">Configuring the FILESTREAM file</p></div>
<p>Now that your  database can handle FILESTREAM, the next is to create the SQL Tables that will store documents using the FILESTREAM.  In this example I will be using three tables:</p>
<ul>
<li>File &#8211; storage for the document via the FILESTREAM</li>
<li>Document &#8211; metadata about the File</li>
<li>Folder &#8211; Virtual folder for the document</li>
</ul>
<p><strong>File table script</strong></p>
<div><code><br />
CREATE TABLE [dbo].[File](<br />
[FileID] [int] IDENTITY(1,1) NOT NULL,<br />
[DocumentFileId] [uniqueidentifier] ROWGUIDCOL NOT NULL,<br />
[DocumentFile] [varbinary](max) FILESTREAM NULL,<br />
CONSTRAINT [File_PK] PRIMARY KEY CLUSTERED<br />
(<br />
[FileID] ASC<br />
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] FILESTREAM_ON [FileStreamGroup1],<br />
UNIQUE NONCLUSTERED<br />
(<br />
[DocumentFileId] ASC<br />
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]<br />
) ON [PRIMARY] FILESTREAM_ON [FileStreamGroup1]<br />
</code></div>
<p>&nbsp;</p>
<p>One thing we have found is that you only want to access the File table when you are ready to display the document. If you include this table in RIA Service Domain Service it will really slow things down &#8211; a lot. That is why we separated the metadata from the FILESTREAM into two tables &#8211; File and Document.</p>
<p><strong>Document table</strong>. You can add as many columns for metadata as needed for your project.</p>
<div id="attachment_534" class="wp-caption alignnone" style="width: 310px"><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMDocument.png"><img class="size-full wp-image-534" title="FILESTREAMDocument" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMDocument.png" alt="Document Table" width="300" height="255" /></a><p class="wp-caption-text">Document Table - used to store metadata about the document</p></div>
<p>A few things to notice:</p>
<ol>
<li>This table contains a description and some metadata about the file. We use this table to bind a list of documents in a treeview or gridview control.</li>
<li>The guid field is used to create a second unique field. More to come on this in part 3.</li>
<li>Path will store the actual file name (e.g. MyDocument.pdf). We need this field so we can determine the type of file that is stored in the database (more on this in part 3).</li>
<li>FolderID points to a Folder table (see below). We use this table to organize documents in Folders.</li>
</ol>
<p><strong>Folder table</strong>. We use the ParentFolderID to enable nested folders.</p>
<div id="attachment_536" class="wp-caption alignnone" style="width: 305px"><a href="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMFolder.png"><img class="size-full wp-image-536" title="FILESTREAMFolder" src="http://blog.farreachinc.com/wp-content/uploads/2011/06/FILESTREAMFolder.png" alt="Folder table" width="295" height="256" /></a><p class="wp-caption-text">Stores the location of the document</p></div>
<p>Okay, now our database is configured for FILESTREAM and we have the necessary tables to store documents. We are creating a Silverlight LOB application using WCF RIA Services, so assuming we already have our Silverlight project created our next steps will be:</p>
<ol>
<li>Add/Update Entity Framework Entity Data Model (*.edmx) in the project. Include the File, Document, and Folder tables.</li>
<li>Add/Update Domain Service class and metadata for the three tables.</li>
</ol>
<p>Tip &#8211; I like to include two methods when returning a Document. One that includes the File (i.e. Heavy version) and one that does not include the File (i.e. Lightweight version). This gives me flexibility on the client side.</p>
<div><code><br />
public Document GetDocumentById(int documentId)<br />
{<br />
return this.ObjectContext.Documents.Where(d =&gt; d.DocumentID == documentId).FirstOrDefault();<br />
}<br />
public Document GetDocumentWithFileById(int documentId)<br />
{<br />
return this.ObjectContext.Documents.Include("File")<br />
.Where(d =&gt; d.DocumentID == documentId).FirstOrDefault();<br />
}</code></div>
<p>&nbsp;</p>
<p>To get a list of documents for a folder I use the following query. This can be bound to a gridview control.</p>
<p><code><br />
public IQueryable GetDocumentsByFolderId(int folderId)<br />
{<br />
return this.ObjectContext.Folders<br />
.Include("Document")<br />
.Where(f =&gt; f.FolderID == folderId).OrderByDescending(com =&gt; com.Document.CreatedDate);<br />
}<br />
</code></p>
<p>With these changes in place your server side should be ready to handle the storage of documents, which we will discuss in part 2.</p>
<p>Resources:</p>
<ul>
<li>FILESTREAM Storage in SQL<br />
Server 2008: <a href="http://msdn.microsoft.com/en-us/library/cc949109(v=sql.100).aspx">http://msdn.microsoft.com/en-us/library/cc949109(v=sql.100).aspx</a></li>
<li>Best Practices: <a href="http://coolthingoftheday.blogspot.com/2009/11/sql-server-2008-filestream-best.html">http://coolthingoftheday.blogspot.com/2009/11/sql-server-2008-filestream-best.html</a></li>
</ul>
<p>&nbsp;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F06%2F07%2Fstoring-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1/' addthis:title='Storing Files in SQL Server using WCF RIA Services and Silverlight &#8211; Part 1 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/06/07/storing-files-in-sql-server-using-wcf-ria-services-and-silverlight-part-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Locking and Unlocking controls in a Silverlight LOB Application</title>
		<link>http://blog.farreachinc.com/2011/05/10/locking-and-unlocking-controls-in-a-silverlight-lob-application/</link>
		<comments>http://blog.farreachinc.com/2011/05/10/locking-and-unlocking-controls-in-a-silverlight-lob-application/#comments</comments>
		<pubDate>Tue, 10 May 2011 12:37:19 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight - Technical]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=492</guid>
		<description><![CDATA[Recently we were working on a Silverlight Line of Business application with a requirement that all data fields should be read only until the user clicks an Edit button.  This was the requirement for all screens and there are quite &#8230; <a href="http://blog.farreachinc.com/2011/05/10/locking-and-unlocking-controls-in-a-silverlight-lob-application/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/05/10/locking-and-unlocking-controls-in-a-silverlight-lob-application/' addthis:title='Locking and Unlocking controls in a Silverlight LOB Application ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Recently we were working on a <a title="Silverlight Development" href="http://www.farreachinc.com/Silverlight-Development" target="_blank">Silverlight Line of Business application </a>with a requirement that all data fields should be read only until the user clicks an Edit button.  This was the requirement for all screens and there are quite a number of screens in the application.  We thought about this for a bit and decided it would be most efficient to come up with a technique we could write once and apply across all screens.  This entry describes our solution.</p>
<p>Our approach simply involves creating a method which accepts a control and a boolean parameter and then loops through all children controls of the passed control and sets the appropriate property based on the bool parameter (e.g. IsEnabled = false;).  Here are the details.</p>
<p>Created a public static class that can be used by all screens in the system.</p>
<p><code>namespace FarReach.App.Helpers<br />
{<br />
public static class HelperFunctions<br />
{<br />
}<br />
}</code></p>
<p>Within this class we created public static methods that are called by the screens. There is one method for Lock and another for Unlock. Notice that these methods accept a control of type Panel, which means that in each screen we will wrap all of the controls to lock/unlock in a StackPanel.<br />
<code><br />
public static void LockChildControls(Panel parentControl)<br />
{<br />
EditChildControls(parentControl, true);<br />
}</code></p>
<p>public static void UnlockChildControls(Panel parentControl)<br />
{<br />
EditChildControls(parentControl, false);<br />
}</p>
<p>Now the guts of things &#8211; the EditChildControls method. You will note that we loop through all child controls of the parent control which was passed in by the screen. We look for specific types of controls to Lock/Unlock and process them appropriately. Note also that we are using <a title="Telerik Silverlight Controls" href="http://www.telerik.com/products/silverlight.aspx" target="_blank">Telerik Silverlight controls </a>and add cases for those controls. Another item to note is that for some container controls like Grid, Border, and StackPanel, we call EditChildControls so that we can Lock/Unlock their children controls.<br />
<code><br />
private static void EditChildControls(Panel parentControl, bool isLock)<br />
{<br />
foreach (var control in parentControl.Children)<br />
{<br />
switch (control.GetType().Name.ToLower())<br />
{<br />
case "textbox":<br />
((TextBox)control).IsReadOnly = isLock;<br />
break;<br />
case "checkbox":<br />
((CheckBox)control).IsEnabled = !isLock;<br />
break;<br />
case "radmaskedtextinput":<br />
((RadMaskedTextInput)control).IsReadOnly = isLock;<br />
break;<br />
case "radcombobox":<br />
((RadComboBox)control).IsEnabled = !isLock;<br />
break;<br />
case "raddatepicker":<br />
((RadDatePicker)control).IsReadOnly = isLock;<br />
break;<br />
case "radgridview":<br />
RadContextMenu.GetContextMenu((RadGridView)control).IsEnabled = !isLock;<br />
break;<br />
case "stackpanel":<br />
EditChildControls((StackPanel)control, isLock);<br />
break;<br />
case "grid":<br />
EditChildControls((Grid)control, isLock);<br />
break;<br />
case "border":<br />
EditChildControls((Border)control, isLock);<br />
break;<br />
}<br />
}<br />
}</code></p>
<p>private static void EditChildControls(Border parentControl, bool isLock)<br />
{<br />
if (parentControl.Child != null)<br />
{<br />
switch (parentControl.Child.GetType().Name.ToLower())<br />
{<br />
case &#8220;stackpanel&#8221;:<br />
EditChildControls((StackPanel) parentControl.Child, isLock);<br />
break;<br />
case &#8220;grid&#8221;:<br />
EditChildControls((Grid) parentControl.Child, isLock);<br />
break;<br />
}<br />
}<br />
}</p>
<p>Now that we have all of this setup it is easy to implement from the screen. All we have to do is wrap all of our controls within a StackPanel and then call the Lock/Unlock function in code.<br />
<code><br />
private void SetToViewMode()<br />
{<br />
btnEdit.Visibility = Visibility.Visible;<br />
btnSave.Visibility = Visibility.Collapsed;<br />
HelperFunctions.LockChildControls(stackOrgInfo);<br />
HelperFunctions.LockChildControls(stackBillingInfo);<br />
}</code></p>
<p>private void SetToEditMode()<br />
{<br />
btnEdit.Visibility = Visibility.Collapsed;<br />
btnSave.Visibility = Visibility.Visible;<br />
HelperFunctions.UnlockChildControls(stackOrgInfo);<br />
HelperFunctions.UnlockChildControls(stackBillingInfo);<br />
}<br />
That&#8217;s it! Now all of our screens can be locked and unlocked with ease. I hope you find this tip useful. I would be happy to hear you thoughts or techniques you may have implemented regarding this type of requirement.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F05%2F10%2Flocking-and-unlocking-controls-in-a-silverlight-lob-application%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/05/10/locking-and-unlocking-controls-in-a-silverlight-lob-application/' addthis:title='Locking and Unlocking controls in a Silverlight LOB Application ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/05/10/locking-and-unlocking-controls-in-a-silverlight-lob-application/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>MIX11 Highlights</title>
		<link>http://blog.farreachinc.com/2011/04/18/mix11-highlights/</link>
		<comments>http://blog.farreachinc.com/2011/04/18/mix11-highlights/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 08:20:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight - Technical]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=408</guid>
		<description><![CDATA[By now everyone should have recovered from MIX11 and had some time to think about what we saw and learned during that 72 hour conversation.  Here are some of my thoughts from the conference.  (View MIX11 Sessions) &#160; Windows Phone &#8230; <a href="http://blog.farreachinc.com/2011/04/18/mix11-highlights/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/04/18/mix11-highlights/' addthis:title='MIX11 Highlights ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>By now everyone should have recovered from <a title="http://live.visitmix.com/" href="http://live.visitmix.com/" target="_blank">MIX11</a> and had some time to think about what we saw and learned during that 72 hour conversation.  Here are some of my thoughts from the conference.  (<a title="http://channel9.msdn.com/events/mix/mix11" href="http://channel9.msdn.com/events/mix/mix11" target="_blank">View MIX11 Sessions</a>)</p>
<p>&nbsp;</p>
<p><strong>Windows Phone 7</strong></p>
<ul>
<li>I was surprised to hear that <a title="http://www.scottlogic.co.uk/blog/colin/2011/04/gartner-predicts-a-promising-future-for-windows-phone-7-developers/" href="http://www.scottlogic.co.uk/blog/colin/2011/04/gartner-predicts-a-promising-future-for-windows-phone-7-developers/" target="_blank">Gartner is projecting WP7 to surpass iOS in 2014</a>.  This is great news if you are a WP7 developer.</li>
<li>WP7 to Verizon &#8211; I asked a couple of people at the Windows Phone booth about the date when we could expect Verizon to carry WP7.  &#8220;Soon&#8221; was the answer.  I was not able to determine if &#8220;soon&#8221; was like in the next month or this summer.  Whenever this happens I can see a lot more people jumping on to WP7.</li>
<li>The next version of <a title="http://arstechnica.com/microsoft/news/2011/04/windows-phone-7-mango-one-heck-of-an-upgrade.ars" href="http://arstechnica.com/microsoft/news/2011/04/windows-phone-7-mango-one-heck-of-an-upgrade.ars" target="_blank">Windows Phone 7, Mango</a>, got a lot of love at the keynote.  This version will contain many new features including multi-tasking, fast app switching, IE9 browser with HTML5 support, and built-in SQL database.  Some other promising features include Live Agents, which allows background processes to run and preserves battery life, deep linking tiles, which allows a user to jump to a specific page in your app like a barcode reader, and access to contacts and calendar on the phone.</li>
</ul>
<ul>
<li>New WP7 developer tools will be released in about a month.  Highlighting these tools are improvements to the emulator and profiling support.  The emulator will now have additional tools to simulate the accelerometer, which includes a shake gesture.  I was very pleased to see that the emulator will have a Location tool as well.  This tool will allow users to test different locations or simulate movement from one location to another.  A definite time saver!</li>
</ul>
<ul>
<li>WP7 tools &#8211; Profiling support.  These profiling tools will allow users to analyze execution and memory usage.  This will be a great tool for developers.</li>
</ul>
<ul>
<li>Will these new WP7 tools work with the current version?  I asked a Microsoft phone person this very question and was surprised that they did not know for sure.  This person indicated that they tools should be compatible with the current version.  Thoughts anyone?</li>
</ul>
<p>&nbsp;</p>
<p><strong>Silverlight</strong></p>
<ul>
<li>Silverlight 5 Beta has been released.  See <a title="http://timheuer.com/blog/archive/2011/04/13/whats-new-in-silverlight-5-a-guide.aspx" href="http://timheuer.com/blog/archive/2011/04/13/whats-new-in-silverlight-5-a-guide.aspx" target="_blank">Tim Heuer&#8217;s blog entry </a>about getting started with Silverlight 5 Beta.</li>
<li><a title="http://johnpapa.net/" href="http://johnpapa.net/" target="_blank">John Papa&#8217;s</a> demo of the 3D home model built using Silverlight 5 Beta was cool, but didn&#8217;t really give me a flavor for what Silverlight 5 has to offer me as a developer of business apps.</li>
<li><a title="http://channel9.msdn.com/Events/MIX/MIX11/MED07" href="http://channel9.msdn.com/Events/MIX/MIX11/MED07" target="_blank">Silverlight Performance Session</a> by <a title="http://channel9.msdn.com/Shows/SilverlightTV/Silverlight-TV-57-Performance-Tuning-Your-Apps" href="http://channel9.msdn.com/Shows/SilverlightTV/Silverlight-TV-57-Performance-Tuning-Your-Apps" target="_blank">Mike Cook</a>.  This is a must for all Silverlight developers.  Please take time to view this session.  Mike provides some great resources, tools, and tips for improving performance of Silverlight apps.</li>
</ul>
<p>&nbsp;</p>
<p><strong>Other Items</strong></p>
<ul>
<li><a title="http://msdn.microsoft.com/en-us/vstudio/async" href="http://msdn.microsoft.com/en-us/vstudio/async" target="_blank">Visual Studio Asynchronous Programming</a> looks to be a promising tool for Windows Phone 7 developers to turn ugly async calls into a synchronous love-fest.  There is a CTP available for developers to try.</li>
</ul>
<ul>
<li><a title="http://www.microsoft.com/surface/" href="http://www.microsoft.com/surface/" target="_blank">Microsoft Surface</a>.  I attended a couple of sessions on Microsoft Surface on day one of the conference.  We got to see some Surface 2.0 applications in action and hear some thoughts from Surface MVPs and Microsoft on the future of Surface.  What I took from all of this is that a) Surface is very cool with great technology and b) Surface is still not ready for primetime yet.  I say this since the Surface 2 machines still cost around $7900 and the technology is still so new.  On the plus side the Surface 2 SDK will be released in the next few months for developers to play.</li>
<li>Kinect and <a title="http://research.microsoft.com/en-us/um/redmond/projects/kinectsdk/" href="http://research.microsoft.com/en-us/um/redmond/projects/kinectsdk/" target="_blank">Kinect SDK</a>.  Yes, it was very cool to get a Kinect at the conference.  It did cause some issues at the security check at the airport, though.  The SDK will be fun to play with when it comes out &#8211; I&#8217;m not sure how my business clients will use this but I&#8217;m sure we can find some way to incorporate the SDK into one of our projects!</li>
</ul>
<p>&nbsp;</p>
<p>At Far Reach we embrace new technologies and always look for ways to use them to create better experiences for our clients.  What did you think about MIX11?</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F04%2F18%2Fmix11-highlights%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/04/18/mix11-highlights/' addthis:title='MIX11 Highlights ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/04/18/mix11-highlights/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MIX11 &#8211; Keynote Notes</title>
		<link>http://blog.farreachinc.com/2011/04/13/mix11-keynote-notes/</link>
		<comments>http://blog.farreachinc.com/2011/04/13/mix11-keynote-notes/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 13:31:14 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight - Technical]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=410</guid>
		<description><![CDATA[Day one of MIX11 is in the books and as Larry David would say, &#8220;it was pret-ty, pret-ty good&#8221;.  There were lots of great sessions, good food, and good conversations occurring.  One of the best things about these conferences is &#8230; <a href="http://blog.farreachinc.com/2011/04/13/mix11-keynote-notes/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/04/13/mix11-keynote-notes/' addthis:title='MIX11 &#8211; Keynote Notes ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Day one of <a title="http://live.visitmix.com" href="http://live.visitmix.com" target="_blank">MIX11</a> is in the books and as Larry David would say, &#8220;it was pret-ty, pret-ty good&#8221;.  There were lots of great sessions, good food, and good conversations occurring.  One of the best things about these conferences is talking to people you see online.  I had the opportunity to briefly chat with <a title="www.jesseliberty.com" href="http://www.jesseliberty.com" target="_blank">Jesse Liberty</a> and <a title="http://channel9.msdn.com/Tags/deepesh-mohnani" href="http://channel9.msdn.com/Tags/deepesh-mohnani" target="_blank">Deepesh Mohnani</a>.</p>
<p>The <a title="http://live.visitmix.com/Keynotes" href="http://live.visitmix.com/Keynotes" target="_blank">keynote</a> was kicked off by <a title="Dean Hachamovitch" href="http://www.microsoft.com/presspass/exec/hachamovitch/" target="_blank">Dean Hachamovitch</a>, VP, Internet Explorer.  The topic was &#8220;HTML5: Native to Windows&#8221;.  There were lots of impressive demos about IE9 and HTML5.  As a Silverlight developer these presentations inspired me to learn more about where HTML5 and Silverlight stood in regards to the future and the web.</p>
<p><strong>Related Links:</strong></p>
<ul>
<li>
<div><a href="http://Foursquareplayground.com" target="_blank">Foursquareplayground.com</a></div>
</li>
<li>
<div><a title="Director.bonjovi.com" href="http://Director.bonjovi.com" target="_blank">Director.bonjovi.com</a></div>
</li>
<li>
<div><a title="Worldsbiggestpacman.com" href="http://Worldsbiggestpacman.com" target="_blank">Worldsbiggestpacman.com</a></div>
</li>
<li>
<div><a href="http://www.beautyoftheweb.com/">www.Beautyoftheweb.com</a></div>
</li>
<li>
<div><a href="http://www.html5labs.com/">www.Html5labs.com</a></div>
</li>
</ul>
<p>Scott Guthrie then took the stage to discuss the latest in the .NET world.  Some highlights from his talk.</p>
<ul>
<li>Nuget &#8211; free open source library manager</li>
<li>jQuery &#8211; Microsoft has been contributing</li>
<li>ASP.NET MVC3 Tools update</li>
<li>jQuery 1.5 + Modernizr</li>
<li>Entity Framework 4.1 (shipped officially on 4/11 &#8211; works with all .NET projects).  Contains Code first capability.</li>
<li>Scaffolding &#8211; Generate controllers and views off of EF models</li>
</ul>
<p>&nbsp;</p>
<p><a title="http://www.hanselman.com/blog/" href="http://www.hanselman.com/blog/" target="_blank">Scott Hanselman</a>, Developer Division, then provided some demos on using MVC3 to create an administrative system for &#8220;This Developer&#8217;s Life&#8221; <a href="http://www.thisdeveloperslife.com/">http://www.thisdeveloperslife.com/</a> podcast.  Later he used <a title="http://www.microsoft.com/web/webmatrix/" href="http://www.microsoft.com/web/webmatrix/" target="_blank">Microsoft WebMatrix</a> to build the front end of the site.  I would encourage you to check out these tools.</p>
<p>Stay tuned for additional blog posts as I continue to learn and meet great people.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F04%2F13%2Fmix11-keynote-notes%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/04/13/mix11-keynote-notes/' addthis:title='MIX11 &#8211; Keynote Notes ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/04/13/mix11-keynote-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 Insights #4: Detecting and Reacting to Connectivity</title>
		<link>http://blog.farreachinc.com/2011/04/04/wp7-insights-4-detecting-and-reacting-to-connectivity/</link>
		<comments>http://blog.farreachinc.com/2011/04/04/wp7-insights-4-detecting-and-reacting-to-connectivity/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 13:31:41 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight - Technical]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=412</guid>
		<description><![CDATA[This article is the fourth in a series which discusses some of the things that I learned while developing my first Windows Phone 7 application.  Update &#8211; I now have two apps in the WP7 marketplace, so I have more &#8230; <a href="http://blog.farreachinc.com/2011/04/04/wp7-insights-4-detecting-and-reacting-to-connectivity/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/04/04/wp7-insights-4-detecting-and-reacting-to-connectivity/' addthis:title='WP7 Insights #4: Detecting and Reacting to Connectivity ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This article is the fourth in a series which discusses some of the things that I learned while developing my first Windows Phone 7 application.  Update &#8211; I now have two apps in the WP7 marketplace, so I have more insights to share.</p>
<p>My<a title="Sturgis Falls app in Zune marketplace" href="http://social.zune.net/redirect?type=phoneApp&amp;id=048bac8b-6451-e011-854c-00237de2db9e" target="_blank"> latest WP7 app </a>displays events for our community&#8217;s annual summer celebration, <a title="Sturgis Falls" href="http://www.sturgisfalls.org" target="_blank">Sturgis Falls</a>.  This app displays a schedule which can be viewed by venue, day, or category.  Displaying the events and other data relies on an active connection with your phone.  One mistake I made which was caught during the <a title="Windows Phone 7 Certification Requirements" href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBwQFjAA&amp;url=http%3A%2F%2Fdownload.microsoft.com%2Fdownload%2F1%2F2%2FD%2F12D67308-29EE-463D-A442-142F6982AECE%2FWindows%2520Phone%25207%2520Application%2520Certification%2520Requirements.pdf&amp;ei=k8qZTcy7BYbKgQePiI3BCg&amp;usg=AFQjCNH3Bnm8mOXJT4u027VoC85olKF7RQ&amp;sig2=oUsLEAASIK13rX7Vu87X8Q" target="_blank">Microsoft Certification process</a> was that I did not handle scenarios which require a data connection but no connection was available.  This resulted in a certification failure. #Fail</p>
<p>Fortunately it is pretty easy to detect and react to network servicing Internet requests.  Start by using the <a title="NetworkInformation" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.net.networkinformation.networkinterface(v=VS.92).aspx" target="_blank">NetworkInterface Class</a>, which is in Microsoft.Phone.dll.  You can use this class to check the network type and detect when the network type is changed.</p>
<p>In my app I only needed to check the network type when my page loaded since that is when I utilize the network connection.  The following code sample runs when a user selects a Venue from a list of Venues.  This page will load all events for the selected Venue.  If there is no network connection then I disable the progress bar and provide feedback to the user.  Note &#8211; Instead of changing the TextBlock to say &#8220;No Connection&#8221; I could have called MessageBox.Show() to pop a message to the user.</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">     void VenueDetails_Loaded<span class="br0">&#40;</span>object sender, RoutedEventArgs e<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
      if <span class="br0">&#40;</span>NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.None<span class="br0">&#41;</span>
      <span class="br0">&#123;</span>
          myProgress.Visibility = Visibility.Collapsed;
          PageTitle.Text = &quot;No Connection&quot;;
      <span class="br0">&#125;</span>
      else
      <span class="br0">&#123;</span>
          _viewModel.LoadEventsByVenueId<span class="br0">&#40;</span>_venueId<span class="br0">&#41;</span>;
          PageTitle.Text = _viewModel.GetVenueNameByVenueId<span class="br0">&#40;</span>_venueId<span class="br0">&#41;</span>;
      <span class="br0">&#125;</span>
  <span class="br0">&#125;</span></pre></div></div>

<p>One other tip related to connectivity relates to my <a href="http://silverlight.farreachinc.com/post/2011/03/23/WP7-Insights-3-Storing-User-State-Activating-and-Deactivating.aspx">previous post about activating and deactivating</a>.  When my app closes or deactivates I am saving the events, venues, and other data to Isolated Storage.  When my app starts or reactivates I can check the network type to determine if I should load data from Isolated Storage or from the web service.  The following code demonstrates this.</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">     private void Application_Activated<span class="br0">&#40;</span>object sender, ActivatedEventArgs e<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
      LoadSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>;
  <span class="br0">&#125;</span>
     void LoadSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
      IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
      if <span class="br0">&#40;</span>NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.None<span class="br0">&#41;</span>
      <span class="br0">&#123;</span>
          if <span class="br0">&#40;</span>settings.Contains<span class="br0">&#40;</span>&quot;gEvents&quot;<span class="br0">&#41;</span><span class="br0">&#41;</span>
              Helpers.Globals.gEvents = <span class="br0">&#40;</span>List&lt;SiteLightEvent&gt;<span class="br0">&#41;</span>settings<span class="br0">&#91;</span>&quot;gEvents&quot;<span class="br0">&#93;</span>;
          if <span class="br0">&#40;</span>settings.Contains<span class="br0">&#40;</span>&quot;gVenues&quot;<span class="br0">&#41;</span><span class="br0">&#41;</span>
              Helpers.Globals.gVenues = <span class="br0">&#40;</span>List&lt;SiteLightVenue&gt;<span class="br0">&#41;</span>settings<span class="br0">&#91;</span>&quot;gVenues&quot;<span class="br0">&#93;</span>;
      <span class="br0">&#125;</span>
  <span class="br0">&#125;</span></pre></div></div>

<p>By implementing these techniques I am able to provide a user a better experience by:</p>
<ul>
<li>Using data from my previous session if no network is available</li>
<li>Notify users when no data connection is available</li>
<li>Not locking up any screens when there is no data connection and therefore allowing my app to pass Microsoft Certification Testing.</li>
</ul>
<p>&nbsp;</p>
<p>I hope you find these tips useful.  Thanks!</p>
<p><strong>Related Links:</strong></p>
<ul>
<li>
<div><strong><a href="http://silverlight.farreachinc.com/post/2011/03/04/WP7-Insights-1-Consuming-REST-APIs-within-a-WP7-app.aspx">WP7 Insights #1: Consuming REST APIs within a WP7 app</a></strong></div>
</li>
<li>
<div><strong><a href="http://silverlight.farreachinc.com/post/2011/03/13/WP7-Insights-2-Creating-a-Splash-Screen-in-WP7.aspx">WP7 Insights #2: Creating a Splash Screen in WP7</a></strong></div>
</li>
<li>
<div><strong><a href="http://silverlight.farreachinc.com/post/2011/03/23/WP7-Insights-3-Storing-User-State-Activating-and-Deactivating.aspx">WP7 Insights #3: Storing User State &#8211; Activating and Deactivating</a></strong></div>
</li>
</ul>
<p>&nbsp;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F04%2F04%2Fwp7-insights-4-detecting-and-reacting-to-connectivity%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/04/04/wp7-insights-4-detecting-and-reacting-to-connectivity/' addthis:title='WP7 Insights #4: Detecting and Reacting to Connectivity ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/04/04/wp7-insights-4-detecting-and-reacting-to-connectivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 Insights #3: Storing User State &#8211; Activating and Deactivating</title>
		<link>http://blog.farreachinc.com/2011/03/23/wp7-insights-3-storing-user-state-activating-and-deactivating/</link>
		<comments>http://blog.farreachinc.com/2011/03/23/wp7-insights-3-storing-user-state-activating-and-deactivating/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 13:32:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Silverlight - Technical]]></category>

		<guid isPermaLink="false">http://blog.farreachinc.com/?p=414</guid>
		<description><![CDATA[This article is the third in a series which discusses some of the things that I learned while developing my first Windows Phone 7 application. The Dwolla app contains a login screen which requires a user to enter their Dwolla &#8230; <a href="http://blog.farreachinc.com/2011/03/23/wp7-insights-3-storing-user-state-activating-and-deactivating/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/03/23/wp7-insights-3-storing-user-state-activating-and-deactivating/' addthis:title='WP7 Insights #3: Storing User State &#8211; Activating and Deactivating ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This article is the third in a series which discusses some of the things that I learned while developing my first Windows Phone 7 application.</p>
<p>The <a href="http://social.zune.net/redirect?type=phoneApp&amp;id=7a7a9b83-511e-e011-9264-00237de2db9e" target="_blank">Dwolla</a> app contains a login screen which requires a user to enter their Dwolla credentials in order to view their account.  One common task in many web application login screens is to include a checkbox to allow the user to &#8220;Remember Me&#8221;, so that users don&#8217;t have to rekey their username on every trip to the login page.</p>
<p>To accomplish this in my Windows Phone 7 application, I used two concepts:</p>
<ul>
<li>Using the <a title="PhoneApplicationService class" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.phoneapplicationservice(v=vs.92).aspx" target="_blank">PhoneApplicationService</a> class to wire up events for the application&#8217;s lifetime.</li>
</ul>
<ul>
<li>Using <a title="IsolatedStorageSettings" href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragesettings(v=vs.95).aspx" target="_blank">IsolatedStorageSettings</a> to save and load user information.</li>
</ul>
<p>&nbsp;</p>
<p>Here is the workflow of how this all will work.</p>
<ul>
<li>User enters their username and password on the login screen and checks the Remember Me checkbox.</li>
<li>User does some stuff &#8211; hopefully send me some money <img src='http://blog.farreachinc.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<ul>
<li>User exits the application (via clicking on the Windows button, logout, etc).</li>
</ul>
<ul>
<li>The application fires either the Closing or Deactivated events.  These events call a method to Save Settings.</li>
</ul>
<ul>
<li>The user navigates back to the Dwolla app.</li>
<li>The application fires either the Launching or Activated events.  These events call a method to Load Settings.</li>
<li>The login page is displayed.  The page uses a value set by the Load Settings method to prefill the username field.</li>
</ul>
<p>&nbsp;</p>
<p>Wiring up the events</p>
<p>To start I added the following code to the App.xaml file in my project to wire up events in the PhoneApplicationService class.</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;"> &lt;Application.ApplicationLifetimeObjects&gt;
     &lt;!--Required object that handles lifetime events for the application--&gt;
     &lt;shell:PhoneApplicationService
         Launching=&quot;Application_Launching&quot; Closing=&quot;Application_Closing&quot;
         Activated=&quot;Application_Activated&quot; Deactivated=&quot;Application_Deactivated&quot;/&gt;
 &lt;/Application.ApplicationLifetimeObjects&gt;</pre></div></div>

<p>Here is the code in the App.xaml.cs class for these events:</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">     private void Application_Launching<span class="br0">&#40;</span>object sender, LaunchingEventArgs e<span class="br0">&#41;</span>
     <span class="br0">&#123;</span>
         LoadSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>;
     <span class="br0">&#125;</span>
     private void Application_Activated<span class="br0">&#40;</span>object sender, ActivatedEventArgs e<span class="br0">&#41;</span>
     <span class="br0">&#123;</span>
         LoadSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>;
         // Ensure that application state is restored appropriately
         if <span class="br0">&#40;</span>!App.ViewModel.IsDataLoaded<span class="br0">&#41;</span>
         <span class="br0">&#123;</span>
App.ViewModel.LoadData<span class="br0">&#40;</span><span class="br0">&#41;</span>;
         <span class="br0">&#125;</span>
     <span class="br0">&#125;</span>
     private void Application_Deactivated<span class="br0">&#40;</span>object sender, DeactivatedEventArgs e<span class="br0">&#41;</span>
     <span class="br0">&#123;</span>
         SaveSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>;
     <span class="br0">&#125;</span>
     private void Application_Closing<span class="br0">&#40;</span>object sender, ClosingEventArgs e<span class="br0">&#41;</span>
     <span class="br0">&#123;</span>
         // Ensure that required application state is persisted here.
         SaveSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>;
     <span class="br0">&#125;</span></pre></div></div>

<p>The following code shows how I save the settings for the user.  Note that I created a class, LoginInfo, to store all of my settings.  It worked well for me to create one class to store my Login Information rather than have a bunch of individual settings.  I also have a class called Globals which contains UserLoginInfo, an instance of LoginInfo.</p>
<p>Globals.cs</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">   public static LoginInfo UserLoginInfo = new LoginInfo<span class="br0">&#40;</span><span class="br0">&#41;</span>;</pre></div></div>

<p>App.xaml.cs</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">     void SaveSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>
     <span class="br0">&#123;</span>
         IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
         if <span class="br0">&#40;</span>Globals.UserLoginInfo != null<span class="br0">&#41;</span>
         <span class="br0">&#123;</span>
settings<span class="br0">&#91;</span>&quot;UserLoginInfo&quot;<span class="br0">&#93;</span> = Globals.UserLoginInfo;
settings.Save<span class="br0">&#40;</span><span class="br0">&#41;</span>;
         <span class="br0">&#125;</span>
     <span class="br0">&#125;</span></pre></div></div>

<p>Here is the code to load the settings previously saved to Isolated Storage.  I am going to take the values and load them into my UserLoginInfo object in my Globals class.  There are other ways to store data on WP7 (e.g. ViewModel, using the App class), but I find this technique has worked well for me in both Silverlight and WP7.</p>
<p>App.xaml.cs</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">     void LoadSettings<span class="br0">&#40;</span><span class="br0">&#41;</span>
     <span class="br0">&#123;</span>
         IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
         if <span class="br0">&#40;</span>settings.Contains<span class="br0">&#40;</span>&quot;UserLoginInfo&quot;<span class="br0">&#41;</span><span class="br0">&#41;</span>
         <span class="br0">&#123;</span>
Globals.UserLoginInfo = <span class="br0">&#40;</span>LoginInfo<span class="br0">&#41;</span>settings<span class="br0">&#91;</span>&quot;UserLoginInfo&quot;<span class="br0">&#93;</span>;
         <span class="br0">&#125;</span>
     <span class="br0">&#125;</span></pre></div></div>

<p>Finally in my login page I interrogate the UserLoginInfo class to determine if the Remember Me box was last checked and if so, what the username should be.  Here is the code snippet for this.</p>

<div class="wp_syntax"><div class="code"><pre class="" style="font-family:monospace;">if <span class="br0">&#40;</span>Globals.UserLoginInfo != null<span class="br0">&#41;</span>
    chkRememberMe.IsChecked = Globals.UserLoginInfo.RememberMe;
if <span class="br0">&#40;</span>Globals.UserLoginInfo != null &amp;&amp; Globals.UserLoginInfo.RememberMe<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
    txtUserName.Text = Globals.UserLoginInfo.EmailAddress;
<span class="br0">&#125;</span></pre></div></div>

<p>That&#8217;s it!  Now users returning to the Dwolla app will have their username prefilled from their previous session and save them time so they can send money quicker.  I hope you find these tips useful.  Thanks!</p>
<p>Resources</p>
<ul>
<li><a href="http://www.codebadger.com/blog/post/2010/10/18/WP7-Development-Tip-of-the-Day-IsolatedStorageSettings.aspx" target="_blank">WP7 Development Tip of the Day: IsolatedStorageSettings</a></li>
</ul>
<ul>
<li><a href="http://www.windowsphonegeek.com/articles/WP7-Application-Lifecycle-and-Tombstoning" target="_blank">WP7 Application Lifecycle and Tombstoning</a></li>
</ul>
<p>&nbsp;</p>
<p><strong>Related Links:</strong></p>
<ul>
<li>
<div><strong><a href="http://silverlight.farreachinc.com/post/2011/03/04/WP7-Insights-1-Consuming-REST-APIs-within-a-WP7-app.aspx">WP7 Insights #1: Consuming REST APIs within a WP7 app</a></strong></div>
</li>
<li>
<div><strong><a href="http://silverlight.farreachinc.com/post/2011/03/13/WP7-Insights-2-Creating-a-Splash-Screen-in-WP7.aspx">WP7 Insights #2: Creating a Splash Screen in WP7</a></strong></div>
</li>
<li>
<div><strong><a href="http://silverlight.farreachinc.com/post/2011/03/23/WP7-Insights-3-Storing-User-State-Activating-and-Deactivating.aspx">WP7 Insights #3: Storing User State &#8211; Activating and Deactivating</a></strong></div>
</li>
<li>
<div><strong><a href="http://silverlight.farreachinc.com/post/2011/04/04/WP7-Insights-4-Detecting-and-Reacting-to-Connectivity.aspx">WP7 Insights #4: Detecting and Reacting to Connectivity</a><br />
</strong></div>
</li>
</ul>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.farreachinc.com%2F2011%2F03%2F23%2Fwp7-insights-3-storing-user-state-activating-and-deactivating%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe><div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://blog.farreachinc.com/2011/03/23/wp7-insights-3-storing-user-state-activating-and-deactivating/' addthis:title='WP7 Insights #3: Storing User State &#8211; Activating and Deactivating ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.farreachinc.com/2011/03/23/wp7-insights-3-storing-user-state-activating-and-deactivating/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

