0

My app config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="Database" type="Database_Server_Settings.Database_Server_SettingsConfigSection, BMS Internal Database Restore" />
  </configSections>
  <Database>
    <Databases>
      <add id="BMSCarlyle" Server_Name="bmslondon01" Database_Name="Everest_Carlyle" Database_Backup_Location="D:\SQL Backups\Carlyle\Everest_Carlyle.bak" TradeDesk_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle.mdf" TradeDesk_Log_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle_log.ldf" />
      <add id="bmsnim" Server_Name="bmslondon01" Database_Name="Everst_Nim" Database_Backup_Location="D:\SQL Backups\Carlyle\Everest_Carlyle.bak" TradeDesk_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle.mdf" TradeDesk_Log_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle_log.ldf" />
    </Databases>
  </Database>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

Now when I try and retrieve it like this

DBID.Items.Add(section.DatabaseSettings["BMSCarlyle"].id)

I retrieve the ID absolutely fine.

However, I want to add all the Ids not just BMSCarlyle to my list therefore

DBID.Items.Add(section.DatabaseSettings[].id)

However, this doesn't appear to work. Can anyone please suggest what I need to put between the "[]"?

Dave here is the handler code

namespace Database_Server_Settings
{
    public class Database_Server_SettingsConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("Databases")]
        public DatabaseCollection DatabaseSettings
        {
            get
            {
                return ((DatabaseCollection)(base["Databases"]));
            }
        }
    }

    [ConfigurationCollection( typeof (DatabaseElement))]
    public class DatabaseCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DatabaseElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DatabaseElement)(element)).id;
        }

        public DatabaseElement this [string idx]
        {
            get
            {
                return (DatabaseElement)BaseGet(idx);
            }
        }
    }

    public class DatabaseElement : ConfigurationElement
    {
        [ConfigurationProperty("id", DefaultValue="", IsKey=true, IsRequired=true)]
        public string id
        {
            get
            {
                return ((string)(base["id"]));
            }
            set
            {
                base["id"] = value;
            }
        }
        [ConfigurationProperty("Server_Name", DefaultValue="", IsKey=false, IsRequired=false)]
        public string Server_Name
        {
            get
            {
                return ((string)(base["Server_Name"]));
            }
            set
            {
                base["Server_Name"] = value;
            }
        }
        [ConfigurationProperty("Database_Name", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Database_Name
        {
            get
            {
                return ((string)(base["Database_Name"]));
            }
            set
            {
                base["Database_Name"] = value;
            }
        }
        [ConfigurationProperty("Database_Backup_Location", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Database_Backup_Location
        {
            get
            {
                return ((string)(base["Database_Backup_Location"]));
            }
            set
            {
                base["Database_Backup_Location"] = value;
            }
        }
        [ConfigurationProperty("TradeDesk_Location", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string TradeDesk_Location
        {
            get
            {
                return ((string)(base["TradeDesk_Location"]));
            }
            set
            {
                base["TradeDesk_Location"] = value;
            }
        }
        [ConfigurationProperty("TradeDesk_Log_Location", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string TradeDesk_Log_Location
        {
            get
            {
                return ((string)(base["TradeDesk_Log_Location"]));
            }
            set
            {
                base["TradeDesk_Log_Location"] = value;
            }
        }
    }
}
4
  • Can you show the code for the Database_Server_Settings.Database_Server_SettingsConfigSection hander? Commented Aug 27, 2014 at 17:22
  • The only way is to loop through all the items of DatabaseSettings. Commented Aug 27, 2014 at 17:29
  • Hi Guanxi, sorry I'm being stupid how would I loop through them? Commented Aug 28, 2014 at 5:37
  • Also @Guanxi is it possible to loop through databasesettings when the key is a string or do I need to have a int key? Commented Aug 28, 2014 at 5:47

1 Answer 1

1

See if you have DBID.Items.AddRange method then first example otherwise second option-

DBID.Items.AddRange(section.DatabaseSettings.Select(p=>p.id).ToList());

or

foreach(var item in section.DatabaseSettings.Select(p=>p.id))
{
DBID.Items.Add(item);
}



[ConfigurationCollection( typeof (DatabaseElement))]
    public class DatabaseCollection : ConfigurationElementCollection, IEnumerable<DatabaseElement>
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DatabaseElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DatabaseElement)(element)).id;
        }

        public DatabaseElement this [string idx]
        {
            get
            {
                return (DatabaseElement)BaseGet(idx);
            }
        }

    #region IEnumerable<DatabaseElement> Members


        public new IEnumerator<DatabaseElement> GetEnumerator()
        {
            int count = base.Count;
            for (int i = 0; i < count; i++)
            {
                yield return base.BaseGet(i) as DatabaseElement;
            }
        }

        #endregion
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Hi San, Neither of those work. It says Error 21 'Database_Server_Settings.DatabaseCollection' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'Database_Server_Settings.DatabaseCollection' could be found (are you missing a using directive or an assembly reference?)
add using System.Linq; and try. hope this will help.
Tried that, I'm still getting the same errors. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; using BMS_Internal_Database_Restore;
I have implemented IEnumerable<DatabaseElement>, it should work now. See the modified code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.