Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
foreach (DataTable dr in tableCollection) // search whole table
{
    if (dr["TestCase5"] == 5)
    {
        dr["TestCase5"] = "CDE";
    }

}


What I have tried:

I tried to fix via code i found on the internet
Posted
Updated 30-Nov-22 1:52am
v3
Comments
OriginalGriff 30-Nov-22 7:09am    
1) DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
2) This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

You could start by telling us what is wrong with the code ...

Use the "Improve question" widget to edit your question and provide better information.
Richard Deeming 30-Nov-22 7:09am    
Since you haven't told us what error, nobody can help you.

Click the green "Improve question" link and update your question to include the full details of the error you're getting, and explain precisely what you have tried and where you are stuck.
PIEBALDconsult 30-Nov-22 7:53am    
"code i found on the internet" << there's your problem.

1 solution

Your dr variable is a DataTable:
DataTable Class (System.Data) | Microsoft Learn[^]

The DataTable class doesn't have an indexer. Asking what value dr["TestCase5"] has is like asking what telephone number London is - the question doesn't make sense.

Assuming your tableCollection variable does actually contain a collection of DataTable objects, and you want to process every row within those tables, you will need a nested loop to iterate over the rows of the table:
C#
foreach (DataTable table in tableCollection)
{
    foreach (DataRow row in table.Rows)
    {
        if (row["TestCase5"] == 5)
        {
            row["TestCase5"] = "CDE";
        }
    }
}
 
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