Click here to Skip to main content
16,021,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone, i'am trying read the data from a table MS acces in a webforms ASP with visual basic 2010, but this show the following error. someone you maybe helpme.

Error Message "Data type mismatch in criteria expression."
here's the code:

Protected Sub btnbuscar1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnbuscar1.Click
Try
Dim con As String = Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\luis.valdez\Documents\info.accdb
conConexion = New OleDb.OleDbConnection(con)
Catch ex As Exception
MsgBox("Error al conectarse")
End Try

Dim lista As Byte
Dim str As String

If txtIdcaso.Text Then
conConexion.Open()
str = SELECT * FROM Casos_Tbl WHERE (IDCaso = '" & txtIdcaso.Text & "')"
'Dim cmd As OleDbCommand = New OleDbCommand(str, conConexion)
adaptador = New OleDb.OleDbDataAdapter(str, conConexion)
registro = New DataSet
adaptador.Fill(registro, "Casos_Tbl")
lista = registro.Tables("Casos_Tbl").Rows.Count
Else
MsgBox("Debe Ingresar un codigo.", vbExclamation, "UTCtickets")
End If
If lista = 0 Then
lblidcase.Text = registro.Tables("Casos_Tbl").Rows(0).Item("IDCaso")
txtuser.Text = registro.Tables("Casos_Tbl").Rows(0).Item("Usuario")
txtarea.Text = registro.Tables("Casos_Tbl").Rows(0).Item("Area")
lblDate.Text = registro.Tables("Casos_Tbl").Rows(0).Item("FechayHora")
Dropcateg.Text = registro.Tables("Casos_Tbl").Rows(0).Item("Categoria")
LblSev1.Text = registro.Tables("Casos_Tbl").Rows(0).Item("Severidad")
dropstat.Text = registro.Tables("Casos_Tbl").Rows(0).Item("Status")
txtdescort.Text = registro.Tables("Casos_Tbl").Rows(0).Item("DescripcionCorta")
txtdescdetall.Text = registro.Tables("Casos_Tbl").Rows(0).Item("DescripcionDetallada")
End If
txtIdcaso.Focus()
End Sub
Posted

It looks like IDCaso may be stored as an integer or other numeric type.

Try changing your select statement to not quote the value:

str = SELECT * FROM Casos_Tbl WHERE (IDCaso = " & txtIdcaso.Text & ")"
 
Share this answer
 
Comments
Sascha Lefèvre 21-Apr-15 17:53pm    
I voted 4 because it will most likely resolve his problem. But you should not suggest concatenating values into a SQL-string because it has several disadvantages; the most signifcant is that it allows for SQL-Injection.
Instead he (and you) should be using SQL-Parameters. Example here: http://www.dotnetperls.com/sqlparameter
I would suggest you to improve your solution and mention that.
Bob@work 21-Apr-15 18:05pm    
I concur.

As an alternative (recommend limiting to simple queries), you can test the value of txtIdcaso.text for the proper format. An example would be

if cint(val(txtidcaso.text)=0, then don't accomplish the query.

However, if the datatable has a record with IDCaso=0 then, you'll need a different approach and SQL Parameters are much more secure.

Afirmative, is a field autonumber and also is autoincrement the, what can i to do for read the data withou problems?
 
Share this answer
 
Comments
Sascha Lefèvre 21-Apr-15 17:54pm    
This is not a solution. And Bob@work didn't get a notification about it.
Please delete this and post it as a comment instead with the "Have a Question or Comment?"-Button below his answer.
Please also see my comment to the 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