错误[22018] [Microsoft] [ODBC Microsoft Access驱动程序]条件表达式中的数据类型不匹配

时间:2011-12-07 05:22:54

标签: vb.net

我有一个错误错误[22018] [Microsoft] [ODBC Microsoft Access驱动程序]数据类型在条件表达式中不匹配。我一直试图解决这个问题,但不幸的是我无法得到这个错误的黑客。我是dotNet的新手。

Public Sub LogInContinue(ByVal senter As Object, ByVal e As EventArgs)
    Dim LibDS As DataSet = New DataSet
    Dim LibDA As OdbcDataAdapter = New OdbcDataAdapter("SELECT * FROM tblUserAccount WHERE Uname='" & txtUserName.Text & "'", LibConn)
    Dim A As Integer
    LibDS.Tables.Add("tblUserAccount")
    A = LibDA.Fill(LibDS, "tblUserAccount")

    If A = 0 Then
        MessageBox.Show("Username you have supplied is invalid!" & vbCrLf & "Please click OK button to try again.", "INVALID USERNAME", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        If LibDS.Tables(0).Rows(0)("Uname") = Me.txtUserName.Text AndAlso LibDS.Tables(0).Rows(0)("Password") = Me.txtPassword.Text Then
            NewUserID.UUserID = LibDS.Tables(0).Rows(0)("UserID")
            UserAcceptedRole()
        Else
            MessageBox.Show("Password you have supplied is invalid." & vbCrLf & "Please try again!", "PASSWORD MISMATCHED", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End If
End Sub

Private Sub UserAcceptedRole()
    Dim ActualRole As String
    Dim LibDSa As DataSet = New DataSet
    Dim LibDAa As OdbcDataAdapter = New OdbcDataAdapter("SELECT * FROM tblRoles WHERE UserID='" & NewUserID.UUserID & "'", LibConn)
    LibDSa.Tables.Add("tblRoles")
    LibDAa.Fill(LibDSa, "tblRoles")

    ActualRole = LibDS.Tables(0).Rows(0)("Role").ToString
    If ActualRole = "Member" Then
        lblWelcome.Text = "Welcome " & txtUserName.Text
        NewObject.NewLabel(lblID, "You are logged in as " & ActualRole, "Calibri", 12, FontStyle.Regular, 666, 40)
        btnLogIn.Text = "Logout"
    End If
End Sub

错误位于LibDAa.Fill(LibDSa, "tblRoles")下的代码Private Sub UserAcceptedRole()中。请告诉我这个错误到底是什么?如果你对改进我的代码(风格)有一些建议,请告诉我。谢谢你。

2 个答案:

答案 0 :(得分:2)

用户ID可能是整数类型,因此请尝试从围绕用户ID的查询中删除''作为'232'等。错误可能在以下语句中:

Dim LibDAa As OdbcDataAdapter = New OdbcDataAdapter("SELECT * FROM tblRoles WHERE UserID='" & NewUserID.UUserID & "'", LibConn)

检查:LibDS是此SUB的本地

Public Sub LogInContinue(ByVal senter As Object, ByVal e As EventArgs)
    Dim LibDS As DataSet = New DataSet   /// local to this sub

如何在其他SUB UserAcceptedRole()

中访问此内容
ActualRole = LibDS.Tables(0).Rows(0)("Role").ToString

最佳方法是将角色作为参数传递。

答案 1 :(得分:1)

始终使用参数(参数化SQL语句)。详情阅读ParametersSQL Injection.

Private Sub UserAcceptedRole()
    Dim ActualRole As String
    Dim LibDt As New DataTable
    Dim Cmd As New OdbcCommand
    Cmd.CommandText="SELECT * FROM tblRoles WHERE UserID=@UserID"
    Cmd.Connection=LibConn

    MsgBox("Verifying the value of : " & NewUserID.UUserID)

    Cmd.Parameters.Add("@UserID",System.Data.Odbc.OdbcType.Int).Value=NewUserID.UUserID

    Dim LibDAa As OdbcDataAdapter = New OdbcDataAdapter(Cmd)
    LibDAa.Fill(LibDt)

    If LibDt.Rows.Count<>0 Then

      ActualRole = LibDt.Rows(0)("Role").ToString

      If ActualRole = "Member" Then
        lblWelcome.Text = "Welcome " & txtUserName.Text
        ....
      End If
    End If
End Sub

PS:您也可以使用DataSet。