user1342164
user1342164

Reputation: 1454

Str.Split for unknown number of values with insert statement for each?

If I dont know the number of comma separated values how can I store each value and then execute an insert statement for each value. So if the value of str is 123456,321654,321545 (I dont know that there will be 3 values ahead of time) how can I split all of em and do 3 inserts? I am thinking it would be a for each statement? But I am not sure how to do that with the .split? Can someone offer me some direction on this? My code below will just return the first value. Thank you

Dim str As String = Session("List")
    str = str.Split(",")(1)

    Return

Upvotes: 3

Views: 168

Answers (2)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

You are correct that it will be a foreach. The Split method returns a string[]

For Each s As String In str.Split(","c)
    ' build the insert statement and execute
Next

Upvotes: 4

Douglas Barbin
Douglas Barbin

Reputation: 3615

You need to split the string. Try this:

Dim str As String = DirectCast(Session("List"), String)
For Each item As String In str.Split(","c)
  ' Do stuff with item here
Next item

Upvotes: 4

Related Questions