Why is sql server giving a conversion error when submitting date.today to a datetime column?

Posted by kpierce8 on Stack Overflow See other posts from Stack Overflow or by kpierce8
Published on 2010-03-17T22:28:59Z Indexed on 2010/03/17 22:31 UTC
Read the original article Hit count: 305

Filed under:
|
|

I am getting a conversion error every time I try to submit a date value to sql server. The column in sql server is a datetime and in vb I'm using Date.today to pass to my parameterized query. I keep getting a sql exception

Conversion failed when converting datetime from character string.

Here's the code

Public Sub ResetOrder(ByVal connectionString As String)
        Dim strSQL As String
        Dim cn As New SqlConnection(connectionString)
        cn.Open()
        strSQL = "DELETE Tasks WHERE ProjID = @ProjectID"
        Dim cmd As New SqlCommand(strSQL, cn)
        cmd.Parameters.AddWithValue("ProjectID", 5)
        cmd.ExecuteNonQuery()

        strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _
        " (@ProjID, @TaskName, @DueDate)"
        Dim cmd2 As New SqlCommand(strSQL, cn)
        cmd2.CommandText = strSQL

        cmd2.Parameters.AddWithValue("ProjID", 5)
        cmd2.Parameters.AddWithValue("DueDate", Date.Today)
        cmd2.Parameters.AddWithValue("TaskName", "bob")
        cmd2.ExecuteNonQuery()
        cn.Close()
        DataGridView1.DataSource = ds.Projects
        DataGridView2.DataSource = ds.Tasks
    End Sub

Any thoughts would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about ADO.NET

Related posts about sql-server