Click here to Skip to main content
11,428,497 members (65,341 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5
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 5-Aug-10 23:18pm
souvikd437
Edited 18-Oct-10 22:35pm
v2
Rate this: bad
good
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
        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
  Permalink  
Comments
Dalek Dave at 19-Oct-10 3:36am
   
Good Answer.
Rate this: bad
good
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.
  Permalink  
Comments
souvikd at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.150428.2 | Last Updated 19 Oct 2010
Copyright © CodeProject, 1999-2015
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