Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying the insert into statement i searched online , no errors found but when i run it

((((Data type mismatch in criteria expression.)))))

'this error appears
what may the cause be?

What I have tried:

VB
'this is what i have tried

Dim sql As String = "INSERT INTO Elementary VALUES(@SchoolName, @SchoodID, @KM, @KF,@KT,@G1M,@G1F,@G1T,@G2M,@G2F,@G2T,@G3M,@G3F,@G3T,@G4M,@G4F,@G4T,@G5M,@G5F,@G5T,@G6M,@G6F,@G6T,@TM,@TF,@GT,@SY,@District,@Field2)"
        Using conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\plan.mdb")
            Using sqlCom = New System.Data.OleDb.OleDbCommand(sql, conn)
                conn.Open()
                sqlCom.Parameters.Add("@name", OleDbType.VarWChar).Value = SchoolNameTextBox.Text
                sqlCom.Parameters.Add("@pass", OleDbType.VarWChar).Value = SchoodIDTextBox.Text
                sqlCom.Parameters.Add("@sec", OleDbType.VarWChar).Value = KMTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = KFTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = KTTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G1MTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G1FTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G1TTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G2MTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G2FTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G2TTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G3MTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G3FTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G3TTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G4MTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G4FTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G4TTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G5MTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G5FTextBox.Text
                 sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G5TTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G6MTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G6FTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G6TTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = TMTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = TFTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = GTTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = SYTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = DistrictTextBox.Text
                sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = Field2TextBox.Text

                Dim icount As Integer = sqlCom.ExecuteNonQuery
            End Using
        End Using
Posted
Updated 9-Aug-20 20:12pm
v3

Start by explicitly listing the columns the parameters should go into:
SQL
INSERT INTO MyTable (Column1, Column2) VALUES (@C1, @C2)
If you don't, then SQL doesn't know exactly what you want to do, so it inserts values in "current table order". So if you have an IDENTITY ID column at the front (and you probably should) it will try to write to that first for example.
You then need to provide the parameters with matching names:
VB
sqlCom.Parameters.Add("@SchoolName", OleDbType.VarWChar).Value = SchoolNameTextBox.Text
sqlCom.Parameters.Add("@SchoodID", OleDbType.VarWChar).Value = SchoodIDTextBox.Text
...

And your ID column - unless that is a Guid / UNIQUEIDENTIFIER column you should not be passing it at all to SQL: SQL Server is a multiuser database system, and prcisifying ID values in your presentation software is a very bad idea - it leads to two or more users specifying the same value and then data gets hopelessly corrupted. Use an IDENTITY column (or a UNIQUEIDENTIFIER) and let SQL sort out the value.
 
Share this answer
 
Comments
Beginner213456 10-Aug-20 2:31am    
Dim sql As String = "INSERT INTO Elementary (SchoolName,SchoodID,KM,KF,KT,G1M,G1F,G1T,G2M,G2F,G2T,G3M,G3F,G3T,G4M,G4F,G4T,G5M,G5F,G5T,G6M,G6F,G6T,TM,TF,GT,SY,District,Field) VALUES(@SchoolName, @SchoodID, @KM, @KF,@KT,@G1M,@G1F,@G1T,@G2M,@G2F,@G2T,@G3M,@G3F,@G3T,@G4M,@G4F,@G4T,@G5M,@G5F,@G5T,@G6M,@G6F,@G6T,@TM,@TF,@GT,@SY,@District,@Field2)"

is this correct?
OriginalGriff 10-Aug-20 3:05am    
It looks better, but I don't have access to your DB to check the column names.
A quick check with my text editor says they match up though:
SchoolName     @SchoolName   
SchoodID       @SchoodID    
KM             @KM          
KF             @KF          
KT             @KT           
G1M            @G1M          
G1F            @G1F          
G1T            @G1T          
G2M            @G2M          
G2F            @G2F          
G2T            @G2T          
G3M            @G3M          
G3F            @G3F          
G3T            @G3T          
G4M            @G4M          
G4F            @G4F          
G4T            @G4T          
G5M            @G5M          
G5F            @G5F          
G5T            @G5T          
G6M            @G6M          
G6F            @G6F          
G6T            @G6T          
TM             @TM           
TF             @TF           
GT             @GT           
SY             @SY           
District       @District     
Field          @Field2
Beginner213456 10-Aug-20 3:19am    
Those are the only columns on my table database

or perhaps . there is something missing on my code?
Beginner213456 10-Aug-20 3:24am    
Those are the only columns on my table database

or perhaps . there is something missing on my code?
or should i change my column properties to short text? because i used number
OriginalGriff 10-Aug-20 3:33am    
If you have used number columns in the DB, then you have to pass actual numbers, not textbox content: use int.TryParse, double.TryParse, and so on as appropriate for the number column type to check and convert the textbox contents first, then only try to save to the DB if they all validate.
Never try to send unvalidated and unconverted user input to your DB, unless it's going to a text field. Once "bad data" gets into your DB, it's much, much harder to fix it!
Quote:
((((Data type mismatch in criteria expression.)))))

