.NET, Technology

CSV export to MS Excel, SYLK file error

I just received the following error when creating a CSV formatted file of some report data, and opening it in Microsoft Excel:

Excel has detected that 'xxx.xls', is a SYLK file. Either the file has errors or it is not a SYLK file format. Click OK to try to open the file in a different format.

After some research it turns out this problem occurs if the first two letters in the file content is ID (in uppercase). The only way to get rid of this error message is therefore to not use ID in uppercase (Id, id is ok), or change it to something else.

.NET, Technology

How to generate the SHA-1 hash of a string using C#

The namespace System.Security.Cryptography contains classes for working with cryptography such as hashing. Within this namespace you will find the SHA1Managed class which we can utilize to generate the SHA-1 hash of a string. Below you will find a helper method using this class:

public static string GetSha1(string value)
{
	var data = Encoding.ASCII.GetBytes(value);
	var hashData = new SHA1Managed().ComputeHash(data);

	var hash = string.Empty;

	foreach (var b in hashData)
		hash += b.ToString("X2");

	return hash;
}
.NET, Technology

How to convert from string to enum using C# and generics

In order to convert from a string to an enumeration in .NET you can use the method Enum.Parse as follows:

public enum SortExpressions
{
	FirstNameAsc,
	FirstNameDesc,
	LastNameAsc,
	LastNameDesc
}

var value = "lastnameasc";
var sortExpression = (SortExpressions) Enum.Parse(typeof(SortExpressions), value, true);

Since this operation is often needed we should make a ConvertToEnum method so we can reuse it. But instead of returning it as the typical object type, we should rather use generics to return it as the actual type: Read More »

.NET, Technology

Extending IDataReader implementations using extension methods

In my post How to extend classes using extension methods, I write about how to extend classes with your own methods. This time we will extend the IDataReader interface with some new methods to save some typing. Since we extend the interface, our methods will be available in all the classes implementing the IDataReader interface, such as the SqlDataReader. Read More »

.NET, Technology

How to extend classes using extension methods

In .NET 3.5 there is a new functionality called extension methods. Extension methods let you extend any classes in the .NET framework, 3rd party frameworks or your own frameworks with your own methods. Read More »

.NET, Technology

The C#using statement

In C#, when you want to make sure an object is disposed, then you might place you’re code within a try finally block as follows: Read More »

.NET, Technology

How to write to the Windows event log using .NET

In the .NET framework you have the System.Diagnostics.EventLog class which provides static methods for working with the Windows event log. Read More »

.NET, Technology

The C# break statement

In C# you have the break statement which is most often seen in relation to terminating a switch statement. However it can also be used to terminate any loop statement as follows:

for (int i = 0; i < 10; i++)
{
	if (i == 5) break;
}

If you have nested loop- or switch statements, then the break statement will only terminate the closest one.

.NET, Technology

How to avoid .NET naming collisions

In order to avoid .NET naming collisions you can use a namespace- or class alias. Read More »