Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataset into string 2

Status
Not open for further replies.

slybitz

Technical User
Mar 25, 2005
113
US
Hello. I have a dataset that I fill with one column of data (amount of rows varies). I'm trying to figure out how to take each value in each row and append it to a string. So for example, if the dataset is full of one column of values 1111 in rwo 1, 2222 in row 2, 3333 in row 3, 4444 in row 4... I want the string to end up looking like "1111, 2222, 3333, 4444". I'm not too sure how to grab the values out of the dataset individually.

Is this possible?
 
Dim s As String

s = ""

For Each dr As DataRow in DataSet1.Tables(0).Rows
'note: use the actual name of the field in place of FieldName
s &= dr.Item("FieldName").ToString & ","
Next

'remove the last comma from the string
s = s.SubString(0, s.Length - 1)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
First off, datasets can't have rows --- they however can have datatables, which is what I'm assuming your actually asking.

Try something like this and see if it works for you:

(note, I didn't actually run this with a real table, so you'll want to test)

Code:
Private Function ConvertDataToString(ByVal dt As DataTable) As String

      Dim DataString As String = ""

      ' use counter instead of for/each since we'll need to know when we get to the end
      ' could also use "for i as integer = 0 to tbl.rows.count - 1"
      Dim count As Integer = 0
      Do While count < dt.Rows.Count

         ' since there is only one column in the row, we can just take the column at index 0
         DataString += dt.Rows(count).Item(0).ToString
         If count < dt.Rows.Count Then
            ' only append the comma if it's not the last row
            DataString += ", "
         End If

         ' increment counter
         count += 1

      Loop

      Return DataString

   End Function
 
Also, if there are a lot of rows, and performance is a consideration, you may want to use a StringBuilder instead of a string.

Anytime you perform a concatination with strings the compiler has to create a 3rd string to put the first two strings into.

So if the variable s is already 500 characters long (2 Bytes per character = 1000B) and you do this:

s &= "'1234',"

You will have the 1KB of memory for the original value of s, 12B for the new value to append, and 1012B for the new value. Which means you are using about 2KB of memory to concatinate those two strings.

-Rick


VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Perfect!!!! Thank you all. I'll test it out and let you know if it works. Stars to come!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top