Convert String to Long

heather

New member
Joined
Mar 20, 2006
Messages
2
Programming Experience
1-3
I am trying to convert a String from a Textbox to a Long Integer. I've tried everything I can think of. Thanks in advance. Here's the error I get:

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:
Line 553: Dim getSerial As TextBox = e.Item.Cells(5).Controls(0)
Line 554: Dim iIdNumber as TableCell = e.Item.Cells(0)
Line 555: Dim intID as Long = Convert.ToInt64(iIdNumber.Text)
Line 556: Dim strPC As String = UCase(getPC.Text)Line 557: Dim strSerial As String = getSerial.Text

... and here's the code:

Sub dgrd_application_Update(sender As Object, e As DataGridCommandEventArgs)
Dim getPC As TextBox = e.Item.Cells(4).Controls(0)
Dim getSerial As TextBox = e.Item.Cells(5).Controls(0)
Dim iIdNumber as TableCell = e.Item.Cells(0)
Dim intID as Long = Convert.ToInt64(iIdNumber.Text)
Dim strPC As String = UCase(getPC.Text)
Dim strSerial As String = getSerial.Text

' CONNECT TO DATABASE...
objConnection = New OledbConnection(strConnection)
objConnection.Open()

strSQL = "UPDATE SQA SET PC = '" & strPC & "', SerialNo = '" & strSerial & "' WHERE ID = " & intID

' UPDATE SQA TABLE...
objCommand = New OledbCommand(strSQL,objConnection)
objCommand.ExecuteNonQuery() objCommand = Nothing

' DISCONNECT FROM DATABASE...
objConnection.Close() objConnection = Nothing dgrd_application.EditItemIndex = -1
BindData()

End Sub
 
iIdNumber corresponds to an AutoNumber in an Access Database that is a type Long Integer. I picked Int64 because I was told that was equivalent to a Long Integer. At first, I was trying to use the Integer type but was having problems and thought I needed to switch to Long... but I'm still having problems!
 
However, i'd do that without much complicating (as it follows):
VB.NET:
[COLOR=#0000ff][SIZE=2]Dim[/SIZE][/COLOR][SIZE=2] intID [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2].Parse([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].[/SIZE]iIdNumber[SIZE=2].Text)[/SIZE]

Regards ;)
 
May I suggest:

Verify if the textbox contains a number...

VB.NET:
        If IsNumeric([COLOR=Blue]iIdNumber.Text[/COLOR]) Then
            intID = Convert.ToInt64([COLOR=Blue]iIdNumber.Text[/COLOR])
            '[COLOR=Green]MessageBox.Show(intID, "", MessageBoxButtons.OK, MessageBoxIcon.Information)[/COLOR]
        Else
            MessageBox.Show[COLOR=Black]("[/COLOR][COLOR=Black]iIdNumber [/COLOR]must be a numeric value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
 
Long before is Integer now (or Int32), it's 4 bytes.
 
But a long now is in fact int64....
What is an integer now is int32
What is short now is int16

-tg
 
it really doesn't matter unless you have enormous big value(s) inside certain field. Let me illustrate by an example. An illustration would be where it is known in advance that values of the same field will be acquired with a total value not over 256 "records/rows/whatever.." (althrough autonumber is a type Long Integer) well, then you can even go for Byte. Mainly his issue wasn't about size but rather about conversion and i think that Integer is just correct datatype in this case.
 
JohnH said:
... is Integer now (or Int32)...
I totally agree with you, kulrom :) The last post was just to prove the connection between the data types, their factual byte sizes, and the terminology used in different contexts.
 
JohnH said:
Yes, TechGnome, but that is irrelevant to the question :)

The question was about MS Access autonumber field, which is 4 bytes (32bits)
http://office.microsoft.com/en-au/assistance/HP052385181033.aspx

This was in old references called "long integer".
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acdatComparisonDataTypes_HV05186571.asp
(when Integer was Long and Short was Integer..)

Yeah, I didn't realize that until after I hit the button....
I'll take my lumps now...

-tg
 
Back
Top