"Can't convert string array into integer" Please help!

michael03m

New member
Joined
Dec 3, 2010
Messages
1
Programming Experience
Beginner
Hi,

I'm pretty much a beginner with VB.NET and am in the middle of a project for my class. I created an array of strings that pulls data from a csv file, and now im trying to update a database file with it. It gives me an error, though, saying it can't convert my string array into an integer. Can anyone help me?

VB.NET:
Dim UPCquantity As Integer
Dim updatedUPCquan As Integer
Dim UPC As Integer
Dim UPCquan As Integer
Dim upcstring(0) As String
Dim inputstream As New FileStream("packingslip.csv", FileMode.Open, FileAccess.Read)
Dim reader As New StreamReader(inputstream)

Do Until reader.EndOfStream
    upcstring = reader.ReadLine.Split(",")
    UPC = upcstring(0)
    UPCquan = upcstring(1)
    UPCquantity = myInvDB.GetQtyOnHand(1, UPC)
    updatedUPCquan = UPCquan + UPCquantity
    myInvDB.UpdateQtyOnHand(1, UPC, updatedUPCquan)
Loop
If myInvDB.UpdateQtyOnHand(1, manualUPC.Text, updatedUPCquan) = True Then
    MsgBox("Successful Entry")
Else
    MsgBox("Unsuccessful")[/SIZE][/SIZE]
End If
End Sub
 
Last edited by a moderator:
Converting strings to integers

You can use the TryParse() method to convert a string to an integer. The first argument is the string to convert. The second argument is the number to convert into.

Here is an example -- It makes use a an initialized string array to copy to an empty integer array. In your case, you would be using a string array that already has values assigned. The For-Next loop copies the converted values into the integer array one by one.

VB.NET:
		Dim strarray() As String = {"11", "22", "33", "44", "55"}
		Dim intarray(4) As Integer
		For x As Integer = 0 To 4
			Integer.TryParse(strarray(x), intarray(x))
		Next x
 
Back
Top