DateTimePicker control

Hi,

I try to insert date time pricker control value to Sql server data base, using CType

Ctype(DH_DateTimePicker.Value, DateTime)

I received the next message:

System.Data,SqlClient.SqlException: {"Incorrect syntax near '.2006'."}

Can explain me the message

Thanks!!




Answer this question

DateTimePicker control

  • Chris.Stewart

    I would try something like this

    strSQL = "INSERT INTO Employees (Name, HDate) VALUES ('" & _

    PrepareStr(txtName.Text) & "', #" & _

    Me.HDateDateTimePicker.Value.ToString & "#)"



  • ozhonetech

    The date is in the wrong format for sql server to use. I would use a parameter instead.



    String strConn = "Server = .;Database = AdventureWorks; Integrated Security = SSPI;";
    SqlConnection conn = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand("Update HumanResources.Employee Set ModifiedDate = @NewDate Where EmployeeID = 1", conn);
    cmd.Parameters.AddWithValue("@NewDate", dateTimePicker1.Value);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();




  • Priyanka Choughule

    Thanks !

    I think that the problem is in the date format I 've used "dd.mm.yyyy".

    More details:

    Dim strCnn As String = My.Settings.EmsPrintConnectionString

    Dim cnSQL As SqlConnection

    Dim cmSQL As SqlCommand

    Dim strSQL As String

    strSQL = "INSERT INTO Employees (Name, HDate) VALUES (" & _

    PrepareStr(txtName.Text) & "," & _

    CType(Me.HDateDateTimePicker.Value, DateTime) & ")"

    cnSQL = New SqlConnection(strCnn)

    cnSQL.Open()

    cmSQL = New SqlCommand(strSQL, cnSQL)

    cmSQL.ExecuteNonQuery()

    Does this suggest anything to you

    Thanks in advance !

    Best regards,

    George



  • Tdah

    Hi Ken,

    I've tested the your suggestion but now arise the error message (input date 20.10.2006):

    Incorrect syntax near '.10'.

    I found that the reason is in the date format. I insert date by DateTimePicker in "mm.dd.yyyy" format. But SQL surver use ANSI format "yyyy-mm-dd" to store datetime.

    Do you know if is a way to change the language setting in SQL Server

    Thanks

    Best Regards

    George

    P.S.: Mikael Egner in ADO.NET Technology Preview found part of the error- missing two single-quotes (').



  • DateTimePicker control