tags:

views:

94

answers:

2

Hi,

I'm converting a project to use ObjectDataSources instead of SqlDataSources. I'm using text boxes for user input of dates. When these were left blank with a SqlDataSource, the value sent to the stored procedure was null, allowing me to get the right results from a query with no date specified.

With the ObjectDataSource, a blank input to the date text boxes gets converted to 01/01/0001 00:00:00, which is not acceptable for the query.

Anyone know how I can receive a null value for this parameter on my ObjectDataSource when the textbox is left empty?

thanksHi

A: 

I found a way to do this, by checking for mindate inline and replacing with null. Feels a bit hacky, but it works fine, and I can't find any other suggestions. If you have a better idea, please let me know!

cmd.Parameters.AddWithValue("@ABCId", objectType == "AbcId" ? objectId.ToString() : null);
            cmd.Parameters.AddWithValue("@CounterpartyId", objectType == "CounterpartyId" ? objectId.ToString() : null);
            cmd.Parameters.AddWithValue("@FromDate", fromDate == DateTime.MinValue ? null : fromDate.ToLongDateString());
            cmd.Parameters.AddWithValue("@ToDate", toDate == DateTime.MinValue ? null : toDate.ToLongDateString());
            cmd.Parameters.AddWithValue("@RelationshipId", objectType == "RelId" ? objectId.ToString() : null);
            cmd.Parameters.AddWithValue("@ShowNonTradePayments", showNonTradePayments);
            cmd.Parameters.AddWithValue("@ShowTradePayments", showTradePayments);
willemaster
You say "when these were left blank ... the value sent to the stored procedure was null...", are you sure it wasn't sent as DateTime.MinValue, as you had to specifically handle that in your answer-code? If toDate isn't nullable, it can never be null.
Lasse V. Karlsen
I meant that originally, when this was done using a SqlDataSource, then date textboxes, when left blank, would result in null values reaching the stored proc. My stored proc would then include all results regardless of date. When I tried to do the same with the ODS, MinDate values were being passed through to the stored proc, resulting in error or incorrect results. So I had to find a way to convert incoming MinDate values to nulls so the stored proc would behave correctly - that's what the code above does.
willemaster
A: 

I ended up having "01/01/1900" as my "null". Not the book solution, but actually it does not bother us. We have our own class for conversions, and it catches these sentinel values transparently, so we can forget about this issue for most things. In HQL/SQL queries, instead of asking for not null values, we just ask for values greater than 01/01/1901 (not null) or values smaller then 01/01/1901 (null).

Daniel Dolz
Yep, I can see that that would work, but I'm surprised MS hasn't provided a more elegant solution, especially when this workaround's not necessary with a SqlDataSource.
willemaster