Click here to Skip to main content
15,893,668 members

How to insert date\time in sql DB using vb.net

souvikd asked:

Open original thread
Hi,

I am new to vb.net and I am trying to use a datetime picker in my form.
While inserting date to the sql server in datetime column I am getting an error stating it cannot convert the string to date.

I understand the fact that I am trying to insert string into date column, hence please help me with the right code. Here is the code I am using.

Dim sqlCmdStr_Insrt As String = "INSERT INTO Project_details(project_Id,Project_name,Description," _
        & "Stdate,NdDate)Values('" & txtPro_id.Text & "','" & txtPro_Nm.Text & "','" & txtPro_Dsc.Text & "','" _
        & dateSt_Projct.Value.ToLongDateString & "','" & dateEnd_Project.Value.ToLongDateString & "')"

Try
            'Executing the Sql Query to insert the data to the Project_details table
            DataStore.ExecuteNonQuery(sqlCmdStr_Insrt, sqlConnStr)
            Call btnReset_CrtPro_Click(sender, e)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
Tags: .NET (.NET 3.5)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900