How to use ASP.NET Configuration on external SQL Server? - asp.net

In visual studio there is a nice possibility to manage users and memberships for an ASP.NET site. I moved the membership data from SQLEXPRESS to a normal SQL server. The website works fine, however how do i now manage my users/profiles/etc... like I was used to in visual studio? Is it possible to tell vs2010 to 'look' into the new sql server database in stead of the apsnetdb.mdf file?
Thanks,
Erik

This is how I do it:
In your web.config file, under the connectionStrings section, name your connection string, LocalSqlServer, i.e.
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Server=123.123.123.123;Database=dbName;User ID=dbuser;Password=dbPassword;Trusted_Connection=False;" />
</connectionStrings>
Your role and membership providers, if have the connectionStringName set, change them to LocalSqlServer otherwise, the default providers "should" automatically refer to LocalSqlServer anyway.
In VS2010, I then simply go to Project > ASP.NET Configuration which will start the tool to manage users, roles etc.
HTH

Related

How to link an SQL Database in Visual Studio to Razor cshtml?

I was following this tutorial
https://learn.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/displaying-data
but I am doing this in Visual Studio instead of WebMatrix since there is no more support for WebMatrix anymore. I tried creating a database within Visual Studio, but I'm not sure how to reference it with C#. Any help would be appreciated.
Also if possible link it to a Microsoft Access database.
If your DB is already configured on your project, you must first open the connection in your cshtml header or whatever
var db = Database.Open("DATABASENAME");
The Database must be connected with a few line in the Web.config page (This kind of informationS)
<connectionStrings>
<add name="DATABASENAME" connectionString="server=Servername;database=DATABASENAME;trusted_connection=True" providerName="System.Data.SqlClient" />
<add connectionString="Server=.\DATABASE STRING;Database=DATABASENAME;Trusted_Connection=True" name="DATABASE NAME" providerName="System.Data.SqlClient" />
</connectionStrings>

ASP.NET VB change db connection on web forms framework

I have created an instance of ASP.NET Web Forms in my Microsoft Visual Studio. By default the program generates user login page and registration which are linked to the .mdb file.
I want to reconfigure the standard connection and link it to my instance of MSSQL 2012 SQLExpress server.
How can I do it?
Ok so I will give some direction and link you to a page.
First let me ask you this the connection you are using presently is it in a web.config file. If so this is where you will change the server/instance and database/catalog name.
Web.Config modification for SQL Server Instance
or this link
Web Config modification for SQL Express 2
So if you in the web.config file you should see something similar to this.
<ConnectionStrings>
<add name="" connectionString="Server=(localhost)\SqlExpress;Max Pool Size=300;Initial Catalog=database;User ID=username;Password=password;" providerName="System.Data.SqlClient"/>
</Connectionstrings>
or
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= (LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\database.mdf;Initial Catalog=database;Integrated Security=True" providerName="System.Data.SqlClient" />
you need to change the follow:
Server or Data Source
and
Initial Catalog or AttachDbFilename
Now your credentials you may use any username and password that has access to the sql server express instance.
I hope this helps.

How will be visual studio deployment scnerio

I consumed about deployment for my projects. For example I am developing a asp.net web api or asp.net mvc project on visual studio. I use local database and local file usernames and password for ssl. I finish the development and publis it on my local iis server and see the working application. After local iis test, I load it to server but sometimes forget the change web.config settings, connectionstrings, ssl certificate paths and password. So the working server application that users accessed fails. How can I solve this problems?
What I do is I replace the configuration in the Web.Release.config using Transform
So if for example in my Web.config I have the following connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\sample.mdf;Initial Catalog=sample;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
In my Web.Replace.config I update the connectionString for production like this:
<connectionStrings xdt:Transform="Replace">
<add name="DefaultConnection" connectionString="SERVER_CONNECTION" providerName="System.Data.SqlClient" />
</connectionStrings>
Though keep in mind that the Transformation that I did in Web.Replace.config only applies when you publish your web app. You won't be able to test it locally by just running your application in Debug/Release mode
You should implement Web.Config transformation. Check this MSDN link for more information.
Just to give an overview - by implementing Web.config transformation, your project will have different Web.Config file (For example, Web.config, Web.Live.config, Web.PreProd.Config, etc), i.e., each different file for diffrent build configurations. The main web.config should remain as it is. Its just the other config files where you will need to specify the configurations which you want to overwrite. Once done, if you publish with a particular build configuration, then the newly generated web.config (in publish folder) should have all configurations from web.config + the differences you would have specified in the transformed file for that build configuration.

ASP.NET IDENTITY WEB FORMS

OK. I am experiencing something very strange with the asp.net identity in web forms. I setup the web.config file to establish connection to my local sqlexpress server. I previously created a database and ran the web application which uses asp.net identity and the 4.5 .net framework. The first time I ran the web app the tables were automatically created for me in the sql server database but then I noticed that in the APP_DATA folder under the solution there was another database created with a name aspnetdb.mdf
Although I have pointed out that the connection string connects to the sql server for some reason the web app connects to the newly created localdb database located in APP_DATA.
Am I doing something wrong here?
thanks
Oddly enough I have just come across this very issue in our codebase. One of my developers was using the 'old' Role provider to check if user was in a particular role. This was causing the app to create the database in App_Data as the config for all that still exists in machine.config. The connection string and provider details are specified in machine.config, so your web.config will only add or remove them. So for example if your machine.config says:
<connectionStrings>
<add name="LocalSqlServer" connectionString="..."/>
</connectionStrings>
And your web.config has this:
<connectionStrings>
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
Your application will be able to see and use both strings. To fix it you can clear the connection strings first like this:
<connectionStrings>
<clear />
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
But that still leaves the underlying problem that the old role (or profile) provider is being used in code. so you will instead get lots of errors when using it. You need to switch code like this:
if (User.IsInRole("Admin"))
{
//Do admin stuff
}
To something like this (in my case _userManager is of type UserManager<User> and is injected into my controller constructor at runtime.
var user = _userManager.FindById(User.Identity.GetUserId());
if (_userManager.IsInRole(user.Id, "Admin"))
{
//Do admin stuff
}

ASP.NET: How can I explore the default user database

So if I create a new MVC project with basic log in template, there should be somewhere a database which contains all the user login information. There's even the default connection string which points to it.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MyProjectName-20140521042155.mdf;Initial Catalog=aspnet-MyProjectName-20140521042155;Integrated Security=True" providerName="System.Data.SqlClient" />
How ever, when exploring the project folder I cannot find it. I also tried to turn on "show all files" option in the solution explorer.
Sql Server Management Studio can be used to connect to the local db server. You can also use visual studio server explorer.

Resources