Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried using the below code (for loop) to add rows to a datatable. The debugger shows the datatable row count increments; but the row list never populates. Am I creating some sort of scope issue with the for loop?

C#
myTable.Columns.Add("Item1", typeof(int)); // set fixed number columns
myTable.Columns.Add("Item2", typeof(int));
myTable.Columns.Add("Item3", typeof(int));
                       
  for (int i = 0; i < numItems; i++)
  {
    DataRow myRow = myTable.NewRow();

    line = sr.ReadLine();
    subLine = line.Substring(line.LastIndexOf("= ") + 2); // find value

    myRow["Item1"] = int.Parse(subLine,System.Globalization.NumberStyles.Any);     

    myTable.Rows.Add( myRow );            // add row object to the table


What I have tried:

I've tried declaring the NewRow object inside and outside the for loop. Outside i see an exception thrown.
Posted
Updated 22-Jan-17 11:02am
Comments
Kornfeld Eliyahu Peter 22-Jan-17 14:32pm    
At first glance your code seems all right... Please review it using this: https://msdn.microsoft.com/en-us/library/system.data.datatable.newrow(v=vs.110).aspx
[no name] 22-Jan-17 15:32pm    
It's probably not populating because you are using Substring incorrectly. Using the debugger would tell you what is wrong.
Kornfeld Eliyahu Peter 23-Jan-17 3:15am    
1. If you post your comments as solution you actually close your own question. It moves from unanswered to answered I gets less attention
2. The messages you posted here are irrelevant - those are status messages of the DLL loader
3. You are talking about 'list members' - what list? Maybe your problem is around the binding and not the new rows? Show the relevant code, so someone would check it

1 solution

I don't think the Substring line is it. It seems to load the int into the row object without error. Just to be sure I commented it out and replaced with the following;
C#
subLine = "123";
I get the same result, row counts and still no list members.

The debug widow list a lot of the following;
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
I'm assuming these are status messages??
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900