Writing value to datetime field in SQL database in dd/mm/yyyy format

  • Thread starter Thread starter Ben Williams
  • Start date Start date
B

Ben Williams

Hello,

I'm hoping i'm posting to the correct newsgroup - this question involves
both a SQL database and VB. I am a newbie and i'm writing a program in
which one of a handful of fields will have a date entered. The contents of
this field (and others) need to be written back to a SQL database.

The 'date' field will be input in the format of the Regional settings
(mainly dd/mm/yyy for the UK). When it comes to the point where it is
written to the SQL database, VB changes the date to mm/dd/yyyy format. The
SQL server is expecting the date in the format dd/mm/yyyy so returns an
error saying the date is out of range.

Is there a simple way around this problem that I have missed as I am sure
other people must have stumbled upon this before?

If I could work through this problem I could get into some proper
development!

Any thoughts would be really appreciated...

Thanks!

Ben
 
Hi Ben,

You should be able to do something like this:
mdatestring = mdate.ToString("ddmmyyyy")

HTH,

Bernie Yaeger
 
Thanks for your reply. That works....almost!

It converts the date but it seems to miss out the month

i.e. "16002004"

This is the code snippet:

Dim dteDueDate As DateTime
Dim strDueDate As String

'Convert dteDueDateA.Text to ddmmyyyy
dteDueDate = dteInputDate.Text
strDueDate = dteDueDate.ToString("ddmmyyyy")

If I step through the code now and replace the 00 with 02 it will write
it to the database which is a major step forward!

I'm so close now - any thoughts would again be appreciated!

Ben
 
* "Ben Williams said:
I'm hoping i'm posting to the correct newsgroup - this question involves
both a SQL database and VB. I am a newbie and i'm writing a program in
which one of a handful of fields will have a date entered. The contents of
this field (and others) need to be written back to a SQL database.

The 'date' field will be input in the format of the Regional settings
(mainly dd/mm/yyy for the UK). When it comes to the point where it is
written to the SQL database, VB changes the date to mm/dd/yyyy format. The
SQL server is expecting the date in the format dd/mm/yyyy so returns an
error saying the date is out of range.

Why not use a parameter with the insert command?

More info:

<
 
Hi Ben,

A very simple change will solve the problem:

strDueDate = dteDueDate.ToString("ddMMyyyy") ' use caps for month

HTH,

Bernie
 
Back
Top