views:

338

answers:

2

I have one porject in VB.NET with Ms Access As Backend...While inserting data into table there is no error at query and Data Type Error,but sometime OleDB Exception occurs Error is"Data type mismatch in criteria expression." the query is ....

Dim cmdstr1 As String = ""
        Dim constr As String = ""
          Dim sqlQuery As String = ""
        constr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\PhoneDiary\MyDiary.mdb"

 cmdstr = "Insert Into [DetailData]([ShortName],[Title],[FirstName],[MiddleName],[LastName],[Occupation],[DOB],[Gender],[Country],[State],[Address],[ZipCode],[City],[Memo],[CategoryId],[SubCategoryId],[FavouritesID],[PhotoPath],[vCardPath]) VALUES(aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp,qq,rr,ss)"

  con = New OleDbConnection(constr)
  com = New OleDbCommand(cmdstr, con)
  con.Open()
 com.Parameters.AddWithValue("aa", xx)
 com.Parameters.AddWithValue("bb", cmbTitle.Text)
 com.Parameters.AddWithValue.....
 com.Parameters.AddWithValue....
 .....................
......
 com.ExecuteNonQuery()-------"Data type mismatch in criteria expression"  Here occurs Error

So can u or anybody help me to solve this Problem,please

+2  A: 

What exactly did you pass in for the DOB field? It can be something with your SQL statement.

mqbt
+8  A: 

To the OP, can you post an example of the value of cmdstr when the OleDbException occurs? I suspect mqbt is right and it's probably your DOB field not being able to parse whatever format your DateTime is giving it.

As a related question, is it a good practice to use OleDbCommand.Parameters.AddWithValue to change parameters in a SQL string this way?

I have always built SQL strings in VBA Access just by concatenating the string with the parameters then executing it with Docmd.RunSql(), which seems to suck for long SQL strings with tens of parameters. But it is simple to see the parameters and types. Have I been doing it wrong?

Dale Halliwell