i have an error in a Button click :

  Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\amine\Documents\projet.mdb"
        Dim Conn As New OleDbConnection(ConStr)
        Dim Save As New OleDbCommand("INSERT INTO Client(Nom,Prenom,Adresse,Téléphone,[date]) VALUES(@p1,@p2,@p3,@p4,@p5)", Conn)
        Try

            Save.Parameters.AddWithValue("@p1", TextBox1.Text)
            Save.Parameters.AddWithValue("@p2", TextBox2.Text)
            Save.Parameters.AddWithValue("@p3", TextBox3.Text)
            Save.Parameters.AddWithValue("@p4", TextBox4.Text)
            Save.Parameters.AddWithValue("@p5", DateTimePicker1.Text)

            Conn.Open()
            If (Save.ExecuteNonQuery()) Then
                MsgBox("Ajout réussi ")
            Else
                MsgBox("Error iam here")
            End If
        Catch ex As Exception
            Exit Sub
        End Try

How can i correct this ?

Hmm lemme try the code.

Well, I don't encounter any error. It writes to the database just fine. What error are you encountering?

Edited 4 Years Ago by scudzilla

You mean to say, nothing is written in the database, or a msgbox comes up saying "Error iam here"?

Nothing is written in the database and No Msgbox comes out

I can narrow the problem to the date field in your database. It seems that it won't accept the datetimepicker.value, therefore the error. If you change the date field format to text, your code will work. I'm still looking into why it won't accept date values.

EDIT: Well, it works when you use Save.Parameters.AddWithValue("@p5", DateTimePicker1.Value.ToShortDateString)
Additionally, the database you posted has the fourth field named as "gsm" instead of "Téléphone" so change the database field or the code accordingly.

Edited 4 Years Ago by scudzilla

I'd try looking at the date format, I always try to pass date values into my databases as YYYY-MM-DD (2012-05-03) or DD-MMM-YYYY (03-May-2012) formats to avoid confusion with the U.S. (wrong) date format and the civilised one ;-) i.e. mm/dd/yyyy (05/03/2012) vs dd/mm/yyyy (03/05/2012)

dim formatedDate as string 

....
formatedDate = Year(DateTimePicker1.Value) &"-" &Format(Month(DateTimePicker1.Value),"0#") &"-" &format(Day(DateTimePicker1.Value),"0#")
....

Save.Parameters.AddWithValue("@p5", formatedDate)
....

Also you are using DateTimePicker1.Text. You should use DateTimePicker1.Value

Edited 4 Years Ago by G_Waddell: code example

This article has been dead for over six months. Start a new discussion instead.