Code Samples Archives - Page 2 of 2 - Digital Tool Factory blog

The Digital Tool Factory Blog

The List in C#

The first entry in my Code Review is the List<>.

So far, the List is my favorite sort of array in C#.  It has the benefits of

  • being obvious in naming
  • you do not have to know the size when it is declared – the List can expand or contract as needed
  • adding items to the array is quite simple, as is looping through the array
  • finding items in the list is easy as well

The basic List syntax is this

List<string> lstContactList = new List<string>();

At first the syntax looks a bit off, what with the odd angle brackets<> 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.

To add to the list simply use the .Add syntax, for example

lstContactList.Add (“Bob Raindorf”);.

You can find the index of a particular list item by simply using this syntax:

lstContactList.IndexOf(“Bob Raindorf”);

Once you identify the  identified, you can replace the value of the list item quite simply by

lstContactList[lstContactList.IndexOf(“Bob Raindorf”)] = “Robert Raindorf”;

If you want to loop through the list, just do this

foreach (string str in lstContactList)

To clear the list, just do this


That’s all I need to remember (for now anyway), hopefully the act of writing this pushed it into long term memory.


This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog

Sep 10

Written By Steve French


How To Fix Column Doubling in an GridView – with Code Sample

Chocolate Tools
The Problem: You are trying to create an 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.

The Cause: I originally omitted crucial line

in the code sample below.    For whatever reason I got it stuck in my head that the data was doubling, not the columns.

Continue reading →

Aug 10

Written By Steve French


CSS trick – how to use two classes on a single DOM object

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 “TopNav” and a class called “SelectedNav” all you have to do is call them both with code like this

<div id=”HomeNavItem” class=”TopNav SelectedNav”>Home</div>

In other words, you’re just putting them in the same class declaration.  I’ve been doing this for years and I’ve never done that before.


This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog

Jul 10

Written By Steve French


A simple way to create a Microsoft Word document from a template in

Will code for food 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’t worth the trouble.  I wound up doing global replacements in the html of html   Here is how I did it:

  1. Open up your template document in word, and put any target areas in special Brackets, the text would read something like “I, [FULLNAME] do hereby affirm that”.  I found that the target area had to be in uppercase, I’m not sure why
  2. Go to “Save As Html, Filtered”, and save it in a writeable directory.
  3. In your C# code, first load the entire document into a string
  4. Convert that string into an instance of StringBuilder
  5. Use the StringBuilder Replace function, it would look something like this –  sb.Replace(“[FULLNAME]”, strFullName);
  6. Create a TextWriter, and write the StringBuilder to it – make sure you’re saving it with a “.doc” extension.
  7. Close the TextWriter

All told, the code looks like this

//Create a new filename
string strFileName = System.Guid.NewGuid().ToString();
string strPathRoot = “d:domainsMyDomainWord”;
string strPath = strPathRoot + “TemplateHtml.doc”;
string str = System.IO.File.ReadAllText(strPath);
StringBuilder sb = new StringBuilder();
//replace the text
sb.Replace(“[FULLNAME]”, strFullName);
//save it out
TextWriter tw = new StreamWriter(strPathRoot + strFileName + “.doc”);
// write a line of text to the file
// close it

That’s it!

Creative Commons License
photo credit: pvera


This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog

Jul 10

Written By Steve French


How to set up the CheckBoxList control in

I recently came up with the need to have a validated CheckBoxList in and page.  Over the years I’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 “Code Samples” section..

The aspx code is as follows

<strong>Section Name</strong>
<asp:TextBox ID="tbBox" runat="server" Width="1" BorderColor="White" BackColor="White" BorderStyle="None" />
<asp:CustomValidator ID="cvBox" ValidateEmptyText="true" runat="server" ControlToValidate="tbBox" Display="none" ErrorMessage="<b>Required Field Missing</b><br />You must select at least one option" SetFocusOnError="true" ValidationGroup="Main" onservervalidate="cvBox_ServerValidate" />
<ajaxToolkit:ValidatorCalloutExtender ID="vceBox" runat="server" TargetControlID="cvBox" Width="300" />

<br />

<asp:CheckBoxList ID=”cblForms” runat=”server” RepeatColumns=”2″ RepeatDirection=”Horizontal”>
<asp:ListItem Text=”Option A” Value=”A” />
<asp:ListItem Text=”Option B” Value=”B” />
<asp:ListItem Text=”Option C” Value=”C” />
<asp:ListItem Text=”Option D” Value=”D” />

Please note, the control that I am validating is actually a 1 pixel wide textbox that one should not type in. Continue reading →

Jan 10

Written By Steve French


Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.