Home > Software design >  I need to go through each and every row of a datatable
I need to go through each and every row of a datatable

Time:05-27

I have a data table named dt which is a SQL table converted.

All the data was sent from the server so I had to encrypt it. Now I need to decrypt every cell.

I wanted to do it like this:

foreach (DataRow row in dt.Rows)
{
    for (int i = 0; i < dt.Columns.Count; i  )
    {
        row[i] = r.Decrypt(row[i].ToString());
    }
}

Decrypt works fine but row[i] = r.Decrypt(row[i].ToString()); give me the number of the column instead of its content, how do I get the content?

CodePudding user response:

This is the correct way to interact with a datatable

  foreach(DataRow row in dt .Rows)
    {
        foreach(DataColumn column in dt .Columns)
        {
            Console.WriteLine(row[column]);
        }
    }

In your case you are literally passing an int to your row, because You are using count property.

CodePudding user response:

As you mentioned that the other logic (Decrypt) is working fine and you just need to fetch the column content/values then you can try something like:

   foreach(DataRow row in table.Rows)
   {
      foreach(DataColumn column in table.Columns)
      {
          // you can insert your logic to use column value row[column] here
          Console.WriteLine(row[column]);
      }
    }
  • Related