ExtensionMethod.NET Home of 876 C#, Visual Basic, F# and Javascript extension methods


For use with old school ado.net database command parameters. This basically converts the string to System.DBNull.Value if the string is null else it returns the string.


public static class StringExtensions
    public static object ForDatabase(this string str)
        if (str == null)
            return System.DBNull.Value;

        return str;


SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add(new SqlParameter("@Email",email.ForDatabase());

Author: Tomas Tomasson

Submitted on: 17 jan 2011

Language: C#

Type: System.String

Views: 4337