<?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>WhyPad &#187; c#</title>
	<atom:link href="http://www.whypad.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whypad.com</link>
	<description>Tips, tricks, and hacks for life and tech...</description>
	<lastBuildDate>Tue, 06 Jul 2010 15:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C#: Break vs. Continue in Foreach Loops</title>
		<link>http://www.whypad.com/posts/c-break-vs-continue-in-foreach-loops/776/</link>
		<comments>http://www.whypad.com/posts/c-break-vs-continue-in-foreach-loops/776/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 14:17:40 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=776</guid>
		<description><![CDATA[A C# Quick Tip: You&#8217;ve probably seen &#8216;break&#8217; before in Switch Case statements, but it also plays an important role in Foreach Loops. To break completely out of a foreach loop (foregoing all the remaining loops), use: break; To go to the next iteration in the loop, use: continue; When is &#8216;break&#8217; useful? It is [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A C# Quick Tip: </strong> You&#8217;ve probably seen &#8216;break&#8217; before in Switch Case statements, but it also plays an important role in Foreach Loops.</p>
<p>To break completely out of a foreach loop (foregoing all the remaining loops), use:  <strong>break;</strong></p>
<p>To go to the next iteration in the loop, use:  <strong>continue;</strong></p>
<p><span id="more-776"></span></p>
<p>When is &#8216;break&#8217; useful?  It is particularly useful if you&#8217;re looping through an a collection of Objects (like Rows in a Datatable) and you&#8217;re searching for a particular match.  When you find that match, there&#8217;s no need to continue through the remaining rows, so you want to Break out.  There are plenty of other cases as well.</p>
<p>&#8216;Continue&#8217; is useful when you&#8217;ve accomplished what you need to in side a loop iteration and you don&#8217;t want to process the remaining code for that particular iteration.  You&#8217;ll normally have &#8216;continue&#8217; after an &#8216;if&#8217;.</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=776&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/c-break-vs-continue-in-foreach-loops/776/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: That Pesky DBNull Error in Datatables</title>
		<link>http://www.whypad.com/posts/c-that-pesky-dbnull-error-in-datatables/767/</link>
		<comments>http://www.whypad.com/posts/c-that-pesky-dbnull-error-in-datatables/767/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 21:42:02 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=767</guid>
		<description><![CDATA[Datatables and Tableadapters are one of the most beautiful things about C# and Visual Studio, until you&#8217;re processing rows in your datatable and you get something that reads like &#8220;The value for column &#8216;your_column&#8217; in table &#8216;your_table&#8217; is DBNull.&#8221;  If you go a little further, you may get:  &#8220;Exception Details: System.InvalidCastException: Specified cast is not [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.whypad.com/wp-content/uploads/datatable.jpg"><img class="alignleft size-thumbnail wp-image-769" title="datatable" src="http://www.whypad.com/wp-content/uploads/datatable-150x150.jpg" alt="" width="150" height="150" /></a>Datatables and Tableadapters are one of the most beautiful things about C# and Visual Studio, until you&#8217;re processing rows in your datatable and you get something that reads like &#8220;The value for column &#8216;your_column&#8217; in table &#8216;your_table&#8217; is DBNull.&#8221;  If you go a little further, you may get:  &#8220;<strong>Exception Details: </strong>System.InvalidCastException: Specified cast is not  valid.&#8221;  You may go to your Datatable in your Dataset and start trying to change the NullValue for the offending column or trying to change the DefaultValue in the column properties, but chances are that the data type will not let you change that NullValue.  It will insist on &#8220;(Throw Exception)&#8221;.<span id="more-767"></span></p>
<h3>Help for the DBNull Problem has Arrived!</h3>
<p>The answer is so simple (at least in VS 2008 and greater, maybe before that too).  It is rather sad how much time I&#8217;ve wasted trying to deal with this problem.  Without further ado, the answer!</p>
<p>You&#8217;re probably using a Datarow object to get at the column values, probably buried in a foreach statement.  Instead of messing around with the specific column like  myRow.MyColumn, the Row object has an isNull method for each column in the row.  The method name takes the column&#8217;s name and puts &#8220;is&#8221; in front, and &#8220;null&#8221; behind.  So for a column called MyColumn, the method would be:   myRow.IsMyColumnNull();</p>
<p>Here&#8217;s a full example of dealing with nulls in a DateTime field.  The logic is the same for Int or other numerics as well.</p>
<pre class="brush: csharp;">

int iReq = 6196;
certdbTableAdapters.integration_procsTableAdapter taProcs = new certdbTableAdapters.integration_procsTableAdapter();
certdb.integration_procsDataTable dtProcs = taProcs.GetData(iReq);

certdb.integration_procsRow rowProc = dtProcs.Rows[0];

if(!rowProc.IsStartTimeNull())
{
    sProcDate = rowProc.StartTime.ToShortDateString();
}
else
{
    sProcDate = &quot;&quot;;
}
</pre>
<h3>The Craziness I Tried Before</h3>
<p>Before I finally found the answer to this, I was using all sorts of crazy workarounds, including using multiple queries to be sure I never got a null situation, and using the fabulous Case statement within SQL statements.  Here&#8217;s a little tast of a Case statement:</p>
<pre class="brush: sql;">
SELECT DISTINCT Process.ProcessID, Process.Name, Process.Description, Process.ResultLogStatusID,
Process.StatusTimeStamp, ResultLogStatus.Name AS ProcResult,
CASE WHEN LogProcess.EndTime IS NULL THEN '1/1/1900' ELSE LogProcess.EndTime END as rortime,
CASE WHEN LogProcess.Status IS NULL THEN 'missing' ELSE LogProcess.Status END as rorstatus
FROM         RequirementTestLink
LEFT JOIN Process ON Process.ProcessID = RequirementTestLink.ProcessID
LEFT JOIN ResultLogStatus ON Process.ResultLogStatusID = ResultLogStatus.ResultLogStatusID
LEFT JOIN LogProcess ON Process.ProcessID = LogProcess.ProcessID AND LogProcess.ResultOfRecord = 1 WHERE     (RequirementTestLink.RequirementID IN (&quot; + sReqs + &quot;)) ORDER BY Process.ProcessID
</pre>
<p>I tried lots of things&#8230;here are a couple others that didn&#8217;t work on my strongly typed datatables:</p>
<p>myRow.MyColumn != null<br />
or<br />
Convert.IsDBNull(myRow.MyColumn)</p>
<p>Neither worked! It&#8217;s a shame the answer was much simpler!</p>
<p>I hope this post will help someone else out there.  Hopefully I&#8217;ll remember this post the next time I&#8217;m dealing with DBNulls.<br />
Cheers,<br />
Byron</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=767&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/c-that-pesky-dbnull-error-in-datatables/767/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# &#8211; SQL Server Foreign Key, Unique constraints</title>
		<link>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/</link>
		<comments>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/#comments</comments>
		<pubDate>Wed, 27 May 2009 21:40:24 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=682</guid>
		<description><![CDATA[I got tripped up today with a particularly nasty Join statement that worked perfectly form 90% of the queries, but was blowing up at times.  The error message was:  Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. I looked and looked at this SQL code: SELECT Process.ProcessID, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.whypad.com/wp-content/uploads/unique_key_constraint.gif"><img class="alignleft size-thumbnail wp-image-684" title="unique_key_constraint" src="http://www.whypad.com/wp-content/uploads/unique_key_constraint-150x150.gif" alt="unique_key_constraint" width="150" height="150" /></a>I got tripped up today with a particularly nasty Join statement that worked perfectly form 90% of the queries, but was blowing up at times.  The error message was: </p>
<p><span id="more-682"></span></p>
<p>Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.</p>
<p>I looked and looked at this SQL code:</p>
<pre class="brush: sql;">
SELECT Process.ProcessID, Process.Name, Process.Description, Process.ResultLogStatusID, Process.StatusTimeStamp, ResultLogStatus.Name AS ProcResult, RequirementTestLink.RequirementID

FROM RequirementTestLink

LEFT JOIN Process ON Process.ProcessID = RequirementTestLink.ProcessID

LEFT JOIN ResultLogStatus ON Process.ResultLogStatusID = ResultLogStatus.ResultLogStatusID

WHERE (RequirementTestLink.RequirementID IN (&quot; + sReqs + &quot;))

ORDER BY RequirementTestLink.RequirementID, Process.ProcessID
</pre>
<p>It looked perfectly legit to me. I figured out that there would be multiple instances of Process.ProcessID in the RequirementTestLink table. But this shouldn&#8217;t have been a problem since it would just create multiple rows for the multiple instances.</p>
<h2>Relax or turn off constraints in your DataSet.</h2>
<p>The problem was that I was using a SQL Adapter to fill a DataTable. When I made the DataTable, it got junked up with Primary Key and Foreign Key constraints on the ProcessID field. The answer had been right there in the message (see image below).  They spelled it out in the first line:  &#8220;Relax or turn off constraints in your DataSet&#8221;. It took me 30 minutes to work it out on my own&#8230;if I&#8217;d just actually read those pop ups (but so often they are so cryptic I can&#8217;t comprehend them <img src='http://www.whypad.com/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' />  ).<br />
<a href="http://www.whypad.com/wp-content/uploads/fk-constraint.gif"></a><br />
<a href="http://www.whypad.com/wp-content/uploads/relax_constraints.gif"><img class="alignleft size-full wp-image-689" title="relax_constraints" src="http://www.whypad.com/wp-content/uploads/relax_constraints.gif" alt="relax_constraints" width="450" height="228" /></a></p>
<p><a href="http://www.whypad.com/wp-content/uploads/delete_key_button.gif"><img class="alignright size-full wp-image-688" title="delete_key_button" src="http://www.whypad.com/wp-content/uploads/delete_key_button.gif" alt="delete_key_button" width="180" height="236" /></a>So, to get rid of that Primary Key:</p>
<ol>
<li>Left click on the key icon (you can see it in the image at right beside ProcessID)</li>
<li>Right click on the same key icon and select &#8216;Delete key&#8217;. </li>
</ol>
<p><a href="http://www.whypad.com/wp-content/uploads/fk-constraint.gif"><img class="alignleft size-thumbnail wp-image-683" title="fk-constraint" src="http://www.whypad.com/wp-content/uploads/fk-constraint-150x150.gif" alt="fk-constraint" width="150" height="150" /></a>If that doesn&#8217;t do it for you, then zap the Foreign Keys as well.  You can see one coming in at the top right (left picture).  Click on the line and delete it.</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=682&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
