ObjectDataSource converts empty string to 01/01/01
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 开发者_C百科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
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);
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).
精彩评论