How to Use the Custom MembershipProvider ConnectionStringName
By Nannette Thacker
This is an issue that seems to confuse a lot of new developers. How do you use
the "connectionStringName" defined within the membership provider area of the web.config
file within a custom membership provider? Big mouthful, eh? Okay, let's break it
Within the web.config file, you may define your connection strings. For our example,
below I am setting up a connection string to a SQL Server database within my project's
App_Data folder, but it could be a connection string to a remote database on a database
server as well.
Integrated Security=True;User Instance=True"
In the above example, I have named my connection string
Now I want to setup my custom Membership Provider in the system.web section of my
description="Custom Membership Provider"
In the above example, I have inserted the connection string name within the "connectionStringName"
property. Note that if your custom membership provider is defined within a namespace,
to be sure to add that to the name of the provider itself. For instance, if your
namespace is "SSS" then you would add the namespace to the definition:
Now let's look at a few snippets from our custom membership provider class. In the
snippet below, notice I have defined the connection string variable.
Public Class SSSMembershipProvider
Public connStr As String
Typically, you may obtain the value of various configuration file settings with
the use of config():
However, we need to use the ConfigurationManager.ConnectionStrings Property to obtain
the configuration setting. Within the initialize function of the class I now retrieve
the value of the actual connection string that is associated with the name defined
in the web.config:
Public Overrides Sub Initialize(ByVal name As String, _
ByVal config As System.Collections.Specialized.NameValueCollection)
connStr = ConfigurationManager.ConnectionStrings(config("connectionStringName")).ConnectionString
The above is a very long line, and just in case it is cut off on the right, I will
break it on 2 lines so you don't miss it:
connStr = ConfigurationManager.ConnectionStrings(
Now you're all set to use this in your class functions for accessing the connection:
Using conn As New SqlConnection(connStr)
May your dreams be in ASP.NET!