Date convertion problem

J

Jonathan

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
 
J

Jon Skeet [C# MVP]

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

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
 

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

Top