I have a legacy solution written in VB.NET. It writes to a DB2 database using ODBC. I have a Textbox that is filled with a numeric value then I use ToString
in order to cast whatever is written there and write it to the database using ExecuteNonQuery since there the field is of CHAR(10) type, the problem that it yields Arithmetic operation resulted in an overflow
when compiling using AnyCPU
but it does not happen when compiling in 32 bits.
What is causing this since I am using it as a String?
Edit:
Public Sub ExecuteTransaction(ByVal connectionString As String)
Using connection As New OdbcConnection(connectionString)
Dim command As New OdbcCommand()
command.Connection = connection
Try
connection.Open()
command.Connection = connection
command.CommandText = "Update QS36f.table set Cat= 'F' where Num= '" & Me.txt2.ToString.Trim &"'"
command.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub
When I enter 12345
in the TextBox and then do the following:
Dim input As String = Me.txt2.ToString.Trim
The contents of input
are not the desired "12345"
, but instead a string representation of the TextBox and its contents, namely: "System.Windows.Forms.TextBox, Text: 12345"
To get only the contents, use .Text
:
Dim input As String = Me.txt2.Text.Trim
I would also try and convert it to an integer (assuming the desired input is an integer) right away before handing the input to the query:
Dim input As Integer
If Not Integer.TryParse(Me.txt2.Text, input) Then
MessageBox.Show("Input could not be converted to integer")
Exit Sub
End If
If that does not solve it then (as user18387401 mentioned) using parameters instead of string concatenation might help:
command.CommandText = "Update QS36f.table set Cat= 'F' where Num= @Num"
Dim param As New OdbcParameter("Num", OdbcType.VarChar)
param.Value = Me.txt2.Text.Trim
command.Parameters.Add(param)
EDIT: Don't use Parameters.AddWithValue
.