DateTime output parameter in action procedure

Posts   
 
    
brpalmer
User
Posts: 23
Joined: 08-Nov-2006
# Posted on: 20-Apr-2007 12:39:45   

My environment is as follows:

LLBLGen Pro v2.0.0.0 Final (March 19th, 2007) SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll v2.0.7.319 Adapter + .NET v2.0 SQL Server 2005 Express

I have an action procedure in my project that calls the following stored procedure.

CREATE PROCEDURE [dbo].[GetServerDateTime]
    @ServerDateTime datetime output
AS
BEGIN
    SET NOCOUNT ON;
    
    SET @ServerDateTime = GETDATE();
END
GO

If I call this procedure in my application as follows it works fine, correctly returning the database server time.

DateTime serverDateTime = new DateTime(2007, 1, 1);
ActionProcedures.GetServerDateTime(ref serverDateTime);

If, however, I change the initialisation of the variable to the following, it fails.

DateTime serverDateTime = new DateTime();
ActionProcedures.GetServerDateTime(ref serverDateTime);

The exception is as follows.

System.Data.SqlTypes.SqlTypeException was unhandled
  Message="SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value)
       at System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value)
       at System.Data.SqlTypes.SqlDateTime..ctor(DateTime value)
       at System.Data.SqlClient.MetaType.FromDateTime(DateTime dateTime, Byte cb)
       at System.Data.SqlClient.TdsParser.WriteValue(Object value, MetaType type, Int32 actualLength, Int32 encodingByteSize, Int32 offset, TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at RinkerAustralia.TDS.Workflow.DAL.DatabaseSpecific.DataAccessAdapter.CallActionStoredProcedure(String storedProcedureToCall, SqlParameter[] parameters) in D:\Work\Workflow\DAL\DatabaseSpecific\DataAccessAdapter.cs:line 192
       at RinkerAustralia.TDS.Workflow.DAL.DatabaseSpecific.ActionProcedures.GetServerDateTime(DateTime& serverDateTime, DataAccessAdapter adapter) in D:\Work\Workflow\DAL\DatabaseSpecific\ActionProcedures.cs:line 61
       at RinkerAustralia.TDS.Workflow.DAL.DatabaseSpecific.ActionProcedures.GetServerDateTime(DateTime& serverDateTime) in D:\Work\Workflow\DAL\DatabaseSpecific\ActionProcedures.cs:line 44
       ... etc

Is this a bug?

It seems in the second case that it doesn't even try to call the stored procedure. It's like it validates the parameter value before calling the procedure, finds it in error, and returns. I can't confirm whether it calls the procedure or not as there is no sign of it in the DQE trace regardless of whether it succeeds or fails.

Any help would be appreciated.

Regards, Bruce

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Apr-2007 17:02:02   

The problem is that there are limitations difference between .NET DateTime and SqlDateTime. When you instanciate a .NET dateTime variable, it takes the default value of the .NET dateTime type, which is DateTime.MinValue (1/1/0000), while SqlDateTime have a minimum of 1/1/1753.

So I suggest you set the variable to a value >= 1/1/1753, or use a nullable paramter for the stored procedure, so you can pass a dateTime of null value.

Another different solution is to use Database funtion calls to call the GetDate() Sql function ref: Manual "Using the generated code -> Calling a database function"

brpalmer
User
Posts: 23
Joined: 08-Nov-2006
# Posted on: 21-Apr-2007 01:39:20   

Okay, thanks for the advice. Here is what I did in case someone else can benefit.

I changed the stored procedure to the following. Note the change in the declaration of the parameter which allows it to be null.

CREATE PROCEDURE [dbo].[GetServerDateTime]
    @ServerDateTime datetime = NULL output
AS
BEGIN
    SET NOCOUNT ON;
    
    SET @ServerDateTime = GETDATE();
END
GO

Next I modified the parameter to the action procedure in the LLBLGen Pro project to be nullable and regenerated the code.

Finally I modified my calling code in the application as follows.

DateTime? serverDateTime = null;
ActionProcedures.GetServerDateTime(ref serverDateTime);

Now it works fine.

Regards, Bruce