Microsoft KB Archive/321585

= How to use custom configuration data in a Visual Basic .NET component =

Article ID: 321585

Article Last Modified on 4/16/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q321585



SUMMARY
This step-by-step article describes how to use custom configuration files to store the configuration data for your Microsoft Visual Basic .NET components and how to access the custom configuration file in your application.

Your application may have to access a number of .NET components. Your components also require some configuration data such as database connection strings. This configuration data is stored in a custom configuration file.

Create the component
To create a component by using Visual Basic .NET:  Create a new Visual Basic .NET Class Library project named MyComponent. By default, Class1.vb is created.  Add the following code to the Namespace declaration section of Class1.vb. Imports System.Data.SqlClient   Add the following code to the Class1 class. ' Field to hold SQL connection. Private m_SqlConnection As SqlConnection

' This function opens a connection to the database. Public Function OpenConnection(ByVal dataSource As String, ByVal initialCatalog As String, ByVal userId As String) As Boolean Try ' Make the connection string. Dim strConnection As String = &quot;data source=&quot; + dataSource + &quot;;initial catalog=&quot; + initialCatalog + &quot;;user id=&quot; + userId m_SqlConnection = New SqlConnection(strConnection) ' Open the connection. m_SqlConnection.Open

' Do other database operation.

' Return success. Return True Catch ' In case of an error, close the connection. If m_SqlConnection Is Nothing Then m_SqlConnection.Dispose m_SqlConnection = Nothing End If        ' Return failure. Return False End Try End Function  On theBuild menu, click Build Solution to create MyComponent.dll.

Create the test application
To create a console application to test the component that you just created:  Create a new Visual Basic .NET Console Application project. By default, Module1.vb is created. In Solution Explorer, right-click References, and then click Add Reference.</li> Click Browse, and then locate and select MyComponent.dll.</li> Click OK.</li>  Add the following code to the Namespace declaration section of Module1.vb. Imports System.Reflection Imports System.IO Imports System.Configuration Imports System.Xml Imports System.Collections.Specialized </li>  Replace the existing Main method with the following code. Sub Main

Dim objMyCOmponent As MyComponent.Class1 objMyCOmponent = New MyComponent.Class1

' The following code shows how to read the configuration file. Dim codeBase As String = [Assembly].GetExecutingAssembly.CodeBase Dim codeBaseDir As String = Path.GetDirectoryName(codeBase) Dim configFilename As String = Path.Combine(codeBaseDir, &quot;CustomConfigFile.config&quot;)

' Load the configuration file in XML document. Dim doc As XmlDocument = New XmlDocument doc.Load(configFilename)

' Get the databaseconnection node. Dim xNode As XmlNode = doc.GetElementsByTagName(&quot;databaseconnection&quot;).Item(0)

' Use the NameValueSectionHandler class to get the actual databaseconnection setting. Dim csh As IConfigurationSectionHandler = New NameValueSectionHandler Dim nvc As NameValueCollection = CType(csh.Create(Nothing, Nothing, xNode), NameValueCollection)

' Call the method in Component. Dim blnStatus As Boolean blnStatus = objMyCOmponent.OpenConnection(nvc(&quot;data source&quot;), nvc(&quot;initial catalog&quot;), nvc(&quot;user id&quot;))

' Display results. If blnStatus = True Then Console.WriteLine(&quot;Database connection was success fully opened.&quot;) Else Console.WriteLine(&quot;Database connection was not opened.&quot;) End If

End Sub </li> On the Build menu, click Build Solution to create the console application executable file.</li></ol>

Create the configuration file and run the application
To create the configuration file for the component:   In Notepad or in another text editor, type the following text. <add key=&quot;data source&quot; value=&quot;[SQL Server Name]&quot; /> <add key=&quot;initial catalog&quot; value=&quot;pubs&quot; /> <add key=&quot;user id&quot; value=&quot;sa&quot; /> </li> Replace [SQL Server Name] with the name of your instance of Microsoft SQL Server.</li> Add additional parameters, such as password information, according to your requirements.</li> Save the text file as CustomConfigFile.config in the folder where your Console Application executable file resides.

Important The configuration file contains user credentials. For the security reasons, we recommend that you do the following to help protect the configuration file: <ul> Configure the user account for which you want to grant permissions in the Group or user names section.</li> Grant related permissions in the Properties dialog box for the configuration file. You can do this in the Permissions for Administrator section on the Security tab.</li></ul> </li> Run the console application and verify the results.</li></ol>

You receive the following message:

Database connection was success fully opened.

Note that you can also access the custom configuration file in other types of applications.

<div class="references_section">