the safest way of handling datetime between asp.net and ms sql



Hi there

I know that a problem of different datetime strings formatting between
asp.net and ms sql has been covered here widely, but what I couldn't
find is the 100% safe way of getting datetime values from ms sql,
processing them in asp.net and getting them back into sql without any
risk with inconsistent Culture settings etc.

My question is: wouldn't keeping the datetime variables all the time in
datetime type, without any String conversions, prevent all such
problems?

I was trying to check it but there are few obstacles when sticking to
declarative databinding:

1) session parameters

Let's say I'm keeping datetime variable in a session. Then, I want to
use it as a parameter in a datasource. But the variable in session will
be boxed to Object type, and I have no control on how datasource will
cast it - how can I prevent it from casting it to string and inserting
it into the query as a culture-unsafe string?

I think Select Parameters collection in Data Source properties might
help - after choosing session parameter I can show Advanced Properties
and change it's type (from string to datetime) - will Data Source use
this information to cast the session variable onto datetime properly?

2) dropdownlist values

I want to bind some datetime values to dropdownlist and use its
selected value as a control parameter of a data source. Does it mean,
that implicit toString conversion must happen during binding to ddl?
ddl.selectedValue has String type - does it mean, that everything bound
to ddl is converted to string - no way to prevent it? If it's true,
then how would it be done in case of datetime, which may be casted to
string in many different ways (because of formatting possibilities)?
Would standard datetime.toString() be used when populating ddl?


thanks a lot for any insight
cheers
HP

.



Relevant Pages

  • Re: Dates
    ... The SQL query is a string, so the date in it is a part of the string, not a separate DateTime value. ... Frederik Vanderhaeghe wrote: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Failed to convert parameter value from a String to a DateTime
    ... perhaps the error occurs because SQL Server defaults to nvarchar when a parameter is NULL. ... I would try to declare the parameter as DateTime before to assign it the DBNull.Value. ... value may be a valid date such as '01/01/2007' or a null string. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Dates
    ... The conversion of a char data type to a datetime data type resulted in an ... out-of-range datetime value. ... System.Data.Common.DbDataAdapter.FillFromReader(Object data, String ... srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: BETWEEN in dynamic SQL
    ... I have a mess in printed SQL. ... > Instead of EXEC sp_executesql @sql ... >> Syntax error converting datetime from character string. ...
    (microsoft.public.sqlserver.programming)
  • Re: DateTime WebService Discrepancy.
    ... When the datetime is sent by the webservice, ... > Ofcourse I had not even though of just passing a string as a parameter ... > locally and cut out the call to the web service. ...
    (microsoft.public.dotnet.framework.compactframework)