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 / .NET Framework / General / October 2004

Tip: Looking for answers? Try searching our database.

Date convertion problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonathan - 27 Oct 2004 19:11 GMT
Hi,
i am having a hard time trying to convert my date. Here is my problem :

I get an input date from a user in a form (in a asp:textbox control). The
date is validated with the "dd/MM/yyyy" format and stored in a variable.

Dim strDate As DateTime = Date.Text  (strDate contains:20/10/2004 00:00:00)
Or
Dim strDate As String = Date.Text (strDate contains:20/10/2004)

If i want to insert my strDate value in it's field in my SQL Server 2000
database, then i can't because the date field (datetime type) is in a
"MM/dd/yyyy" format.

Is there a way/function/method that can convert this date. For example,
ToShortDateString() on my variable would work if my CurrentCulture would
NOT be "en-GB" but it is...

The way i thought was to convert my variable just before inserting in my
database by using the CONVERT function in my SQL statement but i am not
sure if it is the right thing to do. I even tryed it and it failed :

Dim cmd As New SqlCommand ("INSERT INTO MYTABLE (Date)VALUES('CONVERT
(DATETIME," & strDate & ",101)')", MyConnection)

My Error message:
System.Data.SqlClient.SqlException: Syntax error converting datetime from
character string.

What i am doing wrong?
Isn't there any other way to do such a thing (e.g. change the Culture just
the time to insert the record)?

Any help is greatly appreciated.

Jonathan
Jon Skeet [C# MVP] - 27 Oct 2004 22:29 GMT
> i am having a hard time trying to convert my date. Here is my problem :
>
[quoted text clipped - 8 lines]
> database, then i can't because the date field (datetime type) is in a
> "MM/dd/yyyy" format.

Don't include the string format of the date into your SQL statement at
all. Use database parameters instead.

See http://www.pobox.com/~skeet/csharp/faq/#db.parameters

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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.