user2962142 user2962142 - 23 days ago 7
ASP.NET (C#) Question

WebSecurity.InitializeDatabaseConnection Function does not found my connection String

So i get the error that i should call WebSecurity.InitializeDatabaseConnection
before i call any function of the webSecurity Class

So i created _AppStart.cshtml and placed this code in it :

@using System.Configuration;
@{
string connString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
WebSecurity.InitializeDatabaseConnection(connString, "users", "id", "email", autoCreateTables: true);

}


and in Web.Config i got

<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>

<connectionStrings>
<add name="conString" providerName="System.Data.SqlClient"
connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=sales;Integrated Security=True;Pooling=False"/>
</connectionStrings>


now i get this error from the InitializeDatabaseConncection method :

Connection string "Data Source=(local)\SQLEXPRESS;Initial Catalog=sales;Integrated Security=True;Pooling=False" was not found.

whats the problem ?

Answer

WebSecurity.InitializeDatabaseConnection is not asking you for a connection string. It's asking you for the name of the connection string. It will then retrieve the connection string form the configuration file for you.

WebSecurity.InitializeDatabaseConnection("conString", "users", "id", "email", autoCreateTables: true);

When you have a question about why a method is not behaving as expected, read the documentation! It states for that parameter:

The name of the connection string for the database that contains user information. If you are using SQL Server Compact, this can be the name of the database file (.sdf file) without the .sdf file name extension.

Comments