VB.NET - Inserting Date to SQL Database formatting issue

Posted on 2010-08-18
Medium Priority
Last Modified: 2012-05-10
I created a windows form application that will insert data into a SQL Server 2005 database.  When I do this, I get the following error.

Conversion failed when converting datetime from character string.

Here's my VB.Code:

Dim strDate as STring = Format(Cdate(DateTime.Now), "yyyy-MM-dd").ToString

The date in the database should look like the following:  2010-08-18 00:00:00.000

Trying to get it to same format for the insert.  Any ideas?
Question by:holemania
  • 2
LVL 18

Expert Comment

by:Anil Golamari
ID: 33469221
LVL 35

Accepted Solution

YZlat earned 2000 total points
ID: 33469281
where is your code that does the insert?

According to your error, you are trying to insert string into datetime field in the database
LVL 35

Expert Comment

ID: 33469306
try replacing

Dim strDate as STring = Format(Cdate(DateTime.Now), "yyyy-MM-dd").ToString


Dim strDate as STring = DateTime.Now.ToString("yyyy-MM-dd")

Author Closing Comment

ID: 33469561
Thanks.  You are right, I was inserting it into the wrong field.  Too many of them so I got less fields in my insert then my values.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question