<?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>Sunstorm Blog &#187; .NET</title>
	<atom:link href="http://www.sunstormlabs.net/blog/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sunstormlabs.net/blog</link>
	<description>We make the storm in your head</description>
	<lastBuildDate>Thu, 17 Dec 2009 17:30:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exceptions in BackgroundWorker</title>
		<link>http://www.sunstormlabs.net/blog/2009/12/17/exceptions-in-backgroundworker/</link>
		<comments>http://www.sunstormlabs.net/blog/2009/12/17/exceptions-in-backgroundworker/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 17:28:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=135</guid>
		<description><![CDATA[To save 15 minutes of confusion.
If an exception is thrown in a BackgroundWorker&#8217;s DoWork handler, the backgroundworker will catch it and place the exception in RunWorkerCompleted&#8217;s e.Error.
If it seems like the Exception&#8217;s still being thrown with no apparent way to catch it, it&#8217;s because if e.Error is not null. When that&#8217;s the case, e.Result will throw [...]]]></description>
			<content:encoded><![CDATA[<p>To save 15 minutes of confusion.</p>
<p>If an exception is thrown in a BackgroundWorker&#8217;s DoWork handler, the backgroundworker <em>will</em> catch it and place the exception in RunWorkerCompleted&#8217;s e.Error.</p>
<p>If it seems like the Exception&#8217;s still being thrown with no apparent way to catch it, it&#8217;s because if e.Error is not null. When that&#8217;s the case, e.Result will throw that error (wrapped in a TargetInvocationException) when accessed. Do not access e.Result without checking for e.Error first.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> worker_RunWorkerCompleted<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, RunWorkerCompletedEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  EndCreate<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span>, e.<span style="color: #0000FF;">Result</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &lt;--- THIS WILL CRASH IF e.Error IS NOT NULL</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> worker_RunWorkerCompleted<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, RunWorkerCompletedEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// THIS WILL NOT.</span>
  <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    EndCreate<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span>, e.<span style="color: #0000FF;">Result</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
    EndCreate<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Error</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// accessing e.Result throws the exception</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2009/12/17/exceptions-in-backgroundworker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Initializing DataRows</title>
		<link>http://www.sunstormlabs.net/blog/2009/02/09/initializing-datarows/</link>
		<comments>http://www.sunstormlabs.net/blog/2009/02/09/initializing-datarows/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 17:51:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=108</guid>
		<description><![CDATA[How many ways are there to initialize a new DataRow with new values?


Set the DefaultValue property on the respective DataColumns.
This works fine for static values. However, you&#8217;re likely using the DataSet designer in Visual Studio, and it will hose itself if you try to set a GUID because it can&#8217;t tell them apart from a [...]]]></description>
			<content:encoded><![CDATA[<p>How many ways are there to initialize a new DataRow with new values?</p>
<ol>
<li>
<h3>Set the DefaultValue property on the respective DataColumns.</h3>
<p>This works fine for static values. However, you&#8217;re likely using the DataSet designer in Visual Studio, and it will hose itself if you try to set a GUID because it can&#8217;t tell them apart from a string. Plus, initializing with dynamic values like <code>DateTime.Now</code> won&#8217;t work because you&#8217;ll end up with the date of the creation of the column rather than the date of the row.</li>
<li>
<h3>Initialize after creating it, but before adding it to the DataTable.</h3>
<p>If you&#8217;re creating the row manually, you can just set any initial values before adding it to the table. None of the values, indices, etc. are validated until that happens. Strong typed datasets are even nice enough to generate methods that take and return rows of your type.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">MyDataSet ds <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyDataSet<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
MyTableRow row <span style="color: #008000;">=</span> ds.<span style="color: #0000FF;">MyTable</span>.<span style="color: #0000FF;">NewMyTableRow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
ds.<span style="color: #0000FF;">MyTable</span>.<span style="color: #0000FF;">AddMyTableRow</span><span style="color: #000000;">&#40;</span>row<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

</li>
<li>
<h3>Override the OnTableNewRow of the DataTable in a derived class. <em>This doesn&#8217;t actually work.</em></h3>
<p>Like the derived classes created by the strong typed table generator. You can get to this method if you use the DataSet&#8217;s helpful partial class definition:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Note: does not actually work</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyDataSet <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyTable <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnTableNewRow<span style="color: #000000;">&#40;</span>DataTableNewRowEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>e.<span style="color: #0000FF;">Row</span><span style="color: #008000;">;</span>
            row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnTableNewRow</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It doesn&#8217;t work because <strong>this method is never called <a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98383">unless you also attach a handler to the TableNewRow event</a></strong>, defeating the whole point of creating a virtual method. GG, Microsoft. This set me back a whole afternoon trying to figure out why some of my tables ran the method and others didn&#8217;t.</p>
<p>You can overcome this if you&#8230;</li>
</ol>
<p><span id="more-108"></span></p>
<ol start="3">
<li>
<h3>Handle the TableNewRow event of the DataTable.</h3>
<p>Good if you want to initialize with external values of some kind, but you can also attach the handler from inside the typed DataTable if you override the <code>EndInit()</code> method, which being part of <code>ISupportInitialize</code>, Visual Studio calls every time the table is initialized in a designer. If you use the dataset outside of the designer, well, you&#8217;re screwed.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyDataSet <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> MyTable <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> EndInit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TableNewRow</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, TableNewRowEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// use 'delegate' if not C# 3.0</span>
                MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>e.<span style="color: #0000FF;">Row</span><span style="color: #008000;">;</span>
                row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnTableNewRow</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</li>
<li>
<h3>Use a BindingSource and handle the AddingNew event.</h3>
<p>You bound your dataset to a bunch of controls, and now you want to create new rows with data that&#8217;s only available in your presentation tier. BindingSources let you control the row creation process completely if you handle the AddingNew event. The downside is that you have to create the whole thing manually, and to do that you have to know how BindingSources work.</p>
<p>Another caveat is that the BindingSource&#8217;s Position won&#8217;t move to the newly created item, like it would otherwise. You&#8217;ll have to do that yourself.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">myBindingSource.<span style="color: #0000FF;">AddingNew</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, AddingNewEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #000000;">&#123;</span>
    DataRowView view <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DataView<span style="color: #000000;">&#41;</span>myBindingSource.<span style="color: #0000FF;">List</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddNew</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>view.<span style="color: #0000FF;">Row</span><span style="color: #008000;">;</span>
    row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
    e.<span style="color: #0000FF;">NewObject</span> <span style="color: #008000;">=</span> view<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There&#8217;s a better way in which you don&#8217;t have to create the DataRowView&#8230;</li>
<li>
<h3>Use a BindingSource and handle the ListChanged event.</h3>
<p>This is the easiest way to initialize rows directly on the BindingSource. Unlike the AddingNew method, the BindingSource creates the row, and sets the Position to the new item.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">myBindingSource.<span style="color: #0000FF;">ListChanged</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, ListChangedEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">ListChangedType</span> <span style="color: #008000;">==</span> ListChangedType.<span style="color: #0000FF;">ItemAdded</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        DataRowView view <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>DataRowView<span style="color: #000000;">&#41;</span>myBindingSource<span style="color: #000000;">&#91;</span>e.<span style="color: #0000FF;">NewIndex</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>view.<span style="color: #0000FF;">IsNew</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// Check if the row is in its detached state</span>
            MyDataSet.<span style="color: #0000FF;">MyTableRow</span> row <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>MyDataSet.<span style="color: #0000FF;">MyTableRow</span><span style="color: #000000;">&#41;</span>view.<span style="color: #0000FF;">Row</span>
            row.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Guid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            row.<span style="color: #0000FF;">CreatedOn</span> <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
            bs.<span style="color: #0000FF;">ResetItem</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">NewIndex</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// this is needed to get the controls to show the new values that we just set.</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The ListChanged event is called twice. First when the row is created in its detached state, and then again when it&#8217;s added to the Rows collection of the DataTable. We only need to handle it the first time. The call to ResetItem will make sure that all the bound controls read the new values.</li>
<li>
<h3>Use control-specific events.</h3>
<p>Some controls have their own events to initialize new rows. I don&#8217;t recommend using this approach, because it&#8217;s too control-specific. It&#8217;s much more interoperable to initialize at the BindingSource. You never know how that AddNew might be called.</li>
</ol>
<p>Depending on what kind of data you&#8217;re initializing, and in which tier, you might need to mix and match. #4 for the data tier and #6 for the forms do the job for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2009/02/09/initializing-datarows/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
