1

I'l like to get the datetime from my PC and send it over to my table Here it is : Failure to communicate / String was not recognized as a valid DateTime How to make the current date so when I pass it over to my DB ?

  Dim regDate As Date = Date.Now()
            Dim strDate As String = regDate.ToString("MM\/dd\/yyyy")

SQL throws me an error if I use this :

SQLcmd.Parameters.AddWithValue("@Data", Convert.ToDateTime(strDate))

Error is : String was not recognized as a valid DateTime

            Dim SqlQuery1 As String = "INSERT INTO pontaj(id_pontaj,id,prezente,data) " & _
                "VALUES (0,@Id,1,@Data)"


            Using con = New MySqlConnection("Server = localhost;Database = users; Uid=root; Pwd = password")
                Using SQLcmd = New MySqlCommand(SqlQuery1, con)
                    con.Open()
                    'SQLcmd.ExecuteNonQuery()

                    '  SQLcmd.Parameters.Add(New MySqlParameter("@Id_presence", TextBox1.Text))
                    '  SQLcmd.Parameters.Add(New MySqlParameter("@Id", TextBox2.Text))
                    '  SQLcmd.Parameters.Add(New MySqlParameter("@Hours", TextBox3.Text))
                    '  SQLcmd.Parameters.Add(New MySqlParameter("@Date", TextBox4.Text))


                    ' SQLcmd.Parameters.AddWithValue("@Id_presence", Convert.ToInt32(TextBox1.Text))
                    SQLcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(Label5.Text))
                    ' SQLcmd.Parameters.AddWithValue("@Prezente", Convert.ToInt32(TextBox3.Text))
                     SQLcmd.Parameters.AddWithValue("@Data", Convert.ToDateTime(strDate))


                    SQLcmd.ExecuteNonQuery()
                End Using


            End Using

1 Answer 1

1

Don't try to convert the string back to a date, use directly the date

SQLcmd.Parameters.AddWithValue("@Data", DateTime.Now)

When using parameters, it is the framework code that presents the values to your database engine in a way that could understood as a date, number or string.

Your error is trying to convert a date in a string using a format that probably is not the default format recognized on your machine as a valid datetime.
Now, when you try to reconvert the string back to a date you get the error.
You could use something like this to remove the error

Dim t As DateTime = Convert.ToDateTime(strDate, CultureInfo.InvariantCulture)

but there is no reason to make all these conversions

2
  • Can you help me with something else ? It's basically the same thing , I get an error : Unknown Column Nume in 'field list' Basically I am just trying to add some values to my table users1 My DB : prntscr.com/39on8i vb code : pastebin.com/CcBFiUAN Apr 13, 2014 at 19:00
  • Usually this kind of error is caused by a mistyped field name in the command text, but from your links it seems all correct.
    – Steve
    Apr 13, 2014 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.