I had the same issue while passing date variable from vb.net to SQL Server. I tried with Parameters as suggested above. Still I am facing the same issue. Pl guide.
Dim today As String
today = System.DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss")
Dim todate As DateTime = DateTime.ParseExact(today, "MM-dd-yyyy hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
SQLCom.CommandType = CommandType.Text
SQLCom.Parameters.AddWithValue("@TODAY", CType(todate, Date))
SQLCom.CommandText = "SELECT DATEDIFF(MM,FORMAT(UPD_DATE,'dd-MM-yyyy'),FORMAT(@TODAY,'dd-MM-yyyy')) AS DATE_DIFF FROM tblEMP WHERE EMPID = '" & ID & "';"
Try
Dim daEmpExp As New SqlDataAdapter
daEmpExp.SelectCommand = SQLCom
SQLConn.Open()
daEmpExp.Fill(dsEmpExp)
SQLConn.Close()
Catch ex As Exception
MsgBox("Sorry!!! " & ex.Message)
Finally
If SQLConn.State = ConnectionState.Open Then
SQLConn.Close()
End If
End Try
End If
Return dsEmpExp
Still it catches error "Conversion failed when converting date and/or time from character string."