Option Strict On prohibits operands of type Object for operator '&'. RRS feed

  • Question


    Dim cmd As New OleDb.OleDbCommand
            If Not app.State = ConnectionState.Open Then
                'open connection if it is not yet open
            End If

            cmd.Connection = app
            'check whether add new or update
    If Me.txtID.Tag & "" = "" Then
                'add new 
                'add data to table
                cmd.CommandText = "INSERT INTO ADMIN(CusID,Name) " & _
                                " VALUES('" & Me.txtID.Text & "','" & Me.txtname.Text & "')"
                'update data in table
                cmd.CommandText = "UPDATE ADMIN " & _
                            " SET ID=" & Me.txtID.Text & _
                            ", Name='" & Me.txtname.Text & "'" & _
    " WHERE ID=" & Me.txtID.Tag

            End If
            'refresh data in list
            'clear form

            'close connection

    I GET Option Strict On prohibits operands of type Object for operator '&'.  on this code (If Me.txtID.Tag & "" = "" Then) and (" WHERE ID=" & Me.txtID.Tag)         

    • Edited by christing Friday, August 2, 2019 9:47 AM
    Friday, August 2, 2019 9:47 AM

All replies

  • If Cstr(txtID.Tag) & "" = "" Then

    Be aware that the value is a equal sign inside two double quotes in the way you use it. 


    Friday, August 2, 2019 10:05 AM
  • Hello,

    The Tag property of any Windows form control is of type Object as per the following documentation.

    Which means you can store anything such as a string but must cast the value stored in a Tag to the proper type, in your case CStr.

    Also, you should be using parameters anytime you have code such as shown in cmd.CommandText. Imagine Me.txtname.Text has an apostrophe, this would end up being an unescaped character and cause a runtime exception while using a parameter handles this for you.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, August 2, 2019 10:21 AM