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 ?

Recommended Answers

All 9 Replies

Hmm lemme try the code.

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

Dosn't work :S

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

Send me the database ! please

Nothing is written in the database and No Msgbox comes out

Can you attach your database so I can test it with that myself?

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.

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

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.