Results 1 to 6 of 6

Thread: [2008] Data type mismatch in criteria expression. URGENT

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    [2008] Data type mismatch in criteria expression. URGENT

    Alright I fixed my last problem but now I'm getting this error only when Editing:

    Data type mismatch in criteria expression.

    Here's my Code:

    Table Create:
    vb Code:
    1. Public Sub CreateDatabase()
    2.         Dim DB As New ADOX.Catalog()
    3.         DB.Create(Connection.ConnectionString)
    4.         Connection.Open()
    5.         SQL = "CREATE TABLE [Students] ([CIndex] AutoIncrement, [StudentID] TEXT, [LastName] TEXT, [FirstName] TEXT, [Grade] TEXT, [Gender] TEXT, [BirthDate] TEXT, [PhoneNum] TEXT, [AddressOne] TEXT, [AddressTwo] TEXT, [EmailOne] TEXT, [EmailTwo] TEXT, [Notes] TEXT)"
    6.         Command = New OleDbCommand(SQL, Connection)
    7.         Command.ExecuteNonQuery()
    8.     End Sub


    Student Edit:
    vb Code:
    1. Public Sub Students_Edit()
    2.         Students_SQLSelect()
    3.         Adapter.UpdateCommand = New OleDbCommand("UPDATE [Students] SET StudentID = @ID, LastName = @LN, FirstName = @FN, Gender = @Gen, BirthDate = @Birth, [Phone#] = @Phone, Address1 = @AD1, Address2 = @AD2, Email1 = @E1, Email2 = @E2, Notes = @Notes WHERE CIndex = @CIndex")
    4.         Adapter.UpdateCommand.Connection = Connection
    5.         Connection.Open()
    6.         With Adapter.UpdateCommand
    7.             .Parameters.Add("@ID", OleDbType.VarChar, 10, "StudentID")
    8.             .Parameters.Add("@LN", OleDbType.VarChar, 75, "LastName")
    9.             .Parameters.Add("@FN", OleDbType.VarChar, 75, "FirstName")
    10.             .Parameters.Add("@Grade", OleDbType.VarChar, 2, "Grade")
    11.             .Parameters.Add("@Gen", OleDbType.VarChar, 6, "Gender")
    12.             .Parameters.Add("@Birth", OleDbType.VarChar, 100, "BirthDate")
    13.             .Parameters.Add("@Phone", OleDbType.VarChar, 25, "[Phone#]")
    14.             .Parameters.Add("@AD1", OleDbType.VarChar, 100, "Address1")
    15.             .Parameters.Add("@AD2", OleDbType.VarChar, 100, "Address2")
    16.             .Parameters.Add("@E1", OleDbType.VarChar, 100, "Email1")
    17.             .Parameters.Add("@E2", OleDbType.VarChar, 100, "Email2")
    18.             .Parameters.Add("@Notes", OleDbType.VarChar, 500, "Notes")
    19.             .Parameters.Add("@CIndex", OleDbType.Integer, 5, "CIndex")
    20.         End With
    21.         With frmStudentsAdd
    22.             Adapter.Fill(DS)
    23.             Dim DRow As DataRow
    24.             DRow = DS.Tables(0).Rows(frmStudents.RIndex)
    25.             MsgBox(DRow.Item("StudentID"))
    26.             DRow.BeginEdit()
    27.             DRow("StudentID") = .txtStudentID.Text
    28.             DRow("LastName") = .txtLastName.Text
    29.             DRow("FirstName") = .txtFirstName.Text
    30.             DRow("Grade") = .txtGrade.Text
    31.             If .rbMale.Checked Then
    32.                 DRow.Item("Gender") = "Male"
    33.             ElseIf .rbFemale.Checked Then
    34.                 DRow.Item("Gender") = "Female"
    35.             End If
    36.             DRow("BirthDate") = .dtpBirth.Value.ToShortDateString
    37.             DRow("Phone#") = .txtPhone.Text
    38.             DRow("Address1") = .txtAddressOne.Text
    39.             DRow("Address2") = .txtAddressTwo.Text
    40.             DRow("Email1") = .txtEmailOne.Text
    41.             DRow("Email2") = .txtEmailTwo.Text
    42.             DRow("Notes") = .txtNotes.Text
    43.             DRow.EndEdit()
    44.             Adapter.Update(DS)
    45.             DS.AcceptChanges()
    46.         End With
    47.         Connection.Close()
    48.         Students_LoadToList()
    49.     End Sub

    It inserts fine when adding but the moment I try to Edit it, I get that error.

    EDIT:

    Hmm, I seem to have fixed it, by adding text to all the textboxes. How can I do this without having to add to add textboxes, and When I have Data in all the Textboxes, Everything from Column Genedr is Shifted Over 1.
    Last edited by Wesley008; Nov 9th, 2008 at 03:29 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Data type mismatch in criteria expression. URGENT

    Anyone at all?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Data type mismatch in criteria expression. URGENT

    its because your database fields can't accept a null value

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Data type mismatch in criteria expression. URGENT

    Is their a way to like Check if the Value is NULL and if so, not add to it? Or do I need to do repeated IF statements

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Data type mismatch in criteria expression. URGENT

    I can tell you one thing for a start. BirthDate should absolutely NOT be a Text column. It contains dates so it should be a Date column, or DateTime or whatever. Do NOT store data that is not naturally text in text fields.

    As for your problem, you've added a @Grade parameter to your command but there isn't one in your SQL code. The Jet OLEDB provider uses position, rather than name, to substitute parameter values into the SQL code. As such, the value of your @Grade parameter is getting inserted into your Gender column, etc. You got the data type mismatch because the value of your @Notes parameter, which is text, was being compared to your CIndex column, which is numeric.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Data type mismatch in criteria expression. URGENT

    Thank You Both, I feel kinda stupid for not seeing I was missing Gender.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width