ConnectionStringSettingsCollection.IndexOf Method (ConnectionStringSettings)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns the collection index of the passed ConnectionStringSettings object.

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

public int IndexOf(
	ConnectionStringSettings settings
)

Parameters

settings
Type: System.Configuration.ConnectionStringSettings

A ConnectionStringSettings object in the collection.

Return Value

Type: System.Int32

The collection index of the specified ConnectionStringSettingsCollection object.

The following example shows how to get the index of the specified ConnectionStringSettings object.

static void GetIndex()
{

    try
    {
        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(
         ConfigurationUserLevel.None);

        // Clear the connection strings collection.
        ConnectionStringsSection csSection =
            config.ConnectionStrings;
        ConnectionStringSettingsCollection csCollection =
         csSection.ConnectionStrings;

        // Get the connection string setting element
        // with the specified name.
        ConnectionStringSettings cs =
            csCollection["ConnStr0"];

        // Get its index;
        int index = csCollection.IndexOf(cs);

        Console.WriteLine(
             "Connection string settings index: {0}", 
             index.ToString());

    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }
}

.NET Framework
Available since 2.0
Return to top
Show: