I started to write project on asp.net and faced with trouble. When I try to execute this code:
System.Web.Security.Roles.CreateRole("User");
I get an exception:
An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnection object. The value that was provided for the connection string may be wrong, or it may contain an invalid syntax.
Parameter name: connectionString
That’s part of the configuration of my project:
<configuration>
<system.web>
...
<roleManager enabled="true" defaultProvider="SqlRoleProvider">
<providers>
<clear/>
<add name="SqlRoleProvider"
connectionString="ForumDB"
applicationName="/"
type="System.Web.Security.SqlRoleProvider"/>
</providers>
</roleManager>
</system.web>
<connectionStrings>
<clear/>
<add name="ForumDB"
connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=ForumDB;Integrated Security=True;Pooling=False"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
I initially thought that the connection string has been written wrong, but I checked several times. Also this code works:
var connectionString = ConfigurationManager.ConnectionStrings["ForumDB"];
DbProviderFactory providerFactory = DbProviderFactories.GetFactory(connectionString.ProviderName);
DbConnection connection = providerFactory.CreateConnection();
connection.ConnectionString = connectionString.ConnectionString;
connection.Open();
I am sure that the connection string has been written correctly. What is wrong?
I made a mistake here:
I had to write: