Перейти к основному контенту
Центр разработки для Windows

 none
Conversion failed when converting date and/or time from character string RRS feed

  • Вопрос

  • i am workin with sql server and vb 10. when i run the program, i encounter the following error - Conversion failed when converting date and/or time from character string

    this my code

    Private Sub updateinvoiceDB()
            If InvoicedetailsDataGridView.Rows.Count > 0 Then
    
                Dim invoiceID As Integer = CInt(InvoiceTableAdapter.GetLastIdentityValue)
                Dim datedb As Date = DateDateTimePicker.Value
    
    
                myconnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\kankukhDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
                myconnection.Open()
                sql = "INSERT INTO invoice (date, name, address, total, tax1, tax2) VALUES ('" & datedb & "','" & NameTextBox.Text & "','" & AddressTextBox.Text & "','" & productTotal & "','" & tax1 & "','" & tax2 & "')"
    
                adapter.InsertCommand = New SqlCommand(sql, myconnection)
                adapter.InsertCommand.ExecuteNonQuery()
                myconnection.Close()
    
            End If
    
        End Sub

    please do help me


    shehzi

    15 июня 2012 г. 13:07

Ответы

  • Hi,

    has this error occured on the command execution line (ExecuteNonQuery()) ?

    It seems your "date" in database is a string, so you have to convert date to to stiring, like:

    '" & datedb.toString("dd-MMM-yyyy") & "',
    


    Mitja

    • Помечено в качестве ответа i.shehzi 16 июня 2012 г. 5:16
    15 июня 2012 г. 14:14
  • Hey shezteq:)

    Your solution will cause the problem of SQL injection……I don't think to combine string splits a good way——So plz change to this:

    Private Sub updateinvoiceDB()
            If InvoicedetailsDataGridView.Rows.Count > 0 Then
    
                Dim invoiceID As Integer = CInt(InvoiceTableAdapter.GetLastIdentityValue)
                Dim datedb As Date = DateDateTimePicker.Value
    
    
                myconnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\kankukhDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
                myconnection.Open()
                sql = "INSERT INTO invoice ([date], [name], address, total, tax1, tax2) VALUES (@date,@name,@address,@total,@tax1,@tax2)"
                adapter.InsertCommand = New SqlCommand(sql, myconnection)
                adapter.InsertCommand.Parameters.AddWithValue("@date",datedb.ToString("yyyy/MM/dd"))
                adapter.InsertCommand.Parameters.AddWithValue("@name",your real value)
                ……………………
                adapter.InsertCommand.Parameters.AddWithValue("@tax2",your real value);
                adapter.InsertCommand.ExecuteNonQuery()
                myconnection.Close()
            End If
        End Sub

    PS:If you only want to do something like a simple inserting,just use SqlCommand instead of SqlDataAdapter instead……:D


       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    • Помечено в качестве ответа i.shehzi 16 июня 2012 г. 5:16
    16 июня 2012 г. 2:38

Все ответы