Results 1 to 9 of 9

Thread: data type mismatch in criteria expression

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    data type mismatch in criteria expression

    This error is perplexing as I cannot make it happen on my win7 64bit. But when I compile and send to my mom she gets it every time. I have tried 4 different boxes ranging from xp to win7 and none get this error. Yet when she runs it (win7 64bit) she gets it every time

    Below is the save function. I have tried to do

    SQLInsert.Parameters.AddWithValue("@ID", CInt(Label3.Text))

    and it errors with string to integer error. It will always be a number so I want integer. And it is set to number in the access database. I believe that is where it is error but I don't know. Is there a way to get the line number when the error shows to user?

    Code:
     Private SQLInsert As New OleDbCommand("INSERT INTO Recipe (Routine, Description, Snippet,ParentID) VALUES (@Name, @Descr, @Snip,@ID)", Me.connection)
    
    Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
    
            Try
                If Len(txRoutine.Text) < 1 Then
                    MsgBox("Please enter recipe name.", MsgBoxStyle.OkOnly, "Validation error")
                    txRoutine.Focus()
                    Exit Sub
                End If
    
                If Len(RichTextBox1.Text) < 1 Then
                    MsgBox("Please enter Ingredients.", MsgBoxStyle.OkOnly, "Validation error")
                    RichTextBox1.Focus()
                    Exit Sub
                End If
    
                If Len(RichTextBox2.Text) < 1 Then
                    MsgBox("Please enter Process.", MsgBoxStyle.OkOnly, "Validation error")
                    RichTextBox2.Focus()
                    Exit Sub
                End If
                ' checks to see if name is already taken
                If Len(txRoutine.Text) > 100 Then
                    MsgBox("Title of recipe is too long", MsgBoxStyle.OkOnly, "Validation error")
                    txRoutine.Focus()
                    Exit Sub
                End If
                ' check to see if we are adding a new one or updating a record
                If Not tvEdit Then
                    ' MsgBox("we are adding")
    
                    SQLInsert.Parameters.AddWithValue("@Name", CStr(Replace(txRoutine.Text, "'", "`")))
                    SQLInsert.Parameters.AddWithValue("@Descr", CStr(Replace(RichTextBox1.Text, "'", "`")))
                    SQLInsert.Parameters.AddWithValue("@Snip", CStr(Replace(RichTextBox2.Text, "'", "`")))
                    SQLInsert.Parameters.AddWithValue("@ID", Label3.Text)
                    'connection.Open()
                    SQLInsert.ExecuteNonQuery()
                    Me.adapter.InsertCommand = SQLInsert
                    Dim row = table.NewRow()
                    row("Routine") = CStr(Replace(txRoutine.Text, "'", "`"))
                    row("Description") = CStr(Replace(RichTextBox1.Text, "'", "`"))
                    row("Snippet") = CStr(Replace(RichTextBox2.Text, "'", "`"))
                    row("ParentID") = Label3.Text
                    table.Rows.Add(row)
                    ' add to listview
                Else
    
                    ' MsgBox("we are edting")
                    SQLupdate.Parameters.AddWithValue("@Name", CStr(Replace(txRoutine.Text, "'", "`")))
                    SQLupdate.Parameters.AddWithValue("@Descr", CStr(Replace(RichTextBox1.Text, "'", "`")))
                    SQLupdate.Parameters.AddWithValue("@Snip", CStr(Replace(RichTextBox2.Text, "'", "`")))
                    SQLupdate.Parameters.AddWithValue("@ID", LblCounter.Text)
                    'connection.Open()
                    SQLupdate.ExecuteNonQuery()
                    Me.adapter.UpdateCommand = SQLupdate
                    Dim row = table.NewRow()
                    row("Routine") = CStr(Replace(txRoutine.Text, "'", "`"))
                    row("Description") = CStr(Replace(RichTextBox1.Text, "'", "`"))
                    row("Snippet") = CStr(Replace(RichTextBox2.Text, "'", "`"))
                    row("ParentID") = LblCounter.Text
                    table.Rows.Add(row)
                    ' Me.adapter.Update(Me.table)
    
                End If
                ' End Using
                MsgBox("Recipe: " & txRoutine.Text & vbCrLf & " Has been saved successfully")
                BtnDegree.Visible = False
                tvwMain.Show()
                txRoutine.Visible = False
                BtnAbort.Visible = False
                BtnSave.Visible = False
                LblRecipeName.Visible = False
                RichTextBox1.ReadOnly = True
                RichTextBox2.ReadOnly = True
                ' reload treeview
    
                RefreshTree()
            Catch ex As Exception
                MsgBox("Failed to Save Recipe" & Chr(13) & Chr(13) & ex.Message, vbOKOnly, "Failed To Save")
                Debug.WriteLine(ex.Message)
            End Try
        End Sub

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

    Re: data type mismatch in criteria expression

    When you use AddWithValue, the data type of the parameter is inferred from the value. You are using Strings in all cases. Presumably one of your columns does not contain text, hence the data type mismatch. Data types are NOT interchangeable. If something is a number then use a number, not a String containing numeric characters. Etc.
    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

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: data type mismatch in criteria expression

    What are the datatypes of those fields in your database? And what database is it?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: data type mismatch in criteria expression

    @phpman

    The ID field in the access database is expecting a number. You can use the Integer.Parse Function.

    vb.net Code:
    1. Dim labelvalue As String =label3.Text
    2. Dim value As Integer = Integer.Parse(label3.Text)

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

    Re: data type mismatch in criteria expression

    Quote Originally Posted by Ram2Curious View Post
    @phpman

    The ID field in the access database is expecting a number. You can use the Integer.Parse Function.

    vb.net Code:
    1. Dim labelvalue As String =label3.Text
    2. Dim value As Integer = Integer.Parse(label3.Text)
    Why would you need to parse the contents of a Label? That Label must have been populated using a number in the first place. It's that number that should be being used.
    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
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: data type mismatch in criteria expression

    Quote Originally Posted by jmcilhinney View Post
    Why would you need to parse the contents of a Label? That Label must have been populated using a number in the first place. It's that number that should be being used.

    Hi jm, i have a doubt here. If i try to insert the digit " 1 " using a textbox or a label, then is it going to be inserted as a string datatype or an integer ? Why would someone have to use the CInt Function to convert a value to integer ?

    .

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

    Re: data type mismatch in criteria expression

    The Text of a control is a String, plain and simple. It doesn't matter what characters it contains, it's a String. If your database is supposed to store a number then you need to pass a number. A String containing digit is still a String, not a number.
    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

  8. #8
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: data type mismatch in criteria expression

    Jm, does it mean that an Integer or a Decimal value is still a string ? Is it just a string representation ?

    Edit : If it is so, then when should the CInt or CDbl be used ?
    Last edited by Ram2Curious; Sep 22nd, 2011 at 05:51 AM.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: data type mismatch in criteria expression

    Thanks guys.

    My database is access database. I have the correct structure in the database. The @ID field is a number in the database. But I think I know what is happening. I will find out later today. It maybe a user problem on how they are entering it.

    So I should be using CInt() for that label anyway huh?

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