Click here to Skip to main content
12,948,567 members (62,929 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am new to 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 & "')"
            '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
        End Try
Posted 5-Aug-10 22:18pm
Updated 18-Oct-10 21:35pm
Dalek Dave433.3K
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Imports System.Data.SqlClient
Public Class Form6
    Dim cmd As New SqlCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            cmd.Connection = Class1.strcon
            cmd.CommandText = "insert into dt values('" & (TextBox1.Text) & "')"
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = System.DateTime.Now.ToString((" yyyy-MM-dd"))
    End Sub
End Class
Dalek Dave 19-Oct-10 3:36am
Good Answer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your code totally sucks. If you're new to VB.NET, you should learn it,. THEN learn SQL. However, as I'm sure you're being paid for this mess and don't care about being a good programmer, we'll assume you won't do that, right ?

Your code is a disaster b.c you're string mashing SQL. Which means any user who wants to, can erase your database. Read up on SQL injection. If you fix your SQL injection issues by using parameterised queries, a side effect will be to solve your DateTime issue.
souvikd 6-Aug-10 4:47am
Thanks for your suggestion, to be very true i didn't understand what do u meant by parameterised queries, and u have guessed it right i am doing it for my own project, i also didn't understand - how any one can make disaster to my DB. Please explain
souvikd 6-Aug-10 5:03am
Hey i just read about SQL Injection, i ll definitely take care of the same

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,344
CHill60 3,275
Maciej Los 2,703
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 19 Oct 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100