Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ConnectionStringSettingsCollection.IndexOf Method

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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft