SqlDateTime overflow thrown by Typed DataSet Insert

Posted by end-user on Stack Overflow See other posts from Stack Overflow or by end-user
Published on 2010-04-18T18:25:44Z Indexed on 2010/04/18 18:33 UTC
Read the original article Hit count: 410

I'm using a Typed DataSet with an Insert statement; I have a table that has a smalldatetime field defined to accept null values. When I insert from a .NET 2.0 FormView, I get a "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."

Now, I've read this post, and the parameter as sent to the class constructor is defined as

global::System.Nullable<global::System.DateTime> DoB

So, it looks like it should accept a Nullable obj. Additionally, the generated code is testing the value sent.

        if ((DoB.HasValue == true)) {
            command.Parameters[6].Value = ((System.DateTime)(DoB.Value));
        }
        else {
            command.Parameters[6].Value = global::System.DBNull.Value;
        }

Specifically, the error is occurring when generated SqlClient.SqlCommand.ExecuteScalar() runs:

        try {
            returnValue = command.ExecuteScalar();
        }

So, I guess my question is: how do I use a Typed DataSet to set a blank value (passed from a FormView on CommandName=Insert) to a null in a database?

© Stack Overflow or respective owner

Related posts about strongly-typed-dataset