user2962142 user2962142 - 1 year ago 59
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

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

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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download