This problem, 'Data type mismatch in criteria expression.', has been giving me problems

DeBoss241 0 Reputation points
2023-05-11T00:22:11.49+00:00

This is my code and I'm not sure what's wrong

Dim strsql As String

    strsql = "Insert into Tbltenants (id,housenumber,lastname,firstname,phonenumber,email,term,payment_by,rent_paid) VALUES(@id,@housenumber,@lastname,@firstname,@phonenumber,@email,@term,@payment_by,@rent_paid)"

    Dim cmd2 As New OleDbCommand(strsql, conn)

    cmd2.Parameters.AddWithValue("@id", IDTextBox.Text)

    cmd2.Parameters.AddWithValue("@house number", House_NumberTextBox.Text)

    cmd2.Parameters.AddWithValue("@last name", Last_NameTextBox.Text)

    cmd2.Parameters.AddWithValue("@first name", First_NameTextBox.Text)

    cmd2.Parameters.AddWithValue("@phone number", Phone_NumberTextBox.Text)

    cmd2.Parameters.AddWithValue("@email", EmailTextBox.Text)

    cmd2.Parameters.AddWithValue("@term", TermTextBox.Text)

    cmd2.Parameters.AddWithValue("@payment by", Payment_byTextBox.Text)

    cmd2.Parameters.AddWithValue("@rent paid", Rent_paidTextBox.Text)

    conn.Open()

    cmd2.ExecuteNonQuery()

    conn.Close()

    bind_data()

Please help me.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,892 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
Access
Access
A family of Microsoft relational database management systems designed for ease of use.
390 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,724 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 31,321 Reputation points Microsoft Vendor
    2023-05-11T01:32:04.2066667+00:00

    Hi, Welcome to Microsoft Q&A.

    A possible cause of this problem is that the fields in the database do not match the data types of the parameters passed to the query.

    Please make sure that the data type of each parameter matches the data type of the target column.

    For example, to convert a string to a numeric format in an Access database, you can use conversion functions such as Convert.ToByte, Convert.ToInt16, Convert.ToInt32, Convert.ToInt64, Convert.ToSingle, Convert.ToDouble, Convert.ToDecimal, and so on.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. satya karki 986 Reputation points MVP
    2023-05-11T02:22:01.9+00:00

    Hi, please check the datatype of each column of the table and the value of the parameter provided. Seems like provided parameters datatype and column datatype are not matching.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.