Category Archives: C#

C#: Export a List of Objects to CSV with LINQ, Attributes, and Generics

Have you ever needed to transform data from in-memory C# objects to a CSV flat-file of a specific format?  Here’s an easy way to get the job done with C#, LINQ, and Generics. First you need the Attribute you will … Continue reading

Posted in Attributes, C#, Generics, LINQ | Leave a comment

C# : String Extension to Parse Enums

It seems to me that there is too much typing involved in parsing a string as an Enum type. Here’s a quick string extension method to turn it into a simpler one liner: public static T EnumParse<T>(this string input, bool … Continue reading

Posted in C# | Leave a comment

ASP.Net MVC: Partially Secured Sites / Switching from HTTPS back to HTTP

Most sites out there have some portions that should be only served via HTTPS, while the remainder can be HTTP, such as account pages and content pages respectively.  This is sometimes called a “partially secured site.” Starting with MVC 2, … Continue reading

Posted in ASP.Net, C#, MVC | 1 Comment

C#: String Extension to Replace Accented Characters

Have you ever wanted to replace “accented” characters in a string with their equivalent English character?  Here’s a string extension that replaces these diacritics within a string for C# 2.0 and up: public static string ReplaceDiacritics(this string source) { string … Continue reading

Posted in ASP.Net, C# | Leave a comment

Creating a SQL CLR User Defined Function

Starting with SQL Server 2005, Microsoft added the awesome ability to reference .Net assemblies from your T-SQL procedures.  Here’s a quick 5 step overview on how to get up and running with your code-based User Defined Function. 1) Create a … Continue reading

Posted in C#, MS SQL Server, T-SQL | Leave a comment

Retrieve Image from Windows Clipboard via .Net C#

If you want to snag clipboard data from a different application into the context of your running .Net application, here’s how you can do it: private static Image _clipBoardImage = null; private static Image GetImageFromCopyPasteBuffer() { Thread t = new … Continue reading

Posted in C#, WinForms | Leave a comment

.Net Friend Assemblies

Have you ever want to create a “friend” assembly while programming with the .Net framework?  They can be very useful for segmenting your codebase amongst different projects. Friend Assemblies allow an assembly to see the internal methods and properties of a … Continue reading

Posted in C# | Leave a comment

JavaScript, UTF-8 Encoding: escape() vs. encodeURIComponent()

ASP.Net web applications default to UTF-8 encoding.  Now, if you are using JavaScript to create or change URLs, you might run into an issue with the way the function “escape()” encodes characters if you are handling the decoding in your … Continue reading

Posted in ASP.Net, C#, JavaScript | Leave a comment

Convert Hex to Readable String and Readable Text to Hex

If you ever need to convert Hex data to readable strings or the reverse, try the following methods: private static readonly  char[] HexChars = "0123456789ABCDEF".ToCharArray(); private static string ConvertToHex(string ascii) { if (ascii == null) return null; if (ascii == … Continue reading

Posted in C# | Leave a comment

Read a CSV file with Regular Expressions in .Net

Here’s how you can read a CSV file using Regular Expressions in .Net: </p> public static DataTable GetDataTableFromCsvFile(string file) { // Where the CSV data goes DataTable dt = new DataTable("CsvData"); // The pattern used to parse the CSV const … Continue reading

Posted in C#, Regular Expressions | Leave a comment