if possible, always try to pass the date in YYYYmmDD format
Main Topics
Browse All TopicsHi,
I'm fairly new to Visual Studio 2008 and I'm sure that this question has been answered before, but I just can't seem to get a solution that works. I've taken data from a .csv file and I'm trying to store the date into a sql table in VB.net. into a datetime field.
I'm in the UK so the dates are dd/mm/yyyy
Simplified the code is
dim mysql as string
mysql = "Insert into mytable(MyID, MyDate) values ('" & mydata(0).tostring & ",'" & mydate &"')"
This becomes
Insert into mytable(MyID, MyDate) values ('378549', '27/07/2009')
I get the error message
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
I need to know exactly what I should be putting into my SQLstring so that VB can pass this successfulyy to SQL. Can anyone help.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
so does the statement become
Insert into mytable(MyID, MyDate) SELECT '378549', CONVERT(datetime, '27/07/2009' ,103 )
because neither the date nor the id number are literals they are both supplied from data fields.
so would that be
Insert into mytable(MyID, MyDate) SELECT myID, CONVERT(datetime, mydate ,103 )
or would I have to be adding quotes / apostropes around the fields?
Instead of concatenating the string to be executed (bad form, can lead to SQL Injection issues), you should be using parameters and setting the parameters.
Because I am not a programmer - I don't recall the exact syntax, but basically you create the command with parameters, add the parameters to the collection - set the parameters and then execute. When you do that, the parameters don't need to be in any specific format - they will be sent to SQL Server correctly as datetime data types.
Here is an article that you can review: http://weblogs.asp.net/ble
Essen
Business Accounts
Answer for Membership
by: aneeshattingalPosted on 2009-09-09 at 08:27:02ID: 25292274
Insert into mytable(MyID, MyDate) SELECT '378549', CONVERT(datetime, '27/07/2009' ,103 )