An array of C#, PHP, and HTML programming articles, tutorials, and resources

Archive for November, 2008

At some point, every C# programmer deals with populating, selecting, and even deleting from a DataTable. A moderately advanced operation is transferring an entire single row from an original DataTable and inserting it into a new DataTable. My original thoughtprocess is the following:

DataTable originalTable = new DataTable();
originalTable.Columns.Add("id", typeof(int));
originalTable.Columns.Add("firstName", typeof(string));
originalTable.Columns.Add("lastName", typeof(string));
originalTable.Columns.Add("age", typeof(string));
originalTable.Columns.Add("gender", typeof(string));

DataRow dr = originalTable.NewRow();
dr["id"] = 1;
dr["firstName"] = "Victor";
dr["lastName"] = "Chen";
dr["age"] = 5;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 2;
dr["firstName"] = "John";
dr["lastName"] = "Doe";
dr["age"] = 35;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 3;
dr["firstName"] = "Jane";
dr["lastName"] = "Doe";
dr["age"] = 32;
dr["gender"] = "F";
originalTable.Rows.Add(dr);

DataTable newTable = originalTable.Clone();
newTable.Rows.Add(originalTable.Rows[1]);

Though the above example is long, lines 1 - 30 handle creating and populating the original DataTable (If you need explanation of how this works, check out Add Rows to a DataTable in C# tutorial). Line 32 creates a new DataTable by taking the structure of the original DataTable. In Line 33, we attempt to select Row 1 from the original DataTable and add it as a new row in the new DataTable. However, the above example above does not work. It will throw the runtime error below.

This row already belongs to another table.

The correct solution takes more lines of coding (and therefore more logic), but ultimately gets the job done. It involves using a DataRow’s ItemArray method to transfer a row’s data column by column through an object array, and inserting them column by column back into a new DataRow. See the example below:

DataTable originalTable = new DataTable();
originalTable.Columns.Add("id", typeof(int));
originalTable.Columns.Add("firstName", typeof(string));
originalTable.Columns.Add("lastName", typeof(string));
originalTable.Columns.Add("age", typeof(string));
originalTable.Columns.Add("gender", typeof(string));

DataRow dr = originalTable.NewRow();
dr["id"] = 1;
dr["firstName"] = "Victor";
dr["lastName"] = "Chen";
dr["age"] = 5;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 2;
dr["firstName"] = "John";
dr["lastName"] = "Doe";
dr["age"] = 35;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 3;
dr["firstName"] = "Jane";
dr["lastName"] = "Doe";
dr["age"] = 32;
dr["gender"] = "F";
originalTable.Rows.Add(dr);

DataTable newTable = originalTable.Clone();
DataRow newRow = newTable.NewRow();
newRow.ItemArray = originalTable.Rows[1].ItemArray;
newTable.Rows.Add(newRow);

Keeping consistent with our original example, lines 1-30 handle creating a DataTable. On line 32, create a DataTable. On Line 32, create a new DataRow. On Line 33, I select Row 1 from the Original DataTable, take it’s ItemArray, and transfer it to the newly created DataRow from Line 32. Finally, on line 34, I transfer newRow into the newTable.

If you have more than one row you would like to copy, merely repeat this process through a loop. Happy Coding!

Using C# Dictionary Collection

by Victor | November 19, 2008 in C# | 1 Comment

A Dictionary object in C# has become one of my recent favorites. Below are a few important aspects:

  1. Requires including System.Collections.Generic;
  2. Each entry in a Dictionary is a KeyValuePair (consisting of two objects: key and value)
  3. The key must be unique
  4. The value does not need to be unique
  5. Both the key and value can be any object (string, int, custom class, etc…)
  6. Retrieving a value via a key take close to O(1) time
  7. The order of a KeyValuePair enumeration is not defined

Next is an example of how to create and initialize a Dictionary object. Our Dictionary will have an integer and as a key and a string as a value.

Dictionary<int, string> myDictionary = new Dictionary<int, string>();

Now that we have our Dictionary datatype, we will add two values to the Dictionary. Note that at this point, intellisense knows the two expected types.

myDictionary.Add(1, "Victor");
myDictionary.Add(2, "Billy");

Finally, deleting an object is just as easy:

myDictionary.Remove(1);

And these are the basics to using a Dictionary collection!

You may have noticed the fancing code snippets that accompany many of the tutorials on this website! I would love to say I developed this myself, but the truth is that this is a free resource free for anybody to download and use. It is called SyntaxHighlighter and can be found at http://code.google.com/p/syntaxhighlighter/.

What is Included?

So what does the Syntax Highlighter come with? Below is a list my favorite features of this software:

  1. It’s completely free!
  2. Associate keywords for supported languages are highlighted.
  3. Each line of code is numbered and colored for easier reading!
  4. Allows one click to code only view.
  5. Allows one click copy to clipboard of code.
  6. Allows one click copy to printer.

What Comes With the Download

The file can be downloaded from their official website at http://code.google.com/p/syntaxhighlighter/ as a rar compressed file. Inside the compressed file, you will find various folders and files. The core files that are required include:

  1. clipboard.swf
  2. shCore.js
  3. SyntaxHighlighter.css

Still, there are many more JavaScript files that have been included in the download. Not all of these files are required. Instead, you choose additional optional JavaScript files for the programming or scripting languages you want the Syntax Highlighter to associate.

Installation After Uploading Files

After you have uploaded the required and accompanying optional files, you will need to include the javascript and css files to the header of the pages with which you plan on using the Syntax Highlighter. See the example below:

<link type="text/css" rel="stylesheet" href="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Styles/SyntaxHighlighter.css"></link>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shCore.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushCSharp.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushJava.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushPhp.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushXml.js"></script>
<script language="javascript" src="http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/shBrushJScript.js"></script>
<script language="javascript">
  window.onload = function () {
    dp.SyntaxHighlighter.ClipboardSwf = 'http://www.victorchen.info/wp-includes/js/SyntaxHighlighter/Scripts/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
  }
</script>

In the above, there Line 1 includes the CSS file. Next, Line 2 includes the one core JavaScript file. Lines 4 through 8 include optional JavaScript files. Lines 9 through 14 include setup for using Syntax Highlighter. The portion requires including the clipboard.swf file.

Usage

I assume you already have formatted code contained within the pre tags, as found in a previous tutorial called 3 Steps to Posting Sample Code on your Website. As of now, all you have is a formatted tag, without the fancy upgrades. All you need to add for fancy code formatting is to add name=”code” class=”html”. Exchange html with the type of file you have included. Click to see sample supported files: http://code.google.com/p/syntaxhighlighter/wiki/Languages. An example:

<pre name="code" class="html">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Victor's Programming Aid&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;This is a test&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>

Conclusion

With that, you now have fancy code formatting on your website. Comment on some examples of where you have used the Syntax Highlighter in action!