<?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>Stronico - Steroids for Networking &#187; Code Samples</title>
	<atom:link href="http://blog.stronico.com/category/code-samples/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stronico.com</link>
	<description>Building the Address Book that Works With Your Brain</description>
	<lastBuildDate>Wed, 08 Sep 2010 02:27:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The List in C#</title>
		<link>http://blog.stronico.com/2010/09/the-list-in-c/</link>
		<comments>http://blog.stronico.com/2010/09/the-list-in-c/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 02:27:00 +0000</pubDate>
		<dc:creator>Steve French</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://blog.stronico.com/?p=545</guid>
		<description><![CDATA[<p>The first entry in my Code Review is the<strong> List&#60;&#62;</strong>.</p>
<p>So far, the List is my favorite sort of array in C#.  It has the benefits of</p>
<ul>
<li>being obvious in naming</li>
</ul>
<p><a href="http://blog.stronico.com/2010/09/the-list-in-c/" class="more-link">Read more on The List<> in C#&#8230;</a></p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&#038;id=545&#038;type=feed" alt="" />]]></description>
			<content:encoded><![CDATA[<p>The first entry in my Code Review is the<strong> List&lt;&gt;</strong>.</p>
<p>So far, the List is my favorite sort of array in C#.  It has the benefits of</p>
<ul>
<li>being obvious in naming</li>
<li>you do not have to know the size when it is declared &#8211; the List can expand or contract as needed</li>
<li>adding items to the array is quite simple, as is looping through the array</li>
<li>finding items in the list is easy as well</li>
</ul>
<p>The basic List syntax is this</p>
<blockquote><p><em>List&lt;string&gt; lstContactList = new List&lt;string&gt;();</em></p></blockquote>
<p>At first the syntax looks a bit off, what with the odd angle brackets&lt;&gt; and the () at the very end of the declaration, but that is the only counter-intuitive part of the List.  Please note, a list can hold any type of object or type, not just strings.</p>
<p>To <strong>add to the list</strong> simply use the .Add syntax, for example</p>
<blockquote><p><em>lstContactList.Add (&#8220;Bob Raindorf&#8221;);</em>.</p></blockquote>
<p>You can <strong>find the index</strong> of a particular list item by simply using this syntax:</p>
<blockquote><p>lstContactList.IndexOf(&#8220;Bob Raindorf&#8221;);</p></blockquote>
<p>Once you identify the  identified, you can <strong>replace the value</strong> of the list item quite simply by</p>
<blockquote><p>lstContactList[lstContactList.IndexOf("Bob Raindorf")] = &#8220;Robert Raindorf&#8221;;</p></blockquote>
<p>If you want to <strong>loop through the list</strong>, just do this</p>
<blockquote><p><em>foreach (string str in lstContactList)<br />
{<br />
InsertToDB(str);<br />
}</em></p></blockquote>
<p>To <strong>clear the list</strong>, just do this</p>
<blockquote><p><em>lstContactList.Clear();</em></p></blockquote>
<p>That&#8217;s all I need to remember (for now anyway), hopefully the act of writing this pushed it into long term memory.</p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&id=545&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.stronico.com/2010/09/the-list-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Fix Column Doubling in an ASP.net GridView &#8211; with Code Sample</title>
		<link>http://blog.stronico.com/2010/08/how-to-fix-column-doubling-in-an-asp-net-gridview-with-code-sample/</link>
		<comments>http://blog.stronico.com/2010/08/how-to-fix-column-doubling-in-an-asp-net-gridview-with-code-sample/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 16:15:32 +0000</pubDate>
		<dc:creator>Steve French</dc:creator>
				<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[How To Fix]]></category>

		<guid isPermaLink="false">http://blog.stronico.com/?p=538</guid>
		<description><![CDATA[<p><a title="Chocolate Tools" href="http://www.flickr.com/photos/37921614@N00/3312115991/" target="_blank"><img class="alignright" style="border: 0px initial initial;" src="http://farm4.static.flickr.com/3616/3312115991_cc2a483d06_m.jpg" border="0" alt="Chocolate Tools" width="240" height="160" /></a><br />
<strong>The Problem:</strong> You are trying to create an ASP.net GridView that automatically populates on any dataset, as well as automatically sorts, but for some reason whenever you click the headers to sort the GridView, the data doubles.</p>
<p><a href="http://blog.stronico.com/2010/08/how-to-fix-column-doubling-in-an-asp-net-gridview-with-code-sample/" class="more-link">Read more on How To Fix Column Doubling in an ASP.net GridView &#8211; with Code Sample&#8230;</a></p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&#038;id=538&#038;type=feed" alt="" />]]></description>
			<content:encoded><![CDATA[<p><a title="Chocolate Tools" href="http://www.flickr.com/photos/37921614@N00/3312115991/" target="_blank"><img class="alignright" style="border: 0px initial initial;" src="http://farm4.static.flickr.com/3616/3312115991_cc2a483d06_m.jpg" border="0" alt="Chocolate Tools" width="240" height="160" /></a><br />
<strong>The Problem:</strong> You are trying to create an ASP.net GridView that automatically populates on any dataset, as well as automatically sorts, but for some reason whenever you click the headers to sort the GridView, the data doubles.</p>
<p><strong>The Cause:</strong> I originally omitted crucial line<br />
gv.Columns.Clear();</p>
<p>in the code sample below.    For whatever reason I got it stuck in my head that the data was doubling, not the columns.</p>
<p><strong><span id="more-538"></span>The Solution:</strong> Just add in the .Clear() line of code to the Gridview population routine and the problem is solved.</p>
<p><strong>The Code:</strong></p>
<p><em>All the code you need in the .aspx page:</em><br />
&lt;asp:GridView ID=&#8221;gv&#8221; AllowSorting=&#8221;true&#8221; runat=&#8221;server&#8221; AutoGenerateColumns=&#8221;False&#8221; onsorting=&#8221;gv_Sorting&#8221;&gt;&lt;/asp:GridView&gt;</p>
<p><em>All the code you need to initially populate the GridView in the .cs pages</em></p>
<pre>    private DataSet GenDataSet()
    {
        // Delcare a DataSet and Table
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        DataRow dtRow;

        dt.Columns.Add("MNumber");
        dt.Columns.Add("Legal Name");
        dt.Columns.Add("Email");
        dt.Columns.Add("Level");

        dtRow = dt.NewRow();
        dtRow["MNumber"] = "A"; ;
        dtRow["Legal Name"] = "A";
        dtRow["Email"] = "A";
        dtRow["Level"] = "A";
        dt.Rows.Add(dtRow);

        dtRow = dt.NewRow();
        dtRow["MNumber"] = "B"; ;
        dtRow["Legal Name"] = "B";
        dtRow["Email"] = "B";
        dtRow["Level"] = "B";
        dt.Rows.Add(dtRow);

        dtRow = dt.NewRow();
        dtRow["MNumber"] = "C"; ;
        dtRow["Legal Name"] = "C";
        dtRow["Email"] = "C";
        dtRow["Level"] = "C";
        dt.Rows.Add(dtRow);

        dtRow = dt.NewRow();
        dtRow["MNumber"] = "D"; ;
        dtRow["Legal Name"] = "D";
        dtRow["Email"] = "D";
        dtRow["Level"] = "D";
        dt.Rows.Add(dtRow);

        dtRow = dt.NewRow();
        dtRow["MNumber"] = "E"; ;
        dtRow["Legal Name"] = "E";
        dtRow["Email"] = "E";
        dtRow["Level"] = "E";
        dt.Rows.Add(dtRow);

        ds.Tables.Add(dt);

        //note this is columns
<strong><em>        gv.Columns.Clear();</em></strong>
        foreach (DataColumn col in ds.Tables[0].Columns)
        {
            BoundField bfNew = new BoundField();
            //Initalize the DataField value.
            bfNew.DataField = col.ColumnName;
            //Initialize the HeaderText field value.
            bfNew.HeaderText = col.ColumnName;
            bfNew.SortExpression = col.ColumnName;
            //Add the newly created bound field to the GridView.
            gv.Columns.Add(bfNew);
        }
        return ds;
    }</pre>
<p>That&#8217;s it!</p>
<p><small><a title="Attribution-ShareAlike License" href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank"><img src="http://blog.stronico.com/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="JanneM" href="http://www.flickr.com/photos/37921614@N00/3312115991/" target="_blank">JanneM</a></small></p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&id=538&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.stronico.com/2010/08/how-to-fix-column-doubling-in-an-asp-net-gridview-with-code-sample/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS trick &#8211; how to use two classes on a single DOM object</title>
		<link>http://blog.stronico.com/2010/07/css-trick-how-to-use-two-classes-on-a-single-dom-object/</link>
		<comments>http://blog.stronico.com/2010/07/css-trick-how-to-use-two-classes-on-a-single-dom-object/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 21:04:12 +0000</pubDate>
		<dc:creator>Steve French</dc:creator>
				<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://blog.stronico.com/?p=513</guid>
		<description><![CDATA[<p>I have no idea how this trick has eluded me for so long, but it is possible to use two classes on the same DOM object.  For example to combine a class called &#8220;TopNav&#8221; and a class called &#8220;SelectedNav&#8221; all you have to do is call them both with code like this</p>
<p><a href="http://blog.stronico.com/2010/07/css-trick-how-to-use-two-classes-on-a-single-dom-object/" class="more-link">Read more on CSS trick &#8211; how to use two classes on a single DOM object&#8230;</a></p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&#038;id=513&#038;type=feed" alt="" />]]></description>
			<content:encoded><![CDATA[<p>I have no idea how this trick has eluded me for so long, but it is possible to use two classes on the same DOM object.  For example to combine a class called &#8220;TopNav&#8221; and a class called &#8220;SelectedNav&#8221; all you have to do is call them both with code like this</p>
<blockquote><p>&lt;div id=&#8221;HomeNavItem&#8221; class=&#8221;TopNav SelectedNav&#8221;&gt;Home&lt;/div&gt;</p></blockquote>
<p>In other words, you&#8217;re just putting them in the same class declaration.  I&#8217;ve been doing this for years and I&#8217;ve never done that before.</p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&id=513&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.stronico.com/2010/07/css-trick-how-to-use-two-classes-on-a-single-dom-object/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A simple way to create a Microsoft Word document from a template in Asp.net/C#</title>
		<link>http://blog.stronico.com/2010/07/a-simple-way-to-create-a-microsoft-word-document-from-a-template-in-asp-netc/</link>
		<comments>http://blog.stronico.com/2010/07/a-simple-way-to-create-a-microsoft-word-document-from-a-template-in-asp-netc/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 16:32:52 +0000</pubDate>
		<dc:creator>Steve French</dc:creator>
				<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[ASP.net MVC]]></category>
		<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[How To Fix]]></category>

		<guid isPermaLink="false">http://blog.stronico.com/?p=500</guid>
		<description><![CDATA[<p><a title="Will code for food" href="http://www.flickr.com/photos/67523311@N00/114420037/" target="_blank"><img class="alignright" style="border: 0px initial initial;" src="http://farm1.static.flickr.com/45/114420037_f08201d9b8_m.jpg" border="0" alt="Will code for food" width="240" height="180" align="center" /></a> I recently had the need to create a Microsoft Word document from a template.  I initially tried using Office Web Components and Interop but all that really wasn&#8217;t worth the trouble.  I wound up doing global replacements in the html of html   Here is how I did it:</p>
<p><a href="http://blog.stronico.com/2010/07/a-simple-way-to-create-a-microsoft-word-document-from-a-template-in-asp-netc/" class="more-link">Read more on A simple way to create a Microsoft Word document from a template in Asp.net/C#&#8230;</a></p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&#038;id=500&#038;type=feed" alt="" />]]></description>
			<content:encoded><![CDATA[<p><a title="Will code for food" href="http://www.flickr.com/photos/67523311@N00/114420037/" target="_blank"><img class="alignright" style="border: 0px initial initial;" src="http://farm1.static.flickr.com/45/114420037_f08201d9b8_m.jpg" border="0" alt="Will code for food" width="240" height="180" align="center" /></a> I recently had the need to create a Microsoft Word document from a template.  I initially tried using Office Web Components and Interop but all that really wasn&#8217;t worth the trouble.  I wound up doing global replacements in the html of html   Here is how I did it:</p>
<ol>
<li>Open up your template document in word, and put any target areas in special Brackets, the text would read something like &#8220;I, [FULLNAME] do hereby affirm that&#8221;.  I found that the target area had to be in uppercase, I&#8217;m not sure why</li>
<li>Go to &#8220;Save As Html, Filtered&#8221;, and save it in a writeable directory.</li>
<li>In your C# code, first load the entire document into a string</li>
<li>Convert that string into an instance of StringBuilder</li>
<li>Use the StringBuilder Replace function, it would look something like this &#8211;  sb.Replace(&#8220;[FULLNAME]&#8220;, strFullName);</li>
<li>Create a TextWriter, and write the StringBuilder to it &#8211; make sure you&#8217;re saving it with a &#8220;.doc&#8221; extension.</li>
<li>Close the TextWriter</li>
</ol>
<p>All told, the code looks like this</p>
<blockquote>
<div id="_mcePaste">//Create a new filename</div>
<div id="_mcePaste">string strFileName = System.Guid.NewGuid().ToString();</div>
<div id="_mcePaste">string strPathRoot = &#8220;d:\\domains\\MyDomain\\Word\\&#8221;;</div>
<div id="_mcePaste">string strPath = strPathRoot + &#8220;TemplateHtml.doc&#8221;;</div>
<div id="_mcePaste">string str = System.IO.File.ReadAllText(strPath);</div>
<div id="_mcePaste">StringBuilder sb = new StringBuilder();</div>
<div id="_mcePaste">sb.AppendLine(s);</div>
<div id="_mcePaste">//replace the text</div>
<div id="_mcePaste">sb.Replace(&#8220;[FULLNAME]&#8220;, strFullName);</div>
<div id="_mcePaste">//save it out</div>
<div id="_mcePaste">TextWriter tw = new StreamWriter(strPathRoot + strFileName + &#8220;.doc&#8221;);</div>
<div id="_mcePaste">// write a line of text to the file</div>
<div id="_mcePaste">tw.WriteLine(sb.ToString());</div>
<div id="_mcePaste">// close it</div>
<div id="_mcePaste">tw.Close();</div>
<div id="_mcePaste">tw.Dispose();</div>
</blockquote>
<p>That&#8217;s it!</p>
<p><small><a title="Attribution License" href="http://creativecommons.org/licenses/by/2.0/" target="_blank"><img src="http://blog.stronico.com/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a></small><br />
<small><a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="pvera" href="http://www.flickr.com/photos/67523311@N00/114420037/" target="_blank">pvera</a></small></p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&id=500&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.stronico.com/2010/07/a-simple-way-to-create-a-microsoft-word-document-from-a-template-in-asp-netc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to set up the CheckBoxList control in Asp.net</title>
		<link>http://blog.stronico.com/2010/01/how-to-set-up-the-checkboxlist-control-in-asp-net/</link>
		<comments>http://blog.stronico.com/2010/01/how-to-set-up-the-checkboxlist-control-in-asp-net/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 19:23:54 +0000</pubDate>
		<dc:creator>Steve French</dc:creator>
				<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[How To Fix]]></category>
		<category><![CDATA[ajax toolkit]]></category>

		<guid isPermaLink="false">http://blog.stronico.com/?p=145</guid>
		<description><![CDATA[<p>I recently came up with the need to have a validated CheckBoxList in and Asp.net page.  Over the years I&#8217;ve come up with a standard way of setting up validated CheckBoxLists, so I thought I would share the method and start the brand new &#8220;Code Samples&#8221; section..</p>
<p><a href="http://blog.stronico.com/2010/01/how-to-set-up-the-checkboxlist-control-in-asp-net/" class="more-link">Read more on How to set up the CheckBoxList control in Asp.net&#8230;</a></p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&#038;id=145&#038;type=feed" alt="" />]]></description>
			<content:encoded><![CDATA[<p>I recently came up with the need to have a validated CheckBoxList in and Asp.net page.  Over the years I&#8217;ve come up with a standard way of setting up validated CheckBoxLists, so I thought I would share the method and start the brand new &#8220;Code Samples&#8221; section..</p>
<p>The aspx code is as follows</p>
<blockquote><p><code>&lt;strong&gt;Section Name&lt;/strong&gt;<br />
&lt;asp:TextBox ID="tbBox" runat="server" Width="1" BorderColor="White" BackColor="White" BorderStyle="None" /&gt;<br />
&lt;asp:CustomValidator ID="cvBox" ValidateEmptyText="true" runat="server" ControlToValidate="tbBox" Display="none" ErrorMessage="&lt;b&gt;Required Field Missing&lt;/b&gt;&lt;br /&gt;You must select at least one option" SetFocusOnError="true" ValidationGroup="Main" onservervalidate="cvBox_ServerValidate" /&gt;<br />
&lt;ajaxToolkit:ValidatorCalloutExtender ID="vceBox" runat="server" TargetControlID="cvBox" Width="300" /&gt;</code></p>
<p>&lt;br /&gt;</p>
<p>&lt;asp:CheckBoxList ID=&#8221;cblForms&#8221; runat=&#8221;server&#8221; RepeatColumns=&#8221;2&#8243; RepeatDirection=&#8221;Horizontal&#8221;&gt;<br />
&lt;asp:ListItem Text=&#8221;Option A&#8221; Value=&#8221;A&#8221; /&gt;<br />
&lt;asp:ListItem Text=&#8221;Option B&#8221; Value=&#8221;B&#8221; /&gt;<br />
&lt;asp:ListItem Text=&#8221;Option C&#8221; Value=&#8221;C&#8221; /&gt;<br />
&lt;asp:ListItem Text=&#8221;Option D&#8221; Value=&#8221;D&#8221; /&gt;<br />
&lt;/asp:CheckBoxList&gt;</p></blockquote>
<p>Please note, the control that I am validating is actually a 1 pixel wide textbox that one should not type in.<span id="more-145"></span></p>
<p>The server validate code is this</p>
<blockquote><p><code>protected void cvBox_ServerValidate(object source, ServerValidateEventArgs args)<br />
{<br />
string strCB = CBLValueGet(cblForms);<br />
if (strCB.Length!=0)<br />
{<br />
args.IsValid = true;<br />
}<br />
else<br />
{<br />
args.IsValid = false;<br />
}<br />
}</code></p></blockquote>
<p>My ValueGet routine (it loops though everything and makes sure that at least one item was selected is</p>
<blockquote><p><code>public static string CBLValueGet(System.Web.UI.WebControls.CheckBoxList list)<br />
{<br />
string strValue = "";<br />
for (int counter = 0; counter &lt; list.Items.Count; counter++)<br />
{<br />
if (list.Items[counter].Selected)<br />
{<br />
strValue += list.Items[counter].Value + ",";<br />
}<br />
}<br />
if (strValue.Length - 1 &lt; 0)<br />
{<br />
strValue = "";<br />
}<br />
else<br />
{<br />
strValue = strValue.Remove(strValue.Length - 1, 1);<br />
}</code></p>
<p>return strValue;<br />
}</p></blockquote>
<p>The part of the code that fires after the main submit button is pressed is as follows</p>
<blockquote><p><code>string strCB = Utility.CBLValueGet(cblForms);<br />
char[] sep = { ',' };<br />
string[] ProdList = strCB.Split(sep);<br />
foreach (string str in ProdList)<br />
{<br />
if (str.Length != 0)<br />
{<br />
//soemtihg happens here with the str value;<br />
}<br />
}</code></p></blockquote>
<p>That&#8217;s it!</p>
<img src="http://blog.stronico.com/?ak_action=api_record_view&id=145&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://blog.stronico.com/2010/01/how-to-set-up-the-checkboxlist-control-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
