Connect to SQL Server database using Asp.net MVC - sql

I'm starting with Asp.net MVC. When connect to SQL Server database then error.
This is <connectionStrings>
<add name="SGTSVNDBContext" connectionString="Data Source=DESKTOP-ABKAVRM\SQLEXPRESS;Initial Catalog=SGTSVNDBAUTH_CusCloud;Persist Security Info=True;User ID=sa;Password=;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
This is <appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="DefaultPassword" value="123456" />
<add key="CustomerKey" value="YN8HgQuslP7kHfMeJk0NyHbu1R2IyHo4neUWmIHlffA=" />
<add key="GCM-SENDER-ID" value="86033830857" />
<add key="AUTH-TOKEN" value="AIzaSyB3FLcWcrRlOcu_zyCdl6tMawlvSoX4MeQ" />
<add key="FCM-SENDER-ID" value="122441061332" />
<add key="FCM-SERVER-KEY" value="AAAAHIIOU9Q:APA91bGWrJHwfJKiZsRGs8V62_xhuK9x0AWDNewrEnQiPrsrIpZ2YVYKop4Z44LSdNv_iZcSFmHpQ8YxS-WrTniZ5cZMe5T85PKJzv-CTNjTikwc7rqwoIXU--Ssk84IdmBzM8XcYuBb" />
<!--<add key="BackupPath" value="\\192.168.2.10\SGVN-Server\Scan folder\Phat\" />-->
<add key="BackupPath" value="D:\Mine\SystemGear\SGVN_SGF #07_2015\Project\Sites\WebAPI\Database_Backup\" />
<add key="SqlInstanceName" value="SGSOFT09-V2" />
<add key="BackupDBName" value="SGTSVNDBAUTH_CusCloud" />
<add key="SqlUserName" value="sa" />
<add key="SqlPassword" value="" />
When build project then error as the this

You have no password in your connection string, therefore it is failing to login to the database.

If you are sure the SQL Server is in "mixed mode", and that the password is correct for the "sa" account, I can only suggest it to be that the "sa" user doesn't have access to the named database.
In SQL Management Studio, expand the database branch, then security, then users, and check if the "sa" user appears in the list.
The best way to diagnose this error is to try and connect to the database server using SQL Management studio, logging in AS the user in the connection string.
If you can log in, see if you can see your target database in the tree on the left.
Try it with SQL Management Studio from the same machine you're code is running and it should give you more information about what's wrong.
Try changing your connection string to the following:
If the connection string is correct then check for the key value of connection string used in entity framework class.

Related

How to host the core project with IIS using mdf file

I have tried to host the core project with mdf file in the IIS server. The below webconfig file works fine in the localhost. But when I publish it to IIS, I got a 500-internal server error.
This is the webconfig:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.entit, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="FileManagerConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|App_Data\FileManager.mdf;Integrated Security=True;Connect Timeout=30" />
</connectionStrings>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824"></requestLimits>
</requestFiltering>
</security>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" arguments="%LAUNCHER_ARGS%">
<environmentVariables />
</aspNetCore>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
LocalDB is a database instance provided by the Visual studio. In order to make it work in IIS properly, we should start the Visual Studio and change the application pool identity to the specific account that running the Visual studio. But this is not a common way to publish the database, we should add database immigration to the current project so that we publish the database to the remote database server.
Here is an example of database immigration from localdb to a remote SQL server.
How to transfer ASP.NET MVC Database from LocalDb to SQL Server?
Feel free to let me know if there is anything I can help with.
The .mdf files are SQL Server database files. No other application can understand, read, or update those files. So, I have installed the SQL server in my machine and then host the core project in IIS.

IIS Application is unable to connect to SQL Server database but works when testing in Visual Studio

