Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friend,

In my Asp.Net c# application

we are creating datatable with 10K records and 120 columns, it is tacking more than 2 hours to complete the datatable.

THe following is the code i am using please help me out
C#
foreach (DataColumn dc in policyLevelTable.Columns)
{
    for (int i = 1; i <= 10000; i++)
    {
        if (dc.ColumnName.ToLower() == "policyid")
        {
            DataRow newdr = policyLevelTable.NewRow();
            newdr[0] = i;
            // newdr[1] = 1;
            policyLevelTable.Rows.Add(newdr);
        }
        else
        {
            DataRow[] dr = policyLevelTable.Select("policyid in (" + i + ")");
            dr[0][dc.ColumnName] = RandomParameterType(new List<int>{1,2,3,4}, 0, false);
        }
    }
}


Thanks,
Ramu.

What I have tried:

foreach (DataColumn dc in policyLevelTable.Columns)
{
for (int i = 1; i <= 10000; i++)
{
if (dc.ColumnName.ToLower() == "policyid")
{
DataRow newdr = policyLevelTable.NewRow();
newdr[0] = i;
// newdr[1] = 1;
policyLevelTable.Rows.Add(newdr);

}
else
{
DataRow[] dr = policyLevelTable.Select("policyid in (" + i + ")");

dr[0][dc.ColumnName] = RandomParameterType(new List<int> { 1, 2, 3, 4 }, 0, false);


}
}
}
Posted
Updated 4-Nov-16 1:43am
v2
Comments
Philippe Mori 5-Nov-16 11:21am    
You wrote twice the same code. Once in a properly formatted code block and once as unformatted text. What is the purpose of doing that?

Do it the other way round: loop through the rows as the outer loop, then fill in each column inside it. That way, you don't need to do the (probably time consuming) .Select for each cell in each row, you can omit it entirely, since you will always be working on the same row.
In addition, unless you need a new List each time, create one outside all loops.
Then check RenadomParamaterType and see what it's doing, and how much of the time it's taking.
 
Share this answer
 
Creating 10k rows is never going to be quick. What do you expect the user to do with 10k rows anyway? If they spend 3 seconds per row it would take them 8 hours to process all of the rows. Showing that much data is useless, implement paging and searching so that you only show a reduced amount of data, or only the data that is relevant.
 
Share this answer
 
v2
Comments
#realJSOP 4-Nov-16 11:25am    
The "why" of it is not relevant, IMHO.
I don't know how long this is gonna take, but there's less code, so silver linings...

C#
Random random = new Random();
for (int i = 1; i &lt;= 10000; i++)
{
    DataRow newdr = policyLevelTable.NewRow();
    for (int j = 0; j < policyLevelTable.Columns.Count; i++)
    {
        newdr[j] = (j==0) ? j : random.Next(5);
    }
}


Caveat - this is going to consume quite a bit of memory. If this is done on a website, you will likely have more problems than simple speed issues.
 
Share this answer
 
v4

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