by Edward
30 July 2010 08:20
Caching is one of the least used but a very powerful feature of ASP.NET. It can save you time and also unnecessary trips to the database. If you need to cache any type of information in ASP.NET such as a DataSet, DataTable, or an ArrayList, you can use the cache. This works a lot like a 'session' or 'application variable', but it has some added cache-related methods as well. Scalable web applications should have the ability to store items, whether data objects, pages, or event parts of a page, in memory. This allows you to avoid recreating information, expensive database calls or even connections to 3rd party web services.
However sometimes you might want to clear all these cached objects in one go for some specific reason. Like I said web applications should be scalable and build towards high-performance. This is where you will need to use the 'Cache.Remove()' method, as the built-in cache object in ASP.NET does not support a 'Cache.Clear()' method.
Here is an example of storing a datatable and in the cache:
DataTable dtCacheExample = new DataTable();
//code here: add rows to datatable
Cache["dtCacheExample"] = dtCacheExample;
ArrayList arrCacheExample = new ArrayList();
//code here: add items to arraylist
Cache["arrCacheExample"] = arrCacheExample;
To clear the cache use the following:
// Retrieve a dictionary enumerator used to iterate through the
// key settings and their values contained in the cache.
IDictionaryEnumerator CacheEnum = Cache.GetEnumerator;
while (CacheEnum.MoveNext) {
Cache.Remove(CacheEnum.Key.ToString);
}
by Edward
28 July 2010 21:05
As a .NET developer, you can sometimes spend hours finding errors and handling them when you do not know the core logic of the system, and decent good documentation is not that easy to come by. There are several ways to address erros, by using exceptions. Exceptions are designed to handle errors and most commonly in a "try/catch" statement.
It is good practice to not catch the generic errors, and try and debug from there. If you do this, you are unsure what your code is actually doing - which can result in the business living with the concequences. The key is to know which errors you might expect and catch those and handle them accordingly. An exception I found to be hard to track, is the "OverflowException" exception.
From MSDN: An OverflowException exception is thrown when a casting, conversion or arithmetic operation in a checked context results in an overflow. An overflow occurs when an operation produces a value too large for the destination type, infinity, or Not a Number (NaN).
For example, mathematical operations can cause these type of exceptions. A way to check for this error is to use the 'checked' keyword. The 'checked' keyword is used to detect overflow conditions. You should note that an 'OverflowException' exception occurs only in a checked context.
Here is a code example on how to check for this type of exception, and how to handle it.
public static void Main()
{
try
{
checked
{
int aInt;
int bInt;
int Sum;
aInt = 2000000000;
bInt = 2000000000;
Sum = aInt + bInt;
}
}
catch (OverflowException oExcep)
{
Console.WriteLine("A mathematical operation caused an overflow.");
// Good idea to log this error and deal with it when doing debugging
// I am using my own Logging module.
Logger.LogException(oExcep, "A mathematical operation caused an overflow.");
}
}
by Edward
26 July 2010 08:22
The LINQ distinct select method is a powerful small method that can help you select unique entries from datatable.
The following example will return a datatable with unique results, from a datatable which can be populated from a database. The following example uses a datatable I created to show you how the LINQ distinct select method works.
The example below have a list of users, but I only want the 'user names' to be returned. They have to be unique, as I might want to use it in a report.
/// <summary>
/// Gets the user names.
/// </summary>
public static void GetUserNames()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(int));
dt.Columns.Add("UserName", typeof(string));
dt.Rows.Add(1, "Joe Smith");
dt.Rows.Add(2, "John Doe");
dt.Rows.Add(3, "Joe Smith");
dt.Rows.Add(4, "Jane Smith");
dt.Rows.Add(5, "Jane Doe");
DataTable filterTable = GetUniqueEntries(dt);
}
/// <summary>
/// Gets the unique entries.
/// </summary>
/// <param name="dt">DataTable</param>
/// <returns></returns>
private static DataTable GetUniqueEntries(DataTable dt)
{
var query = (
from row in dt.AsEnumerable()
select row.Field<string>("UserName")).Distinct();
DataTable dtDistinctNames = new DataTable();
dtDistinctNames.Columns.Add("UserName", typeof(string));
//have to return a datatable, thus loop through entries
foreach (string item in query)
{
DataRow newRow = dtDistinctNames.NewRow();
newRow["UserName"] = item;
dtDistinctNames.Rows.Add(newRow);
}
return dtDistinctNames;
}
This code snippet will return you a list of users where 'username' are unique. There eliminating duplicate names from the list. There are alternative ways to do this type of functionality - this blog entry is created to show you how easy it can be to use LINQ with C# code.
by Edward
20 July 2010 08:26
GhostDoc is a free Visual Studio extension which I stumbled accross while looking for something to help me with commenting my code. GhostDoc automatically generates XML documentation comments for methods and properties based on their type, parameters, name, and other contextual information. It takes the 'pain' out of commenting each method word for word - saving you time doing what you do best - coding!
Most documentation created will be a waste of time for a developer, and usually when there is no time to waste a developer might find the documentation is slowing him down. For any decent documentation to be useful, a developer must know that when the documentation was initially generated it was both correct and complete, it has also been updated as the project scope changed, or amendments was made to the code. I ofter find code to out of sync with documentation, which leaves me with lots of 'catch up' to do, before I feel in control and productive.
You can set it up, so when you right click on a method the "Document This" option becomes available that allows you to generate summary comments for your method.

Here is an example of summary comments I created using this tool.
/// <summary>
/// Checks for SQL injection.
/// </summary>
/// <param name="userInput">The user input.</param>
/// <returns></returns>
public static string checkForSQLInjection(string userInput)
{
// code here
}
It is also supported in the following versions of the Visual Studio IDEs:
- Visual Studio 2010
- Visual Studio 2008
- Visual Studio 2005
Supported Languages:
Download it from here:
http://visualstudiogallery.msdn.microsoft.com/en-us/46A20578-F0D5-4B1E-B55D-F001A6345748