Question

I have converted a VB6 project to VB.NET using Visual Studio 2008. I have some code that throws an error for invalid cast type when for doa.Field to String Type.

Here is the code that I am working with:

If rstLogin.BOF = True And rstLogin.EOF = True Then
        MsgBox("User name not found, please try again!", , "Login")
        frmLogin.txtUserName.Focus()
        System.Windows.Forms.SendKeys.Send("{Home}+{End}")
    Else
        If Trim(inPassword) = Clean(rstLogin.Fields.Item("PassWord")) Then
            Call MoveUserRecord(rstLogin)
            LogUserIn = True
        Else
            MsgBox("Invalid Password, please try again!", , "Login")
            frmLogin.txtPassword.Focus()
            System.Windows.Forms.SendKeys.Send("{Home}+{End}")
        End If
    End If

The error is:

Unable to cast COM object of type 'dao.FieldClass' to class type 'System.String'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

The error is thrown on the part of the code for the password.

If Trim(inPassword) = Clean(rstLogin.Fields.Item("PassWord")) Then

I have tried to add .ToString() to everything that I could add it to, and I still get the same error. Can someone please tell me what I am doing wrong, and how I can fix this?

Was it helpful?

Solution

Hans Passant was really close with is answer. In order for you to fix the error, and for your code to compile correctly. You will need to use:

rstLogin.Fields("PassWord").Value.ToString()

I have tested this, and I am sure that it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top