Archive for Aprile 30th, 2008

Convert a string into an enum

CodeKeep C# Feed Aprile 30th, 2008

Description: Source: http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx

Link: http://www.codekeep.net/snippets/c76f9433-c53c-4cae-bf5e-e309b1f51207.aspx

enum Colour
{
  Red,
  Green,
  Blue
} 

// ...
Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);
Console.WriteLine("Colour Value: {0}", c.ToString());

// Picking an invalid colour throws an ArgumentException. To
// avoid this, call Enum.IsDefined() first, as follows:
string nonColour = "Polkadot";

if (Enum.IsDefined(typeof(Colour), nonColour))
  c = (Colour) Enum.Parse(typeof(Colour), nonColour, true);
else
  MessageBox.Show("Uh oh!");

Manual Elmah error handling

CodeKeep C# Feed Aprile 30th, 2008

Description: Manual Elmah error handling

Link: http://www.codekeep.net/snippets/761321e3-fc4a-4531-a63c-c24f9ef9a283.aspx

Elmah.ErrorLog errorLog = Elmah.ErrorLog.GetDefault(HttpContext.Current);
                errorLog.Log(new Elmah.Error(ex));

Unit test with RhinoMocks Record-Playback

CodeKeep C# Feed Aprile 30th, 2008

Description: Unit test with RhinoMocks Record-Playback

Link: http://www.codekeep.net/snippets/5a083679-748f-41f8-af2d-e4a47257013b.aspx

[Test]
public void WriteYourTest()
{
	MockRepository mocks = new MockRepository();
	using (mocks.Record())
	{
	}

	using (mocks.Playback())
	{
	}
}

Method for Special Character Checkl

CodeKeep C# Feed Aprile 30th, 2008

Description: This method check a file name character

Link: http://www.codekeep.net/snippets/b0da90e1-1e30-42c5-91a6-8009b6d8fc29.aspx

public bool isSpecialCharacterExist(string strText, string strSpecialCharacters)
        {
            try
            {
                string strPattern = "[^a-zA-Z" + strSpecialCharacters + "]";
                Regex objRegex = new Regex(strPattern);
                bool result = objRegex.IsMatch(strText);
                return result;
            }
            catch
            {
                return true;
            }
        }

Export DataSet to CSV

CodeKeep C# Feed Aprile 30th, 2008

Description: Download a CSV file from an ASP.Net page. Just call the method from any button click.

Link: http://www.codekeep.net/snippets/aa3d04d0-ec11-47bd-86e6-00373e57fcf2.aspx

private void downloadCSV(DataSet ds, string filename)
{
	Response.Clear();
	Response.ContentType = "text/plain";
	Response.AppendHeader("content-disposition",
												"attachment; filename=" + filename + ".csv");
	Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

	DataTable table = ds.Tables[0];

	//Write the column headers
	bool first = true;
	foreach (DataColumn column in table.Columns)
	{
		if (!first)
		{
			Response.Write(",");
		}
		Response.Write("\"");
		Response.Write(column.ColumnName);
		Response.Write("\"");
		first = false;
	}

	//Write the data
	foreach (DataRow row in table.Rows)
	{
		Response.Write(Environment.NewLine);
		first = true;
		foreach (DataColumn column in table.Columns)
		{
			if (!first)
			{
				Response.Write(",");
			}
			Response.Write("\"");
			Response.Write(row[column.ColumnName]);
			Response.Write("\"");
			first = false;
		}
		Response.Write(Environment.NewLine);
	}

	Response.Flush();
	Response.End();
}