Changing membership connection string
.net, c#, connection-string, sqlmembershipprovider
Solution
What i have also found on the net about problem is this here:
A simpler, albeit somewhat eyebrow-raising solution is just modifying the connection string in the providers early enough in the request's lifecycle:
private void SetProviderConnectionString(string connectionString)
{
var connectionStringField =
Membership.Provider.GetType().GetField("_sqlConnectionString",
BindingFlags.Instance | BindingFlags.NonPublic);
if (connectionStringField != null)
connectionStringField.SetValue(Membership.Provider, connectionString);
}
Calling this method from Global.asax.cs inside Application_PreRequestHandlerExecute does the job. Haven't tested it too much, but even if something doesn't work, it just means it needs to be done earlier. No guarantees this will work with future versions of the framework, although most likely it will.
So, the 'SetProviderConnectionString' method can be called manually (after the Initialize method is finished), instead of expecting the framework to call the overwritten Initialize method when the Membership.Provider is referenced for a first time.
Problem
Iam new to asp.net membership & I need help to change its connection string programmatically. What I have tried till now is I have create a class project name Sample as namespace** and extends the `System.Web.Security.SqlMembershipProvider` code as ``` namespace Sample { public class Connectionstring : SqlMembershipProvider { public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) { string connectionString = "server=xx.xx.xx;database=db;user id=un;password=pwd"; // Set private property of Membership provider. FieldInfo connectionStringField = GetType().BaseType .GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic); connectionStringField.SetValue(this, connectionString); } } } ``` and altered web config file in membership tag as ``` <membership defaultProvider="SQLMembershipProvider"> <providers> <add name="SQLMembershipProvider" type="sample.Connectionstring,sample" connectionStringName="SQLMembershipConnString" applicationName="@@@@@@@" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" /> </providers> </membership> ``` and while running the web application project the connection string which i am changing is not get altered? waiting for your valuable responses and comments