<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>rioting bits</title>
	
	<link>http://riotingbits.com</link>
	<description>an uncontrollable .net blog</description>
	<lastBuildDate>Sun, 05 Sep 2010 12:09:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RiotingBits" /><feedburner:info uri="riotingbits" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Accessing controls inside a Repeater from Javascript</title>
		<link>http://feedproxy.google.com/~r/RiotingBits/~3/8F7_9KWWgSs/</link>
		<comments>http://riotingbits.com/2009/accessing-controls-inside-a-repeater-from-javascript/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 08:02:53 +0000</pubDate>
		<dc:creator>Dan Dumitru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net + javascript]]></category>
		<category><![CDATA[repeaters]]></category>

		<guid isPermaLink="false">http://riotingbits.com/?p=211</guid>
		<description><![CDATA[The way ASP.NET assigns IDs for controls has long been criticized. In order for them to be unique for a given page, the IDs get forms like ctl00_ContentPlaceHolder1_lblCountTotal, depending on the server controls that contain them. This means that when the IDs of the containing controls change, the child&#8217;s ID changes too, breaking your Javascript [...]]]></description>
			<content:encoded><![CDATA[<p>The way ASP.NET assigns IDs for controls has long been criticized. In order for them to be unique for a given page, the IDs get forms like <em>ctl00_ContentPlaceHolder1_lblCountTotal</em>, depending on the server controls that contain them. This means that when the IDs of the containing controls change, the child&#8217;s ID changes too, breaking your Javascript code that directly accessed the control by its full ID.</p>
<p>The usual way to handle this issue is by using the ClientID property in Javascript, like <em>&lt;%= lblCountTotal.ClientID %&gt;</em>. However, there are some situations when you can&#8217;t use the ClientID of a control. A notable one is when the control is inside the ItemTemplate of a Repeater. So if you&#8217;re thinking, for example, to update all the label values in your Repeater on a Javascript event, you realize you can&#8217;t really access them and start thinking of a work-around. I&#8217;m going to describe you how I handled the issue.</p>
<h4>My use case</h4>
<p>In a couple of my recent projects at work, we are developing some in-house web applications for a network of hospitals and part of the web apps is porting all their reporting logic. More exactly, they have a lot of excels with different data input forms and, from those filled-in forms, reports must be generated.</p>
<p>Now, when recreating those data input forms on the web, we try to keep as much of the existing look &amp; feel as possible, so we won&#8217;t confuse the users too much with the changes. This means that those calculations Excel makes &#8220;on the fly&#8221; have to work the same way when the user goes from field to field on our webform. And if the forms are large (and they usually are) and we want those calculations to run as fast as not-noticeable, we have to use Javascript (so purely client-side code) to do them.</p>
<h4>The &#8216;didactic&#8217; example</h4>
<p>In order not to bore you to death and make some sense with what I&#8217;m talking about, I made a small form to help you follow me.</p>
<p> <img class="aligncenter size-full wp-image-223" title="form_screenshot" src="http://riotingbits.com/wp-content/uploads/form_screenshot.png" alt="form_screenshot" width="560" height="414" /><br />
As the user fills in the fields with the number of visitors per time zone, the form must calculate the total number of visitors and the percentages (visitors for a time zone / total number of visitors). Each time a field has been changed, the total is changed and consequently all the percentages.</p>
<p>Some of the ASP.NET mark-up for the page:</p>
<pre class="brush: xml;">
&lt;tr style=&quot;background-color: #FFFFCC;&quot;&gt;
    &lt;td&gt;
        Total
    &lt;/td&gt;
    &lt;td&gt;
        &lt;asp:Label ID=&quot;lblCountTotal&quot; runat=&quot;server&quot;&gt;0&lt;/asp:Label&gt;
    &lt;/td&gt;
    &lt;td&gt;
        &amp;nbsp;
    &lt;/td&gt;
&lt;/tr&gt;
&lt;asp:Repeater ID=&quot;rptVisitors&quot; runat=&quot;server&quot;&gt;
    &lt;ItemTemplate&gt;
        &lt;tr style=&quot;background-color: #EEEEEE;&quot;&gt;
            &lt;td&gt;
                &lt;%#Eval(&quot;TimeZoneName&quot;) %&gt;
            &lt;/td&gt;
            &lt;td&gt;
                &lt;asp:TextBox ID=&quot;txtCount&quot; runat=&quot;server&quot; onblur='&lt;%# &quot;UpdateValues(&quot; + Eval(&quot;Index&quot;) + &quot;, this.value)&quot; %&gt;'&gt;&lt;/asp:TextBox&gt;
            &lt;/td&gt;
            &lt;td&gt;
                &lt;asp:Label ID=&quot;lblPercent&quot; runat=&quot;server&quot;&gt;n/a&lt;/asp:Label&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/ItemTemplate&gt;
&lt;/asp:Repeater&gt;
&lt;asp:HiddenField ID=&quot;repeaterRowsCount&quot; runat=&quot;server&quot; /&gt;
</pre>
<p> <br />
The Repeater there is bound using <em>GetSystemTimeZones()</em>, a method I talked about in <a href="http://riotingbits.com/2009/getting-the-current-time-in-a-different-time-zone-c-sharp/">my previous blog post</a>:</p>
<pre class="brush: csharp;">
protected void Page_Load(object sender, EventArgs e)
{
    int index = 0;
    rptVisitors.DataSource = TimeZoneInfo.GetSystemTimeZones()
                                         .OrderBy(tz =&gt; tz.StandardName)
                                         .Select(tz =&gt; new
                                         {
                                             TimeZoneName = tz.StandardName,
                                             Index = index++
                                         });
    rptVisitors.DataBind();

    repeaterRowsCount.Value = rptVisitors.Items.Count.ToString();
}
</pre>
<p> <br />
You can see in the ASP.NET mark-up how, on the <em>onblur</em> event of <em>txtCount</em>, so when the user tabs to the next field, the <em>UpdateValues()</em> Javascript method is called. <em>UpdateValues()</em> calculates the total number of visitors and then the percentages, and the catch is setting those percentages for each <em>lblPercent</em>.</p>
<p>As I said, you can&#8217;t use the ClientID property to access controls inside Repeaters. The labels get IDs like <em>ctl00_ContentPlaceHolder1_rptVisitors_ctl01_lblPercent</em>, <em>ctl00_ContentPlaceHolder1_rptVisitors_ctl02_lblPercent</em>, and the trick I finally used was building their IDs on the go, by getting the base part from the ClientID of the Repeater.</p>
<p>This is how my Javascript code turned out to look:</p>
<pre class="brush: jscript; highlight: [1,32];">
var repeaterId = '&lt;%= rptVisitors.ClientID %&gt;';
var repeaterRowsCount = 0;
var values = [];

window.onload = function() {
    repeaterRowsCount = eval(document.getElementById('&lt;%=repeaterRowsCount.ClientID %&gt;').value);

    for (i = 0; i &lt; repeaterRowsCount; i++)
        values[i] = 0;
}

function UpdateValues(row, value) {
    values[row] = 0;
    if (value != '' &amp;&amp; !isNaN(value))
        values[row] = eval(value);

    var totalCount = 0;
    for (i = 0; i &lt; repeaterRowsCount; i++)
        totalCount += values[i];

    document.getElementById('&lt;%= lblCountTotal.ClientID %&gt;').innerHTML = totalCount;

    for (i = 0; i &lt; repeaterRowsCount; i++) {
        var percent = 'n/a';
        if (totalCount &gt; 0)
            percent = ((values[i] / totalCount) * 100).toFixed(1) + &quot;%&quot;;

        var rowNo = i;
        if (rowNo &lt; 10)
            rowNo = &quot;0&quot; + rowNo;

        document.getElementById(repeaterId + '_ctl' + rowNo + '_lblPercent').innerHTML = percent;
    }
}
</pre>
<p>I highlighted there the two lines, getting the ClientID of the Repeater, and then building the ID for each label. Besides providing a way to access the labels, using the Repeater&#8217;s ClientID as a base ensures that this code won&#8217;t break when subsequent changes are made to the project.</p>
<p>I&#8217;ve let the rest of the code there, so you&#8217;d see the whole solution. Most notably, the way I got the number of items in the Repeater, which I needed in the Javascript code, by using the <em>repeaterRowsCount</em> hidden field.</p>
<h4>Alternatives</h4>
<p>The first thought I had when I started working at the webforms was making the calculations in code-behind, by using AJAX calls. But, even if it would have been nicer, I soon found out that it was too slow for forms with lots of fields. As soon as you got at about 200 fields, you could notice the delay, and I even had forms with 1600 fields!</p>
<p>Another nice solution would have been jQuery&#8217;s &#8216;ends-with&#8217; selector. Actually, on my first take I was using, instead of this:</p>
<pre class="brush: jscript;">
document.getElementById(repeaterId + '_ctl' + rowNo + '_lblPercent').innerHTML = percent;
</pre>
<p>, this:</p>
<pre class="brush: jscript;">
$('id$=_ctl' + rowNo + '_lblPercent').html(percent);
</pre>
<p>Which is, of course, shorter, and worked perfectly on my IE8 browser. However, the &#8216;ends-with&#8217; selector is terribly slower on older browsers, <a href="http://encosia.com/2009/06/09/11-keystrokes-that-made-my-jquery-selector-run-10x-faster/">as Dave Ward points out</a> (execution time of a &#8216;ends-with&#8217; selector, in milliseconds):</p>
<p><img class="aligncenter size-full wp-image-281" title="encosia-ends-with-comparison" src="http://riotingbits.com/wp-content/uploads/encosia-ends-with-comparison.png" alt="encosia-ends-with-comparison" width="492" height="199" /><br />
And this made my form unusable on IE7, for example, so I had to ditch the jQuery. I must note, however, that jQuery is a wonderful library, it just wasn&#8217;t the &#8220;tool for the job&#8221;.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RiotingBits?a=8F7_9KWWgSs:xSEup7t-YQU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=8F7_9KWWgSs:xSEup7t-YQU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=8F7_9KWWgSs:xSEup7t-YQU:AJN6rANEukQ"><img src="http://feeds.feedburner.com/~ff/RiotingBits?d=AJN6rANEukQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=8F7_9KWWgSs:xSEup7t-YQU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=8F7_9KWWgSs:xSEup7t-YQU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=8F7_9KWWgSs:xSEup7t-YQU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=8F7_9KWWgSs:xSEup7t-YQU:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RiotingBits/~4/8F7_9KWWgSs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://riotingbits.com/2009/accessing-controls-inside-a-repeater-from-javascript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://riotingbits.com/2009/accessing-controls-inside-a-repeater-from-javascript/</feedburner:origLink></item>
		<item>
		<title>Getting the current time in a different Time Zone (C#)</title>
		<link>http://feedproxy.google.com/~r/RiotingBits/~3/OP3ferZ9vbM/</link>
		<comments>http://riotingbits.com/2009/getting-the-current-time-in-a-different-time-zone-c-sharp/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 17:58:34 +0000</pubDate>
		<dc:creator>Dan Dumitru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[timezoneinfo]]></category>

		<guid isPermaLink="false">http://riotingbits.com/?p=164</guid>
		<description><![CDATA[A fast start with the solution (getting the current time in EST):

TimeZoneInfo timezone_EST = TimeZoneInfo.FindSystemTimeZoneById(&#34;Eastern Standard Time&#34;);

DateTime now_EST = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timezone_EST);

And now for the yada yada.
As standard as the &#8216;Eastern Standard Time&#8217; might seem to you, C# doesn&#8217;t know a thing about it. In fact, it doesn&#8217;t know much about any timezone, except the local [...]]]></description>
			<content:encoded><![CDATA[<p>A fast start with the solution (getting the current time in EST):</p>
<pre class="brush: csharp;">
TimeZoneInfo timezone_EST = TimeZoneInfo.FindSystemTimeZoneById(&quot;Eastern Standard Time&quot;);

DateTime now_EST = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timezone_EST);
</pre>
<p>And now for the <a href="http://en.wikipedia.org/wiki/The_Yada_Yada">yada yada</a>.</p>
<p>As standard as the &#8216;Eastern Standard Time&#8217; might seem to you, C# doesn&#8217;t know a thing about it. In fact, it doesn&#8217;t know much about any timezone, except the local timezone (the one on the computer running the application) and the UTC.</p>
<p>Why that, it beats me. I would have expected to have at least the more used timezones kept inside the basic library. But I guess it wasn&#8217;t meant to be.</p>
<p>Instead, to get information about a specific timezone, you have to retrieve it from your system&#8217;s registry, with the <a href="http://msdn.microsoft.com/en-us/library/system.timezoneinfo.findsystemtimezonebyid.aspx">FindSystemTimeZoneById</a> method. This means you have to hardcode the timezone&#8217;s ID, and this after you made sure the computer that runs the code has your intended timezone with that ID in the registry.</p>
<p>To get a complete list of the timezones available on your system, you can go ahead and check the registry! (just joking, there&#8217;s <a href="http://msdn.microsoft.com/en-us/library/system.timezoneinfo.getsystemtimezones.aspx">a method</a> for that)</p>
<p>Fortunately, these timezone IDs are pretty standard across Windows installations. And I&#8217;ll just throw here a list, to easily grab an ID when I need it (these are the timezones on my Windows 7; compared with a list I got from a Windows XP, there are ~7 different entries, rather obscure timezones):</p>
<table>
<tbody>
<tr>
<td><strong>Id</strong></td>
<td><strong>Display Name</strong></td>
<td><strong>Supports <a href="http://en.wikipedia.org/wiki/Daylight_saving_time">DST</a></strong></td>
</tr>
<tr>
<td>Afghanistan Standard Time</td>
<td>(UTC+04:30) Kabul</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Alaskan Standard Time</td>
<td>(UTC-09:00) Alaska</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Arab Standard Time</td>
<td>(UTC+03:00) Kuwait, Riyadh</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Arabian Standard Time</td>
<td>(UTC+04:00) Abu Dhabi, Muscat</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Arabic Standard Time</td>
<td>(UTC+03:00) Baghdad</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Argentina Standard Time</td>
<td>(UTC-03:00) Buenos Aires</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Atlantic Standard Time</td>
<td>(UTC-04:00) Atlantic Time (Canada)</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>AUS Central Standard Time</td>
<td>(UTC+09:30) Darwin</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>AUS Eastern Standard Time</td>
<td>(UTC+10:00) Canberra, Melbourne, Sydney</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Azerbaijan Standard Time</td>
<td>(UTC+04:00) Baku</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Azores Standard Time</td>
<td>(UTC-01:00) Azores</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Canada Central Standard Time</td>
<td>(UTC-06:00) Saskatchewan</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Cape Verde Standard Time</td>
<td>(UTC-01:00) Cape Verde Is.</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Caucasus Standard Time</td>
<td>(UTC+04:00) Yerevan</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Cen. Australia Standard Time</td>
<td>(UTC+09:30) Adelaide</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Central America Standard Time</td>
<td>(UTC-06:00) Central America</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Central Asia Standard Time</td>
<td>(UTC+06:00) Astana, Dhaka</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Central Brazilian Standard Time</td>
<td>(UTC-04:00) Manaus</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Central Europe Standard Time</td>
<td>(UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Central European Standard Time</td>
<td>(UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Central Pacific Standard Time</td>
<td>(UTC+11:00) Magadan, Solomon Is., New Caledonia</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Central Standard Time</td>
<td>(UTC-06:00) Central Time (US &amp; Canada)</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Central Standard Time (Mexico)</td>
<td>(UTC-06:00) Guadalajara, Mexico City, Monterrey</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>China Standard Time</td>
<td>(UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Dateline Standard Time</td>
<td>(UTC-12:00) International Date Line West</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>E. Africa Standard Time</td>
<td>(UTC+03:00) Nairobi</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>E. Australia Standard Time</td>
<td>(UTC+10:00) Brisbane</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>E. Europe Standard Time</td>
<td>(UTC+02:00) Minsk</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>E. South America Standard Time</td>
<td>(UTC-03:00) Brasilia</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Eastern Standard Time</td>
<td>(UTC-05:00) Eastern Time (US &amp; Canada)</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Egypt Standard Time</td>
<td>(UTC+02:00) Cairo</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Ekaterinburg Standard Time</td>
<td>(UTC+05:00) Ekaterinburg</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Fiji Standard Time</td>
<td>(UTC+12:00) Fiji, Kamchatka, Marshall Is.</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>FLE Standard Time</td>
<td>(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Georgian Standard Time</td>
<td>(UTC+03:00) Tbilisi</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>GMT Standard Time</td>
<td>(UTC) Dublin, Edinburgh, Lisbon, London</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Greenland Standard Time</td>
<td>(UTC-03:00) Greenland</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Greenwich Standard Time</td>
<td>(UTC) Monrovia, Reykjavik</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>GTB Standard Time</td>
<td>(UTC+02:00) Athens, Bucharest, Istanbul</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Hawaiian Standard Time</td>
<td>(UTC-10:00) Hawaii</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>India Standard Time</td>
<td>(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Iran Standard Time</td>
<td>(UTC+03:30) Tehran</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Israel Standard Time</td>
<td>(UTC+02:00) Jerusalem</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Jordan Standard Time</td>
<td>(UTC+02:00) Amman</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Korea Standard Time</td>
<td>(UTC+09:00) Seoul</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Mauritius Standard Time</td>
<td>(UTC+04:00) Port Louis</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Mid-Atlantic Standard Time</td>
<td>(UTC-02:00) Mid-Atlantic</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Middle East Standard Time</td>
<td>(UTC+02:00) Beirut</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Montevideo Standard Time</td>
<td>(UTC-03:00) Montevideo</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Morocco Standard Time</td>
<td>(UTC) Casablanca</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Mountain Standard Time</td>
<td>(UTC-07:00) Mountain Time (US &amp; Canada)</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Mountain Standard Time (Mexico)</td>
<td>(UTC-07:00) Chihuahua, La Paz, Mazatlan</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Myanmar Standard Time</td>
<td>(UTC+06:30) Yangon (Rangoon)</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>N. Central Asia Standard Time</td>
<td>(UTC+06:00) Almaty, Novosibirsk</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Namibia Standard Time</td>
<td>(UTC+02:00) Windhoek</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Nepal Standard Time</td>
<td>(UTC+05:45) Kathmandu</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>New Zealand Standard Time</td>
<td>(UTC+12:00) Auckland, Wellington</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Newfoundland Standard Time</td>
<td>(UTC-03:30) Newfoundland</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>North Asia East Standard Time</td>
<td>(UTC+08:00) Irkutsk, Ulaan Bataar</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>North Asia Standard Time</td>
<td>(UTC+07:00) Krasnoyarsk</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Pacific SA Standard Time</td>
<td>(UTC-04:00) Santiago</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Pacific Standard Time</td>
<td>(UTC-08:00) Pacific Time (US &amp; Canada)</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Pacific Standard Time (Mexico)</td>
<td>(UTC-08:00) Tijuana, Baja California</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Pakistan Standard Time</td>
<td>(UTC+05:00) Islamabad, Karachi</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Romance Standard Time</td>
<td>(UTC+01:00) Brussels, Copenhagen, Madrid, Paris</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Russian Standard Time</td>
<td>(UTC+03:00) Moscow, St. Petersburg, Volgograd</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>SA Eastern Standard Time</td>
<td>(UTC-03:00) Georgetown</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>SA Pacific Standard Time</td>
<td>(UTC-05:00) Bogota, Lima, Quito, Rio Branco</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>SA Western Standard Time</td>
<td>(UTC-04:00) La Paz</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Samoa Standard Time</td>
<td>(UTC-11:00) Midway Island, Samoa</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>SE Asia Standard Time</td>
<td>(UTC+07:00) Bangkok, Hanoi, Jakarta</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Singapore Standard Time</td>
<td>(UTC+08:00) Kuala Lumpur, Singapore</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>South Africa Standard Time</td>
<td>(UTC+02:00) Harare, Pretoria</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Sri Lanka Standard Time</td>
<td>(UTC+05:30) Sri Jayawardenepura</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Taipei Standard Time</td>
<td>(UTC+08:00) Taipei</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Tasmania Standard Time</td>
<td>(UTC+10:00) Hobart</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>Tokyo Standard Time</td>
<td>(UTC+09:00) Osaka, Sapporo, Tokyo</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Tonga Standard Time</td>
<td>(UTC+13:00) Nuku&#8217;alofa</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>US Eastern Standard Time</td>
<td>(UTC-05:00) Indiana (East)</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>US Mountain Standard Time</td>
<td>(UTC-07:00) Arizona</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>UTC</td>
<td>(UTC) Coordinated Universal Time</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Venezuela Standard Time</td>
<td>(UTC-04:30) Caracas</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Vladivostok Standard Time</td>
<td>(UTC+10:00) Vladivostok</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>W. Australia Standard Time</td>
<td>(UTC+08:00) Perth</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>W. Central Africa Standard Time</td>
<td>(UTC+01:00) West Central Africa</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>W. Europe Standard Time</td>
<td>(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
<tr>
<td>West Asia Standard Time</td>
<td>(UTC+05:00) Tashkent</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>West Pacific Standard Time</td>
<td>(UTC+10:00) Guam, Port Moresby</td>
<td style="text-align: center;">Nay.</td>
</tr>
<tr>
<td>Yakutsk Standard Time</td>
<td>(UTC+09:00) Yakutsk</td>
<td style="text-align: center;"><span style="color: green;">Yay!</span></td>
</tr>
</tbody>
</table>
<p> </p>
<p>If you like reading in-depth articles from the Creators, I highly recommend a post called <a href="http://blogs.msdn.com/bclteam/archive/2007/06/18/a-brief-history-of-datetime-anthony-moore.aspx">A Brief History of DateTime</a>, <span style="font-size: 0.5em;">not</span> by Stephen Hawking, on the BCL Team Blog. It&#8217;s about how the DateTime types evolved across .NET versions and how, in order to preserve backwards-compatibility and reliability, the usage of these types may not be that intuitive in some cases.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RiotingBits?a=OP3ferZ9vbM:dYIKoxfWedA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=OP3ferZ9vbM:dYIKoxfWedA:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=OP3ferZ9vbM:dYIKoxfWedA:AJN6rANEukQ"><img src="http://feeds.feedburner.com/~ff/RiotingBits?d=AJN6rANEukQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=OP3ferZ9vbM:dYIKoxfWedA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=OP3ferZ9vbM:dYIKoxfWedA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=OP3ferZ9vbM:dYIKoxfWedA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=OP3ferZ9vbM:dYIKoxfWedA:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RiotingBits/~4/OP3ferZ9vbM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://riotingbits.com/2009/getting-the-current-time-in-a-different-time-zone-c-sharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://riotingbits.com/2009/getting-the-current-time-in-a-different-time-zone-c-sharp/</feedburner:origLink></item>
		<item>
		<title>int? == null, or why it misbehaves in Linq-to-SQL</title>
		<link>http://feedproxy.google.com/~r/RiotingBits/~3/NvF9TChViP4/</link>
		<comments>http://riotingbits.com/2009/int-null-or-why-it-misbehaves-in-linq-to-sql/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 16:08:34 +0000</pubDate>
		<dc:creator>Dan Dumitru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linq-to-sql]]></category>
		<category><![CDATA[nullable types]]></category>

		<guid isPermaLink="false">http://riotingbits.com/?p=104</guid>
		<description><![CDATA[Let Category be a class/table, with a nullable int field named ParentId, to help me keep an hierarchy of categories.
And the following method, which returns an enumeration of all the categories having the given ParentId:

public static IEnumerable&#60;Category&#62; Enumerate(int? parentId)
{
    RiotingDataContext dataContext = new RiotingDataContext();

    return dataContext.Categories.Where(c =&#62; c.ParentId == parentId);
}

Now, [...]]]></description>
			<content:encoded><![CDATA[<p>Let Category be a class/table, with a <em>nullable int</em> field named ParentId, to help me keep an hierarchy of categories.</p>
<p>And the following method, which returns an enumeration of all the categories having the given ParentId:</p>
<pre class="brush: csharp;">
public static IEnumerable&lt;Category&gt; Enumerate(int? parentId)
{
    RiotingDataContext dataContext = new RiotingDataContext();

    return dataContext.Categories.Where(c =&gt; c.ParentId == parentId);
}
</pre>
<p>Now, this works as expected&#8230; right until you pass a <em>parentId</em> which is null. At that moment, the returned sequence is empty, although there are categories in the database that have a null ParentId.</p>
<p>I ran into this more than once and, needless to say, you are quite surprised when you see this for the first time. What to do? Of course, a quick check with <a href="http://www.developer.com/db/article.php/3482216">SQL Profiler</a>, to see what the hell Linq-to-SQL is doing underneath.</p>
<pre class="brush: sql;">
exec sp_executesql N'SELECT [t0].[Id], [t0].[Name], [t0].[ParentId]
FROM [dbo].[Categories] AS [t0]
WHERE [t0].[ParentId] = @p0',N'@p0 int',@p0=NULL
</pre>
<p>The problem is that the parameterized stored procedure that is generated checks if our nullable int equals null by doing <strong>ParentId = NULL</strong>, when the right way to do it (SQL-style) is <strong>ParentId IS NULL</strong>. So the code we would want to be generated, when ParentId is null, is:</p>
<pre class="brush: sql;">
SELECT [t0].[Id], [t0].[Name], [t0].[ParentId]
FROM [dbo].[Categories] AS [t0]
WHERE [t0].[ParentId] IS NULL
</pre>
<p>, and is the result we will get if we modify our method like this:</p>
<pre class="brush: csharp;">
public static IEnumerable&lt;Category&gt; Enumerate(int? parentId)
{
    RiotingDataContext dataContext = new RiotingDataContext();

    if (parentId != null)
        return dataContext.Categories.Where(c =&gt; c.ParentId == parentId);
    else
        return dataContext.Categories.Where(c =&gt; c.ParentId == null);
}
</pre>
<p>Or, in a more compact manner (and the way I like to use it):</p>
<pre class="brush: csharp;">
public static IEnumerable&lt;Category&gt; Enumerate(int? parentId)
{
    RiotingDataContext dataContext = new RiotingDataContext();

    return dataContext.Categories.Where(c =&gt; parentId != null ? c.ParentId == parentId : c.ParentId == null);
}
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RiotingBits?a=NvF9TChViP4:Tzs6hSgHzNQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=NvF9TChViP4:Tzs6hSgHzNQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=NvF9TChViP4:Tzs6hSgHzNQ:AJN6rANEukQ"><img src="http://feeds.feedburner.com/~ff/RiotingBits?d=AJN6rANEukQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=NvF9TChViP4:Tzs6hSgHzNQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=NvF9TChViP4:Tzs6hSgHzNQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=NvF9TChViP4:Tzs6hSgHzNQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=NvF9TChViP4:Tzs6hSgHzNQ:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RiotingBits/~4/NvF9TChViP4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://riotingbits.com/2009/int-null-or-why-it-misbehaves-in-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://riotingbits.com/2009/int-null-or-why-it-misbehaves-in-linq-to-sql/</feedburner:origLink></item>
		<item>
		<title>A simple way to persist the Linq-to-SQL Data Context</title>
		<link>http://feedproxy.google.com/~r/RiotingBits/~3/2UwCMmDza_s/</link>
		<comments>http://riotingbits.com/2009/a-simple-way-to-persist-the-linq-to-sql-data-context/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 09:21:47 +0000</pubDate>
		<dc:creator>Dan Dumitru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[data context]]></category>
		<category><![CDATA[linq-to-sql]]></category>

		<guid isPermaLink="false">http://riotingbits.com/?p=95</guid>
		<description><![CDATA[The 3.0 version of C# introduced Linq-to-SQL, an Object-Relational Mapping tool that allows querying relational data using .Net classes. Linq-to-SQL uses a class named DataContext to keep track of the mapping objects and secure their persistance.
But, if you stumbled-upon this post, you probably know these by now. I should add a small explanation by Rick [...]]]></description>
			<content:encoded><![CDATA[<p>The 3.0 version of C# introduced Linq-to-SQL, an Object-Relational Mapping tool that allows querying relational data using .Net classes. Linq-to-SQL uses a class named DataContext to keep track of the mapping objects and secure their persistance.</p>
<p>But, if you stumbled-upon this post, you probably know these by now. I should add a small <a href="http://www.west-wind.com/weblog/posts/246222.aspx" target="_self">explanation by Rick Strahl </a>about the behaviour of the DataContext:</p>
<blockquote><p>Linq to SQL has a persistent approach to managing its &#8216;connection&#8217; to the database via this data context and it basically assumes that you use a single DataContext to make all of your data related access. This doesn&#8217;t mean that it makes persistent connections to the database, but means that the DataContext instance maintains state about active result sets, which is especially important if change tracking is on which is the default.</p></blockquote>
<p>Now, Linq-to-SQL, especially with its language enhancements, greatly improves the handling of database data, making the code more fast-to-produce, maintainable and most of all readable. The classes included in Linq&#8217;s standard libraries are enough for most of small-scale project architectures.</p>
<p>However, when trying to develop a multi-tired web application, most people get to problems where their objects get detached from the DataContext and awkward ways are used to track their changes. The scenario is as follows: the object is retrieved from the DataContext, some changes are made to it, the object is passed through different layers of abstraction in the project architecture (and possibly a postback or two) and finally you end up with a detached object. There are several ways of attaching it back, but they either imply some changes to the table mapped by the object, either some cumbersome code that you have to repeat each time you have the problem.</p>
<p>The solution I&#8217;m using is one of the options Rick Strahl talks about in his article, creating a thread-specific DataContext, but using the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.callcontext.aspx" target="_self">CallContext</a> to make the DataContext persistent, and in rather few lines of code. The basic idea is keeping the same DataContext during a given session, which will be the only one responsible for keeping track of your objects and thus avoiding them to get detached.</p>
<p>Enough with the talk. The code now. Basically it revolves around using a DataContext Factory to keep your DataContext in the CallContext:</p>
<pre class="brush: csharp;">
using System.Data.Linq;
using System.Runtime.Remoting.Messaging;

namespace RiotingBits.Data
{
    public class DataContextFactory&lt;T&gt; where T : DataContext, new()
    {
        public static T RetrieveDataContext()
        {
            string key = string.Format(&quot;DataContext_{0}&quot;, typeof(T));

            T currentDataContext = (T)CallContext.GetData(key);
            if (currentDataContext == null)
            {
                currentDataContext = new T();
                CallContext.SetData(key, currentDataContext);
            }

            return currentDataContext;
        }
    }
}
</pre>
<p>As you can see, the Factory works with generic DataContext types, to allow its usage with different data contexts you may have in your application. The next step is creating an extender for the DataContext you want to persist:</p>
<pre class="brush: csharp;">
namespace RiotingBits.Data
{
    public class DataContextExtender : DataContextFactory&lt;RiotingBitsDataContext&gt;
    {
        public static RiotingBitsDataContext DataContext
        {
            get
            {
                return RetrieveDataContext();
            }
        }
    }
}
</pre>
<p>Here, RiotingBitsDataContext is the name I gave to my DataContext.</p>
<p>After adding these two classes, you don&#8217;t even have to instantiate your DataContext each time you use it. You simply use the static property of the Extender. Some examples of usage for Read/Update/Insert on a normal Users table:</p>
<pre class="brush: csharp;">
public static User GetById(int userId)
{
    return DataContextExtender.DataContext.Users.Where(u =&gt; u.Id == userId).SingleOrDefault();
}

public void Save()
{
    if (this.Id == 0)
        DataContextExtender.DataContext.Users.InsertOnSubmit(this);

    DataContextExtender.DataContext.SubmitChanges();
}
</pre>
<p>As a closing-paragraph, I&#8217;d like to debate about the necessity of such kind of a solution. Is it something terribly wrong with Linq-to-SQL that you may need this? I tend to say no. When you have complex architectures, this is a thing that&#8217;s bound to happen, making your own custom classes to address your project&#8217;s architectural needs. Linq-to-SQL behaves exactly as an Object-Relational Mapping tool is expected to. So my thoughts go more on the line of <a href="http://codebetter.com/blogs/ian_cooper/archive/2008/03/09/architecting-linq-to-sql-applications-part-7.aspx" target="_self">Ian Cooper&#8217;s</a>:</p>
<blockquote><p>I read  a fair number of opinions that are suprised by the behavior of LINQ To SQL. However, having used ORMs for a number of years, I find LINQ To SQL conforms to my expectations as to how an ORM should behave. Indeed a reading of something as old now as Martin Fowler&#8217;s <a href="http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420">Patterns of Enteprise Application Architecture</a> and you will find exactly this pattern of behavior for an ORM discussed. Indeed <a href="http://code.google.com/p/wilsonormapper/">WORM</a> (Wilson O/R Mapper now open source BTW) and <a href="http://www.hibernate.org/343.html">NHibernate</a> both behave in a similar fashion. So some of this seems to be based on expectations that don&#8217;t come from experience of using ORM tools.</p></blockquote>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RiotingBits?a=2UwCMmDza_s:Il_4Nouz1ok:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=2UwCMmDza_s:Il_4Nouz1ok:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=2UwCMmDza_s:Il_4Nouz1ok:AJN6rANEukQ"><img src="http://feeds.feedburner.com/~ff/RiotingBits?d=AJN6rANEukQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=2UwCMmDza_s:Il_4Nouz1ok:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=2UwCMmDza_s:Il_4Nouz1ok:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RiotingBits?a=2UwCMmDza_s:Il_4Nouz1ok:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RiotingBits?i=2UwCMmDza_s:Il_4Nouz1ok:F7zBnMyn0Lo" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RiotingBits/~4/2UwCMmDza_s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://riotingbits.com/2009/a-simple-way-to-persist-the-linq-to-sql-data-context/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://riotingbits.com/2009/a-simple-way-to-persist-the-linq-to-sql-data-context/</feedburner:origLink></item>
	</channel>
</rss><!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