My web app is supposed to be a drop down menu that connects to my SQL Server database and gives me list of choices. When running it through Visual Studio, everything works fine. A list of choices is shown after clicking on the drop down menu.
However after publishing it to IIS, the drop down menu appears blank. I've tried adding NT AUTHORITY\NETWORK SERVICEuser in the SQL Server database. And setting the application pool identity to NETWORK SERVICE as well but it still doesn't work. I also made sure that the IIS and the application pool has proper access to the SQL database, and pretty much whatever solution I can google but nothing seems to work.
This is the connection string in my web.config, I've changed the actual names of the databases and password for security sake.
<connectionStrings>
<!--<add name="Database1"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=Catalog1;Integrated Security=True;Pooling=False"
providerName="System.Data.SqlClient" />-->
<!--<add name="Database1"
connectionString="Data Source=MyBD;Initial Catalog=Catalog1;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=P#ssw0rd;"
providerName="System.Data.SqlClient" />
<add name="Database2"
connectionString="Data Source=MyBD;Initial Catalog=Catalog2;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=P#ssw0rd;"
providerName="System.Data.SqlClient" />
<add name="Database3"
connectionString="Data Source=MyBD;Initial Catalog=Catalog3;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=P#ssw0rd;"
providerName="System.Data.SqlClient" />-->
<add name="Database1"
connectionString="Data Source=MyBD;Initial Catalog=Catalog1;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=P#ssw0rd;"
providerName="System.Data.SqlClient" />
<add name="Database2"
connectionString="Data Source=MyBD;Initial Catalog=Catalog2;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=P#ssw0rd;"
providerName="System.Data.SqlClient" />
<add name="Database3"
connectionString="Data Source=MyBD;Initial Catalog=Catalog3;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=P#ssw0rd;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I'm not sure if it's a problem with my connection strings, or that I'm not publishing the website properly.

ASP.net Identity - Login/Add User Duplicate Database

Have this web-forms using ASP.Net Identity register form,
problem occurs each time I try to login / register new user, error: "Cannot create file 'D:\WebSite1\App_Data\Database.mdf' because it already exists."
Seems like the Identity system try to create new DataBase every time, instead of using the created one at the first user creation.
From web.config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=(localdb)\v11.0; AttachDbFileName=|DataDirectory|\Database.mdf; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<connectionStrings>
<add name="db_mainConnectionString" connectionString="Data Source=(localdb)\v11.0; AttachDbFileName=|DataDirectory|\Database.mdf; Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
<sessionState mode="Custom" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="db_mainConnectionString" />
</providers>
</sessionState>
The problem seems to be with the DatabaseInitializer strategy being used along with the Asp.net identity providers. If you're using code first then it creates your database automatically based on DatabaseInitializer Strategy specified. This is the reason why the application is creating the database every time you're running the applicaion.
see this article for more details over DatabaseInitializer.
You can get rid of this problem by enabling database migrations. Migrations allows you to only propagate the changes in the schema to database rather than droping and creating database every time new changes in the Models are detected.

Can't connect to SQL in web.config

This is my web.config SQL:
<connectionStrings>
<add
name="UsersDB"
connectionString="Server=MYSQL5008.myWindowsHosting.com;Database=db_9b443f_users;Uid=9b443f_users;Pwd=YOUR_DB_PASSWORD;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<membership defaultProvider="SqlProvider"
userIsOnlineTimeWindow="15">
<providers>
<clear />
<add
name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="UsersDB"
applicationName="MyApplication"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
requiresUniqueEmail="true"
passwordFormat="Hashed" />
</providers>
</membership>
<compilation debug="true" targetFramework="4.0" />
</system.web>
Can you tell me what's wrong here please? Thanks!!
And don't mind the 'YOUR_DB_PASSWORD', I just changed it so I can show you the code..
Given that your appear to be connecting to an external server
MYSQL5008.myWindowsHosting.com
ensure that there is an appropriate firewall rule in place to allow the SQL server connection through. The default port is 1433.
There are quite a few other configuration items to check in general, but since this appears to be a hosted SQL solution the firewall is by far the most likely.
UPDATE
As noted by #Steve, is this a SQL Server or MySQL database? If MySQL, you need to change the provider name in your configuration. See
http://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-asp-roles.html
for details.

ApplicationName is not stored in database using SimpleMembership provider

I have tried using SqlMembershipProvider and SimpleMembershipProvider with application name set in web.config file as shown below. I'm able to register new accounts and login but application name is not getting stored in database table aspnet_Applications.
<roleManager enabled="true" defaultProvider="SqlRoleProvider">
<providers >
<clear />
<add name="SqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="DefaultConnection"
applicationName="BasicCloudProject" />
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider" >
<providers>
<clear />
<add
name="SimpleMembershipProvider"
type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"
connectionStringName="DefaultConnection"
applicationName="BasicCloudProject"
/>
</providers>
</membership>
I have set up MVC4 web role in Azure project with MS SQL database having tables as per aspnet_regsql.exe.
The SimpleMembershipProvider does not use the traditional ASP.NET membership database, which you are referring to when you mention the table aspnet_Applications. It has a completely different database schema, which does not include the concept of Applications. If you want to use this to create a multi-tenant application take a look at this QA.