0

I'm creating an application that will look AppSettings, pull those keys and their values up into a DataTable, use the DataTable to edit or add keys, check for duplicate keys once edited, and save them back to into AppSettings. I currently have three buttons.

One is ProgrammedBy which pulls up a separate data table, a View App Settings within App.config and display it on a data table, and finally an Update Settings button that checks for duplicate keys after the information is entered on the DataTable then save the keys and their values back to AppSettings:

AppView

Here is a list of the keys and values within in the App.config file. Note I have given a key a duplicate name:

App.Config

I'm using a nested for loop on the part where it checks for duplicate keys. The System.Collection library only checks the last duplicate key. It totally ignores the first one. When I drill down or hover my mouse over the items variable, Rows/Results View, within Visual Studio it only shows me a 5 key pair. There is obviously a 6th pair as seen above. Here is that for loop and for the purpose of debugging I have put a break point past the end of the for loop:

Nested For Loop within button instance

I need it to be able to throw an exception error when it finds the duplicate key. If at all possible throw the exception error message and highlight what that duplicate key is. Also if possible make the System.Collection library not skip the duplicate key or ignore the duplicate key.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • How do you read `appSettings`? use `ConfigurationManager`? It may use the last duplicate setting value, you should check it. – WAKU Jun 29 '18 at 03:06
  • what should happen when a value with an existing key is entered by the user ? Erase the existing one or prevent its creation ? – Spotted Jun 29 '18 at 11:39
  • Hi @Benny, did you have a chance to look at the below suggestion? – Johan Aspeling Jul 02 '18 at 07:20
  • Please swap the text as images here for formatted text. Images are not compatible with clipboards, screen-readers and search engines, and so make it harder to help you and future readers. – halfer Jul 03 '18 at 06:33

1 Answers1

0

You can use the following code to find duplicate keys:

// Cast the DataTable rows to a list, and select the 'Key' column's Value
var keys = items.Rows.Cast<DataRow>().ToList().Select(r => r["Key"]);

// Find the Duplicate keys
var duplicateKeys = keys.GroupBy(x => x)
            .Where(group => group.Count() > 1)
            .Select(group => group.Key);

// Throw the exception, containing the keys that are duplicated.
if (duplicateKeys.Count() > 0) {
    throw new ConfigurationErrorsException($"Duplicate key(s) found: '{string.Join(", ", duplicateKeys)}' - please revise");
}

The way you have to find the keys from the DataTable might differ, but it boils down to the same situation.

EDIT: This approach will replace all three for loops in your example.

For versions of Visual Studion below 2015:

    throw new ConfigurationErrorsException("Duplicate key(s) found: " + string.Join(", ", duplicateKeys) + " - please revise.");
Johan Aspeling
  • 765
  • 1
  • 13
  • 38
  • Please note I am using String interpolation to insert a variable in a string when the Exception is thrown. This is only available from VS2105 and above, if you do not have that, you can use the edited approach. – Johan Aspeling Jun 29 '18 at 10:00
  • Also note this approach will replace all three for loops in your example. – Johan Aspeling Jun 29 '18 at 10:08
  • 1
    ^ Johan, your additional helpful remarks probably belong in the answer itself. – halfer Jul 03 '18 at 06:31