VB.NET Operator '&' is not defined for string and type 'Internalfield'

2k views Asked by At

Background

Currently working on a plugin in ArcMap done in VBA from early 2000 and refactoring it to VB.NET. The database is a MS Access Database.

Pretty new to VB.NET and coding in general.

Error

The issue I'm facing is that I'm getting the error:

InvalidCastException was unhandled.

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Operator '&' is not defined for string "UPDATE CustomerArea SET Area_m2=547," and type 'InternalField'.

Code

Dim nr
Dim area_polygon_excl_faktor As Double
Dim area_db

Do While Not AdoRS.BOF
   nr = AdoRS("nr")
   area_db = CDbl(AdoRS("area_m2").Value)

   Dim result As MsgBoxResult
   result = MsgBox(aAttrID(index) & "  " & area_db & "  " & area_polygon_exkl_faktor & "  Would you like to update the area?", vbYesNoCancel, "Update of area")

   If result = MsgBoxResult.Yes Then
      strSqlUpdateArea = "UPDATE CustomerArea SET Area_m2='" & area_polygon_excl_faktor & "' WHERE Nr= '" & nr

      AdoConn.Execute(strSqlUpdateArea)

   ElseIf result = MsgBoxResult.No Then

      MsgBox("You have decided not to update the area")

   End If

The AdoRS is an ADODB.Recordset. The issue I am facing is on the line:

strSqlUpdateArea = "UPDATE CustomerArea SET Area_m2='" & area_polygon_excl_faktor & "' WHERE Nr= '" & nr

I've tried rewriting it several times and even adding a Convert.toString in the line nr = AdoRS("nr"), but with no luck. I know that the Nr in Access is an AutoNumber (LongInteger).

2

There are 2 answers

0
Gustav On

The old VBA code looked like this ...

Then no quotes, and force a decimal separator to a dot:

Dim format As System.Globalization.CultureInfo

format = System.Globalization.CultureInfo.InvariantCulture

strSqlUpdateArea = "UPDATE CustomerArea SET Area_m2 = " & area_polygon_excl_faktor.ToString(format) & " WHERE Nr = " & nr.ToString(format)
0
Anna3anana On

The solution was that I had to convert the type InternalField into a string properly and do the steps in the right order.

Dim sNr As String
Dim area_polygon_excl_faktor As Double
Dim area_db

Do While Not AdoRS.BOF
   sNr = Convert.ToString(AdoRS("nr").Value)
   area_db = CDbl(AdoRS("area_m2").Value)

   Dim result As MsgBoxResult
   result = MsgBox(aAttrID(index) & "  " & area_db & "  " & area_polygon_excl_faktor & "  Would you like to update the area?", vbYesNoCancel, "Update of area")

   If result = MsgBoxResult.Yes Then
      strSqlUpdateArea = "UPDATE CustomerArea Set Area_m2='" & area_polygon_excl_faktor & "' WHERE Nr=" & sNr

   AdoConn.Execute(strSqlUpdateArea)
   ElseIf result = MsgBoxResult.No Then
      MsgBox("You have decided not to update the area")
   End If