Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
m getting this error "data type mismatch in criteria expression" in the line cd=oledbcom.executeNonQuery() in insert section

herz the code:
VB
Imports System.Data.OleDb


Public Class Form1

    Dim oledbconn As OleDbConnection
    Dim oledbcom As OleDbCommand
    Dim oledbreader As OleDbDataReader
    Dim connectionString As String
    Dim command As String
    Dim cd As Integer






   

    Private Sub open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles open.Click
        

        connectionString = "provider= Microsoft.Jet.OLEDB.4.0;Data source=Student.mdb"

        oledbconn = New OleDbConnection(connectionString)
        MsgBox("database connected")
        
       
    End Sub

    Private Sub fillreader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fillreader.Click
        
        connectionString = "provider= Microsoft.Jet.OLEDB.4.0;Data source=Student.mdb"


        command = "select * from personinfo"
        oledbcom = New OleDbCommand(command, oledbconn)
        Try
            oledbreader = oledbcom.ExecuteReader()
            If (oledbreader.Read()) Then
                Txtroll.Text = oledbreader(0)
                Txtname.Text = oledbreader(1)
                txtemail.Text = oledbreader(2)
                Txtphone.Text = oledbreader(3)
                Txtpercentage.Text = oledbreader(4)
            End If
        Catch ex As Exception

        End Try
        


    End Sub

    Private Sub close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles close.Click
       

        oledbconn.Close()
        MsgBox("database disconnected")



    End Sub

    Private Sub insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles insert.Click
        
        connectionString = "provider= Microsoft.Jet.OLEDB.4.0;Data source=Student.mdb"

        oledbconn.Open()

        command = "insert into personinfo values('&Txtroll.text&','&Txtname.text&','&Txtemail.text&','&Txtphone.text&','&cint(Txtpercentage.text)&')"
        oledbcom = New OleDbCommand(command, oledbconn)
        cd = oledbcom.ExecuteNonQuery()
        MsgBox("data is entered in database")



        


    End Sub

    Private Sub Txtroll_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtroll.TextChanged

    End Sub

    Private Sub Txtname_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtname.TextChanged

    End Sub
End Class
Posted
Updated 17-Jan-13 21:57pm
v3

VB
command = "insert into personinfo values('&Txtroll.text&','&Txtname.text&','&Txtemail.text&','&Txtphone.text&','&cint(Txtpercentage.text)&')"


This is incorrect in several ways :

1- You should clearly state the name of the columns you are about to update in your insert query.
Thus :

VB
command = "insert into personinfo (columnName1, columnName2, ...) values('&Txtroll.text&','&Txtname.text&','&Txtemail.text&','&Txtphone.text&','&cint(Txtpercentage.text)&')"


2- Do not ever construct a SQL query by concatenating strings. Better use parameterized query.
Thus :

VB
command = "insert into personinfo (columnName1, columnName2, ...) values(@roll,@name,@email,@phone,@percentage)"
command.Parameters.AddWithValue("@roll", Txtrool.Text)
command.Parameters.AddWithValue("@name", Txtname.Text)


and so on...

Hope this helps.
 
Share this answer
 
&','&cint(Txtpercentage.text)&')"

You are converting text to integer but not removing the inverted quotes.
Get rid of them in the query - you won't even need the Cint.

E.g. &',&Txtpercentage.text&)"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900