SqlDateTime.TryParse, almost

ASP.Net, SQL Add comments

kick it on DotNetKicks.com

I talked about an issue I ran into between DateTime.MinValue and SqlDateTime.MinValue in a previous post. One other point I noticed while researching the topic was that, unlike almost all other types, SqlDateTime doesn’t have a TryParse method.

I really like the TryParse method because it helps me write code that is more terse and clean. I can easily use the TryParse methed as the condition for an if statement to handle the outcome without have to resort to ugly try catch blocks all over the place.

On Ian’s suggestion in the comments to my previous article I decided to take a stab at writing an extension method to implement this functionality. Now I said almost in the post title because I couldn’t get the TryParse method attached to the SqlDateTime native class. Now this could be due to this being my first attempt at an extension method so if anyone has any advice or suggestions on making this solution better feel free to leave them in the comments and I’ll update the code.

The solution

As I said I couldn’t get the method attached to the native type so this is the next best thing. I have declared a XSqlDateTime type that contains the TryParse method.

public static class XSqlDateTime {
	public static bool TryParse(string str, out SqlDateTime output) {
		try {
			output = SqlDateTime.Parse(str);
			return true;
		}
		catch {
			output = SqlDateTime.MinValue;
			return false;
		}
	}
}

This is easily used like so.

SqlDateTime date;
XSqlDateTime.TryParse("1/1/1953 12:00 AM", out date);

Just like all other implementations of TryParse on other types, if this method returns false it will assign the default type value to the output variable. In this case it is SqlDateTime.MinValue.

Hope this is useful to someone. I know it is for me. Cheers.

Popularity: 23% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

4 Responses to “SqlDateTime.TryParse, almost”

  1. John S. Says:

    Make sure the class is static along with the method and that the calling class has a reference to the class with your extensions. This worked for me:

    public static class Extensions
    {
    public static bool TryParse(this SqlDateTime dt, string input, out SqlDateTime output)
    {
    // logic here
    return false;
    }
    }

  2. Miron Says:

    Hey, how about using the DateTime.TryParse:
    public static class XSqlDateTime
    {
    public static bool TryParse(string str, out SqlDateTime output)
    {
    DateTime dt;
    if (DateTime.TryParse(str,out dt))
    {
    if (dt SqlDateTime.MaxValue.Value)
    {
    output = SqlDateTime.MaxValue.Value;
    }
    else
    {
    output = (SqlDateTime)dt;
    }
    return true;
    }
    else
    {
    output = SqlDateTime.MinValue;
    return false;
    }
    }
    and saving the heavy block of the try & catch.

  3. Miron Says:

    Sorry, all the code mixed up.
    You can see the code here:
    http://mironabramson.com/blog/page/SqlDateTimeTryParse.aspx

  4. Justin Says:

    Thanks John,

    I’ll give your suggestion a try and then update the post.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Login