I have a dataset called "results" with several rows of data. I'd like to get this data into a string, but I can't quite figure out how to do it. I'm using the below code:
string output = "";
foreach (DataRow rows in results.Tables[0].Rows)
{
output = output + rows.ToString() + "\n";
}
However, I think I'm missing something because this isn't working. Can someone point me in the right direction?
Your
rows
object holds anItem
attribute where you can find the values for each of your columns. You can not expect the columns to concatenate themselves when you do a.ToString()
on the row. You should access each column from the row separately, use afor
or aforeach
to walk the array of columns.Here, take a look at the class:
I've done this a lot myself. If you just need a comma separated list for all of row values you can do this:
A StringBuilder is the preferred method as string concatenation is significantly slower for large amounts of data.
You need to specify which column of the datarow you want to pull data from.
Try the following:
You can get a columns value by doing this
You will also have to cast to the appropriate type.