No idea since I don't see any 'criteria' in this code.
But I have strong feeling that when some parameters appear in a query,
VB
Dim sql As String = "INSERT INTO Elementary VALUES(@SchoolName, @SchoodID, @KM, KF, @KT, @G1M, @G1F, @G1T, @G2M, @G2F, @G2T, @G3M, @G3F, @G3T, @G4M, @G4F, @G4T, @G5M, @G5F, @G5T, @G6M, @G6F, @G6T, @TM, @TF, @GT, @SY, @District, @Field2)"

the parameters definition must match in names and position.
VB
sqlCom.Parameters.Add("@name", OleDbType.VarWChar).Value = SchoolNameTextBox.Text
sqlCom.Parameters.Add("@pass", OleDbType.VarWChar).Value = SchoodIDTextBox.Text
sqlCom.Parameters.Add("@sec", OleDbType.VarWChar).Value = KMTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = KFTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = KTTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G1MTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G1FTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G1TTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G2MTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G2FTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G2TTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G3MTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G3FTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G3TTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G4MTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G4FTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G4TTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G5MTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G5FTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G5TTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G6MTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G6FTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = G6TTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = TMTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = TFTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = GTTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = SYTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = DistrictTextBox.Text
sqlCom.Parameters.Add("@sw", OleDbType.VarWChar).Value = Field2TextBox.Text

Defining parameters with same name is certainly not the way to go.
 
Share this answer
 
Comments
Beginner213456 10-Aug-20 1:54am    
I have checked and the parameters position and names are correct
Patrice T 10-Aug-20 2:01am    
Do you mean that naming parameters "@sw" 26 times feel correct to you ?
Beginner213456 10-Aug-20 2:24am    
sqlCom.Parameters.Add("@SchoolName", OleDbType.VarWChar).Value = SchoolNameTextBox.Text
sqlCom.Parameters.Add("@SchoodID", OleDbType.VarWChar).Value = SchoodIDTextBox.Text
sqlCom.Parameters.Add("@KM", OleDbType.VarWChar).Value = KMTextBox.Text
sqlCom.Parameters.Add("@KF", OleDbType.VarWChar).Value = KFTextBox.Text
sqlCom.Parameters.Add("@KT", OleDbType.VarWChar).Value = KTTextBox.Text
sqlCom.Parameters.Add("@G1M", OleDbType.VarWChar).Value = G1MTextBox.Text
sqlCom.Parameters.Add("@G1F", OleDbType.VarWChar).Value = G1FTextBox.Text
sqlCom.Parameters.Add("@G1T", OleDbType.VarWChar).Value = G1TTextBox.Text
sqlCom.Parameters.Add("@G2M", OleDbType.VarWChar).Value = G2MTextBox.Text
sqlCom.Parameters.Add("@G2F", OleDbType.VarWChar).Value = G2FTextBox.Text
sqlCom.Parameters.Add("@G2T", OleDbType.VarWChar).Value = G2TTextBox.Text
sqlCom.Parameters.Add("@G3M", OleDbType.VarWChar).Value = G3MTextBox.Text
sqlCom.Parameters.Add("@G3F", OleDbType.VarWChar).Value = G3FTextBox.Text
sqlCom.Parameters.Add("@G3T", OleDbType.VarWChar).Value = G3TTextBox.Text
sqlCom.Parameters.Add("@G4M", OleDbType.VarWChar).Value = G4MTextBox.Text
sqlCom.Parameters.Add("@G4F", OleDbType.VarWChar).Value = G4FTextBox.Text
sqlCom.Parameters.Add("@G4T", OleDbType.VarWChar).Value = G4TTextBox.Text
sqlCom.Parameters.Add("@G5M", OleDbType.VarWChar).Value = G5MTextBox.Text
sqlCom.Parameters.Add("@G5F", OleDbType.VarWChar).Value = G5FTextBox.Text
sqlCom.Parameters.Add("@G5T", OleDbType.VarWChar).Value = G5TTextBox.Text
sqlCom.Parameters.Add("@G6M", OleDbType.VarWChar).Value = G6MTextBox.Text
sqlCom.Parameters.Add("@G6F", OleDbType.VarWChar).Value = G6FTextBox.Text
sqlCom.Parameters.Add("@G6T", OleDbType.VarWChar).Value = G6TTextBox.Text
sqlCom.Parameters.Add("@TM", OleDbType.VarWChar).Value = TMTextBox.Text
sqlCom.Parameters.Add("@TF", OleDbType.VarWChar).Value = TFTextBox.Text
sqlCom.Parameters.Add("@GT", OleDbType.VarWChar).Value = GTTextBox.Text
sqlCom.Parameters.Add("@SY", OleDbType.VarWChar).Value = SYTextBox.Text
sqlCom.Parameters.Add("@District", OleDbType.VarWChar).Value = DistrictTextBox.Text
sqlCom.Parameters.Add("@Field2", OleDbType.VarWChar).Value = Field2TextBox.Text


