0

I have an asp.net/vb.net web app that requires information to be put into a multiline text box. If the user hits enter while in the textbox it drops down to next line, and they can enter more data in. When it tries to go to the database it fails because of the way the data is represented. I need help with looping through the field and getting each value.

This is how the data is represented in the DataTable

0;0.123;0.234;0.345;...

I need each value between the ; character... so I was thinking something like this?

If dbRow.Item("PV").ToString.Contains(";") Then

    For Each symbol As String In DBTable.Rows

        'get data b/w the ';'

     Next

End If

Any help would be greatly appreciated

Edit:

If dbRow.Item("PV").ToString.Contains(";") Then

   For Each s As String In dbRow.Item("PV").ToString

       Dim fields() As String = s.Split(";"c)

       For Each value As String In fields

            .Append("'" & CDbl(value) & "'," & "SysDate,1)")
             DBCommand.CommandText = myInsertIntoProperty_DataStringBuilder.ToString
             DBCommand.ExecuteNonQuery()
             myInsertIntoPropertyStringBuilder = New StringBuilder

        Next

     Next

    Else

            .Append("'" & CDbl(dbRow.Item("PV")) & "'," & "SysDate,1)")

    End If
BenMorel
  • 34,448
  • 50
  • 182
  • 322
bbesase
  • 791
  • 4
  • 18
  • 31
  • Are you saying you want to replace the line breaks with semi-colons and when reading from the database, replace the semi-colons with line breaks? – xDaevax May 29 '14 at 12:34
  • Please use Split(';') function see below link http://stackoverflow.com/questions/7503310/get-the-nth-string-of-text-between-2-separator-characters – Deepak Joshi May 29 '14 at 12:37
  • No I don't want to replace them , I just want to get the data in-between the `;` so I want `0,0.123,0.234` – bbesase May 29 '14 at 12:57

1 Answers1

2

You mean something like this?

 Dim s As String In dbRow.Item("PV").ToString()
 Dim fields() As String = s.Split(";"c)

 For Each value As String In fields
     ' Do what you want with the value
 Next

Then access each value with field(0), fields(1), etc. You can then convert it to the appropriate type, for example:

Dim value As Double = Double.Parse(fields(0), CultureInfo.InvariantCulture)
SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • The amount of fields would be varying though, would you just run another `forloop` and do whatever with each field? – bbesase May 29 '14 at 13:03
  • I had to change around one thing and it works except each value in the array `fields()` in the `for loop` is showing up as `""` – bbesase May 29 '14 at 13:18
  • Maybe you have empty values in the DataTable (like `;;`). You can use this to remove empty entries: `Dim fields() As String = s.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)` – SysDragon May 29 '14 at 13:21
  • I just looked at the whole string value and there are no empty entries...I'll update what I have now – bbesase May 29 '14 at 13:32
  • 1
    Just remove the `.ToString` at the end in: `For Each s As String In dbRow.Item("PV").ToString` – SysDragon May 29 '14 at 13:42
  • Sorry I was coding without trying the code. Try my edit – SysDragon May 29 '14 at 14:44