Compare datatable row

Padmanabhan, Venkatesh 120 Reputation points
2023-04-10T14:33:23.87+00:00

Hi. I have the data from web.config stored in a variable. I have a datatable which contains the columns name and marks. I want to compare the datatable with the appSettings.. Example: for each items in the data table, datarow of name equals Joe = key name from app.config and datarow of marks <= value from the app.config The web.config has values in this format <add key="Joe" value="55"/> <add key="Richard" value="90"/> How to compare it in c# ?

 var appSettings = ConfigurationManager.AppSettings.AllKeys
                         .ToDictionary(key => key, key => ConfigurationManager.AppSettings[key]);
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,092 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 47,206 Reputation points Microsoft Vendor
    2023-04-11T12:08:03.9233333+00:00

    Hi @Padmanabhan, Venkatesh ,Welcome to Q&A.
    Create a config file first, and then get its AppSettingsSection. Get the corresponding value by looping dt.rows and using the value of the name column of row as the key. In this way you can compare it well. You could try the following code to get what you wanted.

    using System;
    using System.Configuration;
    using System.Data;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            DataTable dt = new DataTable();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                AppSettingsSection appSettings = config.AppSettings;
                foreach (DataRow row in dt.Rows)
                {
                    string name = row["name"].ToString();
                    int value = Convert.ToInt32(row["value"]);
                    // Retrieve the value from appSettings for the corresponding key
                    string appSettingValue = appSettings.Settings[name]?.Value;
    
                    if (!string.IsNullOrEmpty(appSettingValue) && value <= Convert.ToInt32(appSettingValue))
                    {
                        Console.WriteLine($"For name: {name}, value: {value} is less than or equal to appSetting value: {appSettingValue}");
                    }
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //Create a datatable containing name and value
                dt.Columns.Add("name", typeof(string));
                dt.Columns.Add("value", typeof(int));
                dt.Rows.Add("Joe", 60);
                dt.Rows.Add("Richard", 90);
                dt.Rows.Add("Mary", 60);
                dataGridView1.DataSource = dt;
            }
        }
    }
    

    enter image description here

    <configuration>
      <appSettings>
        <add key="Joe" value="55" />
        <add key="Richard" value="90" />
      </appSettings>
    </configuration>
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.