Click here to Skip to main content
15,885,853 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted
Updated 30-Apr-18 17:59pm
v2

MSIL
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
        Try
            cmd.Connection = Class1.strcon
            cmd.CommandText = "insert into dt values('" & (TextBox1.Text) & "')"
            cmd.ExecuteNonQuery()
            MessageBox.Show("inserted")
        Catch ex As Exception
            MsgBox(ex.Message)
        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
 
Share this answer
 
Comments
Dalek Dave 19-Oct-10 3:36am    
Good Answer.
Member 12963195 17-Aug-17 6:06am    
only save date in database
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.
 
Share this answer
 
Comments
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
Member 14996369 30-Nov-20 21:21pm    
F*** you ,because i can't solve my problem so i post my question here , if you don't help me to resolve my question please sit at there keep quiet thank you
Christian Graus 30-Nov-20 21:24pm    
Holy sh*t, it's been 10 years and you're still working on this?

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



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