Reputation: 824
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
Upvotes: 1
Views: 212
Reputation: 216353
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
Upvotes: 1