Hello,
i got this error when i click my button save.

Error: .Net SqlClient Data Provider: Conversion failed when converting datetime from character string.

please help me...
this is my save button code:

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        Dim check As Integer
        Dim conn As SqlClient.SqlConnection
        Dim cmdStudent As New SqlClient.SqlCommand
        Dim cmdStudent1 As New SqlClient.SqlCommand
        Dim daStudent As New SqlClient.SqlDataAdapter
        Dim dsStudent As New DataSet
        Dim dtStudent As New DataTable

        If txtKosPerolehan.Text = "" Or cbTarikhJaminan.Text = "" Or txtAksesori.Text = "" Or cbPembekal.Text = "" Then
            MsgBox("Penuhkan Borang!", MsgBoxStyle.OkOnly)
        Else
            If MsgBox("Are you sure to save data with Kategori : " & txtKosPerolehan.Text & "?", MsgBoxStyle.OkCancel, "Input confirm") = MsgBoxResult.Cancel Then
                ' do nothing
            Else
                Try
                    conn = GetConnect()

                    conn.Open()
                    cmdStudent = conn.CreateCommand
                    cmdStudent.CommandText = "SELECT * FROM Harta WHERE KOS_PEROLEHAN='" & Trim(txtKosPerolehan.Text) & " ' "
                    daStudent.SelectCommand = cmdStudent
                    daStudent.Fill(dsStudent, "Harta")
                    dtStudent = dsStudent.Tables("Harta")
                    If (dtStudent.Rows.Count > 0) Then
                        MsgBox("data dengan kategori " & Trim(txtKosPerolehan.Text) & " already in database", MsgBoxStyle.OkOnly, "Message :")
                    Else
                        Dim newQE_Date As Date
                        newQE_Date = Me.TARIKH_TERIMADateTimePicker.Value.ToShortDateString
                        cmdStudent1 = conn.CreateCommand
                        cmdStudent1.CommandText = "INSERT INTO Harta(KOS_PEROLEHAN, TARIKH_TERIMA, NO_PESANAN, TARIKH_JAMINAN, PEMBEKAL, NO_RUJUKAN_FAIL, CATATAN1, AKSESORI, NO_SIRI, KOS_BELI, CATATAN2) VALUES('" & Trim(txtKosPerolehan.Text) & "','" & Trim(newQE_Date.ToString("MM-dd-yyyy HH:mm:ss")) & "','" & Trim(txtNoPesanan.Text) & "','" & Trim(cbTarikhJaminan.Text) & "','" & Trim(cbPembekal.Text) & "','" & Trim(txtRujukanFail.Text) & "','" & Trim(txtcatatan1.Text) & "','" & Trim(txtAksesori.Text) & "','" & Trim(txtNoSiri.Text) & "','" & Trim(txtKosBeli.Text) & "','" & Trim(txtCatatan2.Text) & "')"
                        check = cmdStudent1.ExecuteReader.RecordsAffected()
                        If check > 0 Then
                            MsgBox("Student With Id " & Trim(txtKosPerolehan.Text) & " succesfully to added", MsgBoxStyle.OkOnly, "Message :")
                            MODALDataSet.AcceptChanges()

                        Else
                            MsgBox("Student With Id " & Trim(txtKosPerolehan.Text) & " Failure to added", MsgBoxStyle.OkOnly, "Message :")
                        End If

                        conn.Close()
                    End If
                Catch ex As Exception
                    MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
                End Try
            End If
        End If
    End Sub

dont use this Trim(newQE_Date.ToString("MM-dd-yyyy HH:mm:ss")), just get value and insert it .

Regards

commented: Nice!! +0

Thanks!!! now it works!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.