Re: Dates

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Odd. I have never ever had any problems with a date in ISO 8601 format. Then again I live in Sweden, one of the few contries in the world to actually follow the international standard for dates...

You should use a parameterized query in a command object. That way you don't have to bother with the date format.

Frederik Vanderhaeghe wrote:
Result:
Server Error in '/ZoekSite' Application.
--------------------------------------------------------------------------------

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.]
System.Data.SqlClient.SqlDataReader.Read() +176
System.Data.Common.DbDataAdapter.FillLoadDataRow(SchemaMapping mapping) +175
System.Data.Common.DbDataAdapter.FillFromReader(Object data, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue) +260
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords) +129
System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +36
ZoekSite.WebForm1.SQLUitvoeren(String orderby) in D:\Inetpub\ASP\ZoekSite\Zoekpagina.aspx.vb:303
ZoekSite.WebForm1.btnzoeken_Click(Object sender, EventArgs e) in D:\Inetpub\ASP\ZoekSite\Zoekpagina.aspx.vb:93
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292



"Göran Andersson" <guffa@xxxxxxxxx> wrote in message news:O98acg2bGHA.4032@xxxxxxxxxxxxxxxxxxxxxxx
That doesn't matter. 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:
But in my SQL Server the field is of the type 'Datetime'

Fré
"Göran Andersson" <guffa@xxxxxxxxx> wrote in message news:e3xQBL2bGHA.4900@xxxxxxxxxxxxxxxxxxxxxxx
You are implicitly converting the date to a string, that means that it's using the culture settings of the current thread. You should specifically convert the date to a string, and specify how it should be converted. You can use a CultureInfo object, a DateTimeFormat object or a specific format string.

I suggest that you use the ISO 8601 date format. It's unambigous, contrary to the dd/MM/yyyy and MM/dd/yyyy formats, that will depend on the culture settings of the database server.

...
where datum = '" + kalender.SelectedDate.ToString("yyyy-MM-dd") + "'
...

Frederik Vanderhaeghe wrote:
I actually use the calendar object of asp.net. So the select is:
select *
from TBL_Bestanden_Zoeken
where datum = '" & kalender.SelectedDate & "'

But when I search in the SQL Server itself, I can't do this:
select *
from TBL_Bestanden_Zoeken
where datum = '31/12/2005'

The result of kalender.SelectedDate is '31/12/2005'. But when I search for datum = '2005/12/31' it works, but only on the SQL Server, not when I hardcode it in ASP.Net.

It's a very weird thing

Fré

"Göran Andersson" <guffa@xxxxxxxxx> wrote in message news:udW9Zx1bGHA.3908@xxxxxxxxxxxxxxxxxxxxxxx
Do you just send the date as a string to the database, or is it converted to a DateTime at any stage?

Frederik Vanderhaeghe wrote:
Hi,

I have made a search page on which users can search for documents. They can search by documentnumber, customername,... and also by date. Now the problem is that when a date is entered it doesn't show anything.

When I look at the SQL server Enterprise management and make a query there, the following works:
select *
from TBL_Bestanden_Zoeken
where (datum='2005-12-31')

When I code it like that in my asp.net site, that when the field is not empty, it searches for the documents with date='2005-12-31' then it doesn't give any results.

What can I do?

Fré


.



Relevant Pages

  • Re: Dates
    ... Create SqlParameter objects for the parameters and add them to the Parameters collecion of the command. ... 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, DataColumn parentChapterColumn, Object parentChapterValue) +260 ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Oh... no....
    ... select castas datetime) ... Obviously it would be better to use the "standard" format. ... Pro SQL Server 2000 Database Design ... >> data type of the date_variable in stored proc have been set to char ...
    (microsoft.public.sqlserver.programming)
  • Re: Searching on DATETIME Fields
    ... format since it is stored internally as numeric data. ... Another option your might consider is to pass the value as a datetime ... >> Tibor Karaszi, SQL Server MVP ... >>> The field I am querying is of DATETIME data type, ...
    (microsoft.public.sqlserver.server)
  • Re: How to Convert Char to Datetime
    ... The data type is, actually, Varchar 50 and it shows up as ... make sure that the format of the dates as stored in the ... Sylvain Lafontaine, ing. ... out-of-range datetime value." ...
    (microsoft.public.sqlserver.programming)
  • Re: Insert DateTime Value In DB Table?
    ... Is the date format whch you are trying to insert the same ... The data type of the column named "DOB" in the ... DB table is "datetime" which means SQL ...
    (microsoft.public.dotnet.framework.aspnet)