Dates

  • Thread starter Frederik Vanderhaeghe
  • Start date
F

Frederik Vanderhaeghe

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é
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Do you just send the date as a string to the database, or is it
converted to a DateTime at any stage?
 
R

Richard Brown

I've had problems with date format before.

With SQL Server it usualy expects MM/DD/YYYY by default (I think)

Try changing the date format, and see what happens.
 
F

Frederik Vanderhaeghe

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é
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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") + "'
....
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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.
 
F

Frederik Vanderhaeghe

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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 said:
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 said:
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.
 
F

Frederik Vanderhaeghe

And how do I do that??

Fré

Göran Andersson said:
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 said:
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 said:
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é
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é

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é
 
M

Mark Rae

What can I do?

1) Use parameterised queries.

2) If you are actually building up the SQL dynamically, make sure you pass
your date in a TOTALLY UNAMBIGUOUS format. I always use dd MMM yyyy, which
is guaranteed to be evaluated as the same date irrespective of the locale,
regional settings, SQL Server installation options etc.

3) Be aware of the time portion of datetime and smalldatetime fields.

E.g. if you have a table where datetime fields are populated automatically
through the getdate() function, the values that get written to the database
will include the time portion too.

The clause "where (datum='2005-12-31')" is interpreted by SQL Server as
"show me all the records where the datum field contains 31 Dec 2005
00:00:00" - any records, say, where the datum field contains 31 Dec 2005
10:00:00 will not be returned.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Create an SqlCommand object and put the query in it. Use parameter names
like @Date in the query instead of the values (including the surrounding
apostrophes - the command will add them). Create SqlParameter objects
for the parameters and add them to the Parameters collecion of the command.

Frederik said:
And how do I do that??

Fré

Göran Andersson said:
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 said:
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



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é
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é

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é
 
F

Frederik Vanderhaeghe

I tried the following, in the Enterprise Manager I executed the query with
different date formats
Select *
From TBL_Bestanden_Zoeken
Where datum = ' '

It worked (gave results) with '12/31/2005' , '12-31-2005' , '12.31.2005' ,
'2005/12/31' , '2005-12-31' , '2005.12.31'
It didn't work with: '31/12/2005' , '31-12-2005' , '12.31-2005'

In my ASP.Net code I typed the following:
select * from TBL_Bestanden_Zoeken where datum = '12/31/2005' and tried it
with all the ones that worked in the Enterprise Manager
Not a single date format gave any results.

????

Fré
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top