Title: Display all strings from array in datatable
Tags: c#;arrays;datatable
Question: I have a DataTable that I get a row value from and it is formatted as follows: String databaseVal = "00010-12-D2324, K2343-13-D234234".
I am trying to manipulate the data and output each line separated by a comma to a DataTable. This is the code I have so far:
foreach (DataRow dr in finalResults.Rows)
DataTable dt = new DataTable();
string pin = this.rowValue("DB_Values", dr);
string[] splitValue = pin.Split(',');
List<string&gt; list = splitValue.ToList();
foreach (string item in list)
dt.Rows.Add(item.Split(new char[] { '-' }, 3));
foreach (DataRow row in dt.Rows)
equip1 = row[0].ToString();
equip2 = row[1].ToString();
equip3 = row[2].ToString();
So the desired result would be that the table would have 2 rows with the following:
Row 1: equip1=00010, equip2=12, equip3=D2324
Row 2: equip1=K2343, equip2=13, equip3=D234234
Right now I just get the first row and not the second. Any help would be appreciated.
Comment: @awh12, I am displaying them in a report. And I changed the names for this example. But the problem is I get one record but it skips the second. and in some cases there are 3 or 4.

Comment: @awh112 it is not a reserved keyword

Comment: The instruction "DataTable dt = new DataTable();" should precede the first loop and columns should be added to dt.

Comment: What are you doing with the `equipX` variables?  The code seems to work fine for me, provided you are doing something with the values before rewriting them.

Comment: Gotcha.  I ran the code locally and it seems to work fine for me; the only thing I had to do was define the columns of `dt` which I assume you do elsewhere.  Other than that, my `dt.Rows` Count was 2 and the final `foreach` loop executed as expected.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:01/31/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><arrays><datatable>', 'Title': 'Display all strings from array in datatable', 'ViewCount': 85, 'id': 41967122, 'user_AboutMe': '', 'user_AccountId': 982278.0, 'user_DisplayName': 'compucrazy', 'user_Reputation': 65.0, 'user_Views': 122.0, 'user_id': 1001711.0}}