i changed it to that but the error is still the same
Patrice T 10-Aug-20 2:34am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Can you give exact error message ?
The problem is that insert do not have a criteria clause.
Beginner213456 10-Aug-20 2:54am    
'Data type mismatch in criteria expression.
'that is the error message
' i have changed my code to


Dim sql As String = "INSERT INTO Elementary (SchoolName,SchoodID,KM,KF,KT,G1M,G1F,G1T,G2M,G2F,G2T,G3M,G3F,G3T,G4M,G4F,G4T,G5M,G5F,G5T,G6M,G6F,G6T,TM,TF,GT,SY,District,Field2) VALUES(@SchoolName, @SchoodID, @KM, @KF,@KT,@G1M,@G1F,@G1T,@G2M,@G2F,@G2T,@G3M,@G3F,@G3T,@G4M,@G4F,@G4T,@G5M,@G5F,@G5T,@G6M,@G6F,@G6T,@TM,@TF,@GT,@SY,@District,@Field2)"
Using conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\plan.mdb")
Using sqlCom = New System.Data.OleDb.OleDbCommand(sql, conn)
conn.Open()
sqlCom.Parameters.Add("@SchoolName", OleDbType.VarWChar).Value = SchoolNameTextBox.Text
sqlCom.Parameters.Add("@SchoodID", OleDbType.VarWChar).Value = SchoodIDTextBox.Text
sqlCom.Parameters.Add("@KM", OleDbType.VarWChar).Value = KMTextBox.Text
sqlCom.Parameters.Add("@KF", OleDbType.VarWChar).Value = KFTextBox.Text
sqlCom.Parameters.Add("@KT", OleDbType.VarWChar).Value = KTTextBox.Text
sqlCom.Parameters.Add("@G1M", OleDbType.VarWChar).Value = G1MTextBox.Text
sqlCom.Parameters.Add("@G1F", OleDbType.VarWChar).Value = G1FTextBox.Text
sqlCom.Parameters.Add("@G1T", OleDbType.VarWChar).Value = G1TTextBox.Text
sqlCom.Parameters.Add("@G2M", OleDbType.VarWChar).Value = G2MTextBox.Text
sqlCom.Parameters.Add("@G2F", OleDbType.VarWChar).Value = G2FTextBox.Text
sqlCom.Parameters.Add("@G2T", OleDbType.VarWChar).Value = G2TTextBox.Text
sqlCom.Parameters.Add("@G3M", OleDbType.VarWChar).Value = G3MTextBox.Text
sqlCom.Parameters.Add("@G3F", OleDbType.VarWChar).Value = G3FTextBox.Text
sqlCom.Parameters.Add("@G3T", OleDbType.VarWChar).Value = G3TTextBox.Text
sqlCom.Parameters.Add("@G4M", OleDbType.VarWChar).Value = G4MTextBox.Text
sqlCom.Parameters.Add("@G4F", OleDbType.VarWChar).Value = G4FTextBox.Text
sqlCom.Parameters.Add("@G4T", OleDbType.VarWChar).Value = G4TTextBox.Text
sqlCom.Parameters.Add("@G5M", OleDbType.VarWChar).Value = G5MTextBox.Text
sqlCom.Parameters.Add("@G5F", OleDbType.VarWChar).Value = G5FTextBox.Text
sqlCom.Parameters.Add("@G5T", OleDbType.VarWChar).Value = G5TTextBox.Text
sqlCom.Parameters.Add("@G6M", OleDbType.VarWChar).Value = G6MTextBox.Text
sqlCom.Parameters.Add("@G6F", OleDbType.VarWChar).Value = G6FTextBox.Text
sqlCom.Parameters.Add("@G6T", OleDbType.VarWChar).Value = G6TTextBox.Text
sqlCom.Parameters.Add("@TM", OleDbType.VarWChar).Value = TMTextBox.Text
sqlCom.Parameters.Add("@TF", OleDbType.VarWChar).Value = TFTextBox.Text
sqlCom.Parameters.Add("@GT", OleDbType.VarWChar).Value = GTTextBox.Text
sqlCom.Parameters.Add("@SY", OleDbType.VarWChar).Value = SYTextBox.Text
sqlCom.Parameters.Add("@District", OleDbType.VarWChar).Value = DistrictTextBox.Text
sqlCom.Parameters.Add("@Field2", OleDbType.VarWChar).Value = Field2TextBox.Text


Dim icount As Integer = sqlCom.ExecuteNonQuery
End Using
End Using

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