Using Transactions in C#

When programming with c# and SQLServer, you will come into a situation where you need to execute more than one SQL statements, and ensure all commands executed successfully. If one fails, then the entire process should be aborted and moved to the initial stage.

In this scenario we need to make use of transactions. Transactions can be written inside a stored procedure or it can be written inside a C# program. ADO.NET comes with a SQLTransaction class, which can be used for this. This post explains transactions with working example.

We use pubs database for the examples. Executing following scripts will create two tables, Table1 and Table2. Continue reading

Finding image size on a remote website from ASP.NET

This post explains method to find image size located on the remote website. In this example I will be finding size of google logo.


/// <summary>
/// Returns image size located on remote server
/// </summary>
private string GetImageSize(string Url)
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(Url);
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
double LengthInBytes = (double)WebResp.ContentLength;
double LengthInKB = LengthInBytes / 1024;
return Convert.ToString(LengthInKB);
}

Generics in C#


public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
{
}

Any body seen a class something similar to this, and surprised about what would be <T> means ? Stack class can be called as Generic type. They are newly introduced in .NET2005. It’s something similar to C++ templates. But it is not exactly like templates. It lacks some functionalities that templates have.

What does a Generic class or method means ? Continue reading