Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All,

This is the code i am using to write the data to Access Table An data Mismatch error has been occured in runtime.

Any one plzzzzzzzzz Help me.......................

SQL
cmdInsert.CommandText = "INSERT INTO SMSDATA1DB (Start_Of_Packet, Packet_Identifier,Soft_Version,Site_ID,Date_Time,Alarm_String,Solar_Voltage,Solar_Current,Solar_Avg_PF,Solar_Inst_Energy,Solar_Cum_Energy,Load_Voltage,Load_Current,Load_Avg_PF,Load_Inst_Energy,Load_Cumm_Energy,Grid_Voltage,Grid_Current,Grid_Inst_Energy,Grid_Cum_Energy,Batt_Voltage,Batt_Charg_Current,Batt_Charg_Energy,Batt_Disch_Current,Batt_Disch_Energy,Status_String,End_Of_String) VALUES ('" & STX1 & "', '" & STX2 & "', '" & STX3 & "', '" & STX4 & "', '" & STX5 & "' , '" & result1 & "', '" & STX7 & "', '" & STX8 & "', '" & STX9 & "', '" & STX25 & "', '" & STX10 & "', '" & STX11 & "', '" & STX12 & "', '" & STX13 & "', '" & STX26 & "', '" & STX14 & "', '" & STX15 & "', '" & STX16 & "', '" & STX27 & "', '" & STX17 & "', '" & STX18 & "', '" & STX19 & "', '" & STX20 & "', '" & STX21 & "', '" & STX22 & "', '" & result2 & "', '" & STX24 & "')"
            MsgBox(cmdInsert.CommandText)
            cmdInsert.CommandType = CommandType.Text
            cmdInsert.Connection = cnnOLEDB
            cmdInsert.ExecuteNonQuery()
            cmdInsert.Dispose()


The Date&Time Function is Date_Time and the value of that is stored in STX5 string
Posted
Updated 2-Dec-11 19:53pm
v2
Comments
NikulDarji 3-Dec-11 2:29am    
Check the datatype of access database set it "Text"....

For goodness sake! There are so many things wrong here, I'll just go for the big ones:

1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead
2) Do not use such similar names for your text fields. You said it yourself: "The Date&Time Function is Date_Time and the value of that is stored in STX5 string" - You have to tell us, because the names do not help anyone. If you used STXDateTime or similar, we would not need to be told - and in two weeks time, you wouldn't have to look through your code to work it out either, because you will have forgotten. Purely for maintenance, and readability, and reliability, change to sensible names!

The first one will probably help to solve your problem: the string form of the Date and time is almost certianbly in your local format, rather than ISO which the database is expecting. If you feed it 13/01/2009 14:59 and it expects yyyy-MM-dd hh:mm:ss then it will cause a data mismatch. Keep your dates and times in DateTime objects instead of strings, and pass them (and everything else) through as parameters - your code will be more readable, and your problem will go away.
VB
cmdInsert.CommandText = "INSERT INTO SMSDATA1DB (Start_Of_Packet, Packet_Identifier,Soft_Version,...) VALUES (@SOP, @PI, @SV,...)"
cmdInsert.Parameters.AddWithValue("@SOP", STX1)
cmdInsert.Parameters.AddWithValue("@PI", STX2)
cmdInsert.Parameters.AddWithValue("@SV, STX3)
...
 
Share this answer
 
Use the Format fuction in your SQL and set it to your local format:
VB
"SELECT... ,FORMAT(STX5,'DD-MM-YYYY'), ..."
 
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