none
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
                app.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 & "')"
                cmd.ExecuteNonQuery()
            Else
                'update data in table
                cmd.CommandText = "UPDATE ADMIN " & _
                            " SET ID=" & Me.txtID.Text & _
                            ", Name='" & Me.txtname.Text & "'" & _
    " WHERE ID=" & Me.txtID.Tag

                cmd.ExecuteNonQuery()
            End If
            'refresh data in list
            RefreshData()
            'clear form
            Me.btnclear.PerformClick()

            'close connection
            app.Close()

    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. 


    Success
    Cor

    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.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.tag?view=netframework-4.8

    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.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Friday, August 2, 2019 10:21 AM
    Moderator