0

I have some code that loops through a listview and puts the values into 'output1' & 'output2'. However, what is happening is that the results are strung together like thus: 0307NG77775660NG7778. How do I split so that I can include in access db.

There are 2 columns in the listview and they contain values in the format of:

1st row 0307 - NG7777, 2nd row 5660 - 7778 etc. This is a desktop winforms code. Thanks

        Dim BoxList As New List(Of String)
        Dim BoxItem As String
        Dim CustRefList As New List(Of String)
        Dim CustRef As String

        For Each item As ListViewItem In Me.lvSelectedItems.Items
            Dim box As String = item.Text
            BoxItem = item.SubItems.Item(1).Text
            BoxList.Add(box)
            output2 += BoxItem

            Dim cust As String = item.Text
            CustRef = item.SubItems.Item(1).Text
            CustRefList.Add(cust)
            output1 += CustRef
        Next

        output = String.Join(" "c, BoxList.ToArray(), CustRefList.ToArray())

        Dim cmd As OleDbCommand = New OleDbCommand("Insert into Temp (temp, [class]) Values ('" & output1 & "', '" & output2 & "')", oledbCnn)
        dr = cmd.ExecuteReader
user1532468
  • 1,723
  • 8
  • 41
  • 80

1 Answers1

1

Make username a List(Of String), then just do:

usernameList.Add(username) 'instead of output1 += username

When done with the loop, do this:

output1 = String.Join(" "c, usernameList.ToArray())

You can pick a join character of your choice (if you don't like spaces " ").

EDIT: Example:

Dim usernameList As New List(Of String)
Dim session As String

For Each item As ListViewItem In Me.lvSelectedItems.Items
  Dim username As String = item.Text
  session = item.SubItems.Item(1).Text
  usernameList.Add(username)
  output2 += session
Next

output1 = String.Join(" "c, usernameList.ToArray())
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • Neolisk I am quite new to vb.net and not advanced enough yet to code what you suggested. Would you have an example you could post please. Thanks – user1532468 Jan 16 '14 at 19:08
  • @user1532468: See my example. I converted `userName`, you can do `session` for learning purposes. – Victor Zakharov Jan 16 '14 at 19:20
  • Many thanks. Just one question, would session be in the same for each as username or a new one. Thanks – user1532468 Jan 16 '14 at 19:38
  • @user1532468: Same ForEach is fine. – Victor Zakharov Jan 16 '14 at 19:39
  • 1
    Many thanks for your help. If I have any problems I shall repost. – user1532468 Jan 16 '14 at 19:39
  • Neolisk it errors with 'Object reference not set to an instance of an object.' which is occuring here: usernameList.Add(username) – user1532468 Jan 16 '14 at 20:19
  • @user1532468: I am sorry, there was a typo - it should have been `New List(Of String)`. I fixed it just now. – Victor Zakharov Jan 16 '14 at 20:22
  • Neolisk. Please look at my amended code. It isn't doing what I thought it would do. It is grouping all the values for each list in a single line. for example, NG565 77889. I need these to be separate for inclusion in db. The user also has the ability to select multiple files, so I don't think this is correct. What is the correct way? Thanks – user1532468 Jan 17 '14 at 20:10
  • @user1532468: Don't Join then. Just use a List you have. – Victor Zakharov Jan 17 '14 at 20:14
  • Neolisk. Are you meaning output =output1 + output2. Thanks – user1532468 Jan 17 '14 at 20:22
  • Neolisk, I am not grasping this. I am getting same item in each output1 and output2. Do I need to change the ite, text somewhere. Thanks – user1532468 Jan 17 '14 at 20:32
  • I have to go out now, but I shall check for any updates in the morning. Many thanks for your patience. – user1532468 Jan 17 '14 at 20:37
  • @user1532468: ExecuteReader is meant for reading. To update, you need [SqlCommand.ExecuteNonQuery](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx). Check [this out](http://stackoverflow.com/questions/6360153/vb-net-sql-server-insert-executenonquery-connection-property-has-not-been-ini). Basically, loop through your lists, and issue INSERT statement for each. `For i = 0 To list.Count` etc. - assuming both counts are the same, you should be able to do use `list1(i)` and `list2(i)`. Notice use of parameters on the link I mentioned. – Victor Zakharov Jan 17 '14 at 20:42
  • Neolisk, do I loop through the lists in the for each loop and execute sqk there or is that incorrect. thanks – user1532468 Jan 18 '14 at 09:47
  • @user1532468: That would be two loops for you. I'm suggesting one loop, using index variable, to access both of your lists in one loop. The rest is okay. – Victor Zakharov Jan 18 '14 at 13:43