Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

problem with date

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 02 Aug 2007 13:13 GMT
Hi,

i have an application which works with date.
The regional settings of the computer (XP prof. dutch version) are set to
French (Belgium).
Asp.net and Sql server take the short date format of the regional settings
(e.g. 2/08/2007 or 13/08/2007).
I checked both: that's ok.

When i try to insert a date in a datetime field in sql server which is e.g.
13/08/2007, i get the error:
"The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value."

This is the code:
comd.CommandText = "insert into mytable (datbegin,datend) values('" & dbeg &
"','" & dend & "')"

I checked just before inserting the values (with response.write) and they
are: 2/08/2007 and 13/08/2007.

Why doesn't sql server accept those values? They are conform the settings,
no?
Or maybe the Insert command transforms the format of tye date?

Thanks for help
Mark
Tibor Karaszi - 02 Aug 2007 13:23 GMT
First, Profiler is your friend. Use it to find out that SQL is actually submitted by you app.Never
trust a tool/dev environment, which can do anything it like with a datetime value before presenting
it in a human readable format for you.

Here's a backgrounder on datetime that might help:
http://www.karaszi.com/SQLServer/info_datetime.asp

Also, I strongly encourage you to keep datetime values as date datatypes in your host language, and
use parameterized queries instead of constructing datetime literal. this way, it will always work.
And you also gets tons of other benefits from using parameterized queries.
Signature

Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi

> Hi,
>
[quoted text clipped - 20 lines]
> Thanks for help
> Mark
Mark Rae [MVP] - 02 Aug 2007 13:25 GMT
> Why doesn't sql server accept those values?

Because there is no 13th month...

> They are conform the settings, no?

No.

> Or maybe the Insert command transforms the format of tye date?

It doesn't, unless you parameterise it.

Replace dbeg with dbeg.ToString("dd MMM yyyy") - you may have to cast dbeg
to a DateTime first...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Andrew J. Kelly - 02 Aug 2007 13:28 GMT
You are passing these dates in as strings. You really should build
parameters for the statement or better yet use stored procedures. But in any
case what is the dateformat set to for that connection?  See SET DATEFORMAT
in BooksOnLine for more details. Since you are passing it as a string SQL
Server will try to convert it to a Datetime but it needs to know which is
the month, day, year etc.  The preferred way to deal with Dates as strings
is to use the ISO or ANSI formats so there is never a mistake in this
regard. For instance the date in this format will always work regardless of
language or date settings.    'yyyymmdd'   See here for more details as
well:

http://www.karaszi.com/SQLServer/info_datetime.asp
Guide to Datetimes
http://www.sqlservercentral.com/columnists/bsyverson/sqldatetime.asp
Datetimes
http://www.murach.com/books/sqls/article.htm
Datetime Searching

Signature

Andrew J. Kelly SQL MVP

> Hi,
>
[quoted text clipped - 23 lines]
> Thanks for help
> Mark
Jesse Houwing - 02 Aug 2007 13:32 GMT
Hello Mark,

> Hi,
>
[quoted text clipped - 25 lines]
> Thanks for help
> Mark

All your problems will go away if you start using parameters instead of inlining
the date as a string.

DateTime sbeg = new DateTime(2007,2,8);

comd.CommandText = "insert into mytable (datbegin,datend) values(@dstart,
@dend)"
comd.Parameters.Add(new SqlParameter("@dstart", dbeg));
comd.Parameters.Add(new SqlParameter("@dend", dend));

This will mak
Jesse Houwing - 02 Aug 2007 13:44 GMT
Hello Mark,

> Hi,
>
[quoted text clipped - 25 lines]
> Thanks for help
> Mark

There are 3 solutions, 2 at the .NET side, 1 at the SQL side

1) All your problems will go away if you start using parameters instead of
inlining the date as a string. This is the best solution you could choose.

   DateTime dbeg = new DateTime(2007,2,8);
   DateTime dend = new DateTime(2007,2,13);
   comd.CommandText = "insert into mytable (datbegin,datend) values(@dstart,
@dend)";
   comd.Parameters.AddWithValue("@dstart", dbeg);
   comd.Parameters.AddWithValue("@dend", dend);

This will make sure the data is passed as a DateTime.

2) Another option is to format the dates you pass to the query in the following
format: yyyy.mm.dd. You can use string.format to do that easily:

   comd.CommandText = string.Format("insert into mytable (datbegin,datend)
values({0:yyyy.MM.dd}, {1:yyyy.MM.dd})", dbeg, dend);

3) You could add a function around your inserted strings to parse the date
in de SQL statement.

   comd.CommandText = string.Format("insert into mytable (datbegin,datend)
values(convert(datetime, {0}, 105), convert(datetime, {1}, 105))", dbeg,
dend);

More info on the style id's (105 in this case) can be found here: http://sqljunkies.com/HowTo/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk

Whichever route you choose consider this:
- have a look at the string.Format function. It's your bets friend when concatenating
lots of strings together in a readable fashion. A
- have a look at parameters for SQL queries. They're faster, easier to read
and maintain and more secure to boot. They also have less trouble with conversions
as you'll find out.

Jesse
Mark - 02 Aug 2007 15:40 GMT
Thanks for replying

> Hello Mark,
>
[quoted text clipped - 66 lines]
>
> Jesse

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.