Cannot connect to database mvc - sql

I cant connect to database, working on new pc.
Here are params with which i access my sql database :
My connection string in Web.config is :
<connectionStrings>
<add name="DefaultConnection" connectionString="data source=DMITRIJSS-PC\SQLEXPRESS;initial catalog=HRM_MVC_DEV_Test;user id=BTG\DmitrijsS;password=;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
<add name="HRMEntities" connectionString="metadata=res://*/HRMModel.csdl|res://*/HRMModel.ssdl|res://*/HRMModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DMITRIJSS-PC\SQLEXPRESS;initial catalog=HRM_MVC_DEV_Test;user id=BTG\DmitrijsS;password=;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
When I am trying to access db it throws window where I have to write password, when I leave it blank -
Login failed for user BTG\DmitrijsS

<add name="DefaultConnection" connectionString="data source=DMITRIJSS-PC\SQLEXPRESS;initial catalog=HRM_MVC_DEV_Test;Integrated Security=true;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
<add name="HRMEntities" connectionString="metadata=res://*/HRMModel.csdl|res://*/HRMModel.ssdl|res://*/HRMModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DMITRIJSS-PC\SQLEXPRESS;initial catalog=HRM_MVC_DEV_Test;Integrated Security=true;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />

Set up environment variables.
https://msdn.microsoft.com/en-IN/library/ms365307.aspx

Related

what would be the connection string for below in web.config?

I have written something like this but i am getting error as invalid username.
any suggestions?
You can simply set Connection String in the Web Config file and just reference it based on your requirements.. see below
Web Config:
<connectionStrings>
<add name="SQLDBConnectionString" connectionString="Data Source=<SERVER NAME>;Initial Catalog=<DATABASE NAME>;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then to get reference with the connections:
public string SQLServerDBConnstring()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["SQLDBConnectionString"].ToString();
}

The connection string 'ClinlabEntities' in the application's configuration file does not contain the required providerName attribute."

I am using IIS 8 and MVC4 Application I am having an error..
Thank You for your Help.
<connectionStrings>
<add name="ClinlabConnectionString1" connectionString="Data Source=.\sql2012;Initial Catalog=Clinlab;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The connection string in the message error ClinlabEntities is not the same that you posted ClinlabConnectionString1, you should look for ClinlabEntities connection string and add the providerName attribute.

MVC Application is looking for credentials in the wrong database

I have an aspnetservicesdb database that is used for storing user profiles. I have deployed a new asp.net mvc application. The application uses a local sqlexpress database in the appdata directory for membership/profile purposes.
I do not want the application to use the sqlexpress database.
I've removed the connection string from the web.config and AuthConfig.RegisterAuth(); from global.asax
So far, this has worked. I'm able to use User.Identity.Name in the controller succesfully. However, when I tried to use User.IsInRole in the view, I got a sql server not found error, becuase the local sqlexpress db doesn't exist.
How do I tell the application to use my existing database and stop looking for the sqlexpress db?
edit - here are the connection strings
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=removed;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|removed" providerName="System.Data.SqlClient"/>-->
<add name="ApplicationServices" connectionString="Data Source=removed;Initial Catalog=aspservicesdb;UID=removed;PWD=removed" providerName="System.Data.SqlClient" />
edit - here are other membership portions from web config
<authentication mode="Forms">
<forms loginUrl="~/login.aspx" timeout="2880" />
</authentication>
<membership userIsOnlineTimeWindow="480">
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="12" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
You're not telling asp.net to use the membership provider. That should look like this:
<membership defaultProvider="AspNetSqlMembershipProvider" userIsOnlineTimeWindow="480">
And, since you're getting membership some other way, that tells me you're probably using SimpleMembership, which means you need to remove the SimpleMembership code from your application (including the user data context, and other attributes)
It turns out I hadn't added these sections to the web config...
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/"
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
The issue appears to be resolved.

How do I comment out existing database connections & then create a new database connection in the web.config using vb.net?

Basically I have a web.config file that has several database connections in it in both the "connectionStrings" & "appSettings" sections. See example below.
<connectionStrings>
<!-- Connection String for SQL Server 2005 Express -->
<add
name="SiteSqlServer"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"
providerName="System.Data.SqlClient" />
<!-- Connection String for SQL Server 2000/2005
<add
name="SiteSqlServer"
connectionString="Server=(local);Database=DotNetNuke;uid=;pwd=;"
providerName="System.Data.SqlClient" /> -->
</connectionStrings>
<appSettings>
<!-- Connection String for SQL Server 2005 Express - kept for backwards compatability - legacy modules -->
<add key="SiteSqlServer" value="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"/>
<!-- Connection String for SQL Server 2000/2005 - kept for backwards compatability - legacy modules
<add key="SiteSqlServer" value="Server=(local);Database=DotNetNuke;uid=;pwd=;"/>
-->
<add key="InstallTemplate" value="DotNetNuke.install.config" />
<add key="AutoUpgrade" value="true" />
<add key="UseInstallWizard" value="true"/>
<add key="InstallMemberRole" value="true" />
<add key="ShowMissingKeys" value="false" />
<add key="EnableWebFarmSupport" value="false" />
<add key="EnableCachePersistence" value="false"/>
<add key="HostHeader" value="" />
<!-- Host Header to remove from URL so "www.mydomain.com/johndoe/Default.aspx" is treated as "www.mydomain.com/Default.aspx" -->
<add key="RemoveAngleBrackets" value="false" />
<!--optionally strip angle brackets on public login and registration screens-->
<add key="PersistentCookieTimeout" value="0" />
<!--use as persistent cookie expiration. Value is in minutes, and only active if a non-zero figure-->
<!-- set UsePortNumber to true to preserve the port number if you're using a port number other than 80 (the standard)
<add key="UsePortNumber" value="true" /> -->
</appSettings>
What I need to do is loop through each of the connections strings, comment them out & then add my new connection strings to both sections of the web.config file.
This is what I have so far - creating the new connection string is working but I am unsure how to loop through comment out any orig connection strings.
Dim Name As String = "SiteSqlServer"
Dim isNew As Boolean = False
Dim MyWebConfigPath As String = "C:\test\Web.Config"
Dim MyWebConfigFile As New XmlDocument()
MyWebConfigFile.Load(MyWebConfigPath)
Dim list As XmlNodeList = MyWebConfigFile.DocumentElement.SelectNodes(String.Format("connectionStrings/add[#name='{0}']", Name))
Dim node As XmlNode
isNew = list.Count = 0
If isNew = False Then
'Comment out existing connection settings
End If
node = MyWebConfigFile.CreateNode(XmlNodeType.Element, "add", Nothing)
Dim attribute As XmlAttribute = MyWebConfigFile.CreateAttribute("name")
attribute.Value = Name
node.Attributes.Append(attribute)
attribute = MyWebConfigFile.CreateAttribute("connectionString")
attribute.Value = "Data Source=CP5-1234\SQLEXPRESS;Initial Catalog=demo_db;User ID=demo_dbusr;Password=MyPass#1234"
node.Attributes.Append(attribute)
attribute = MyWebConfigFile.CreateAttribute("providerName")
attribute.Value = "System.Data.SqlClient"
node.Attributes.Append(attribute)
MyWebConfigFile.DocumentElement.SelectNodes("connectionStrings")(0).AppendChild(node)
MyWebConfigFile.Save(MyWebConfigPath)
This is what I would like to end up with:
<connectionStrings>
<!-- Connection String for SQL Server 2005 Express -->
<!-- REMOVED BY ME ON 6/6/2012
<add
name="SiteSqlServer"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"
providerName="System.Data.SqlClient" /> -->
<!-- Connection String for SQL Server 2000/2005
<add
name="SiteSqlServer"
connectionString="Server=(local);Database=DotNetNuke;uid=;pwd=;"
providerName="System.Data.SqlClient" /> -->
<add name="SiteSqlServer" connectionString="Data Source=CP5-1234\SQLEXPRESS;Initial Catalog=demo_db;User ID=demo_dbusr;Password=MyPass#1234" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<!-- Connection String for SQL Server 2005 Express - kept for backwards compatability - legacy modules -->
<!-- REMOVED BY ME ON 6/6/2012
<add key="SiteSqlServer" value="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"/> -->
<!-- Connection String for SQL Server 2000/2005 - kept for backwards compatability - legacy modules
<add key="SiteSqlServer" value="Server=(local);Database=DotNetNuke;uid=;pwd=;"/> -->
<add name="SiteSqlServer" connectionString="Data Source=CP5-1234\SQLEXPRESS;Initial Catalog=demo_db;User ID=demo_dbusr;Password=MyPass#1234" providerName="System.Data.SqlClient" />
<add key="InstallTemplate" value="DotNetNuke.install.config" />
<add key="AutoUpgrade" value="true" />
<add key="UseInstallWizard" value="true"/>
<add key="InstallMemberRole" value="true" />
<add key="ShowMissingKeys" value="false" />
<add key="EnableWebFarmSupport" value="false" />
<add key="EnableCachePersistence" value="false"/>
<add key="HostHeader" value="" />
<!-- Host Header to remove from URL so "www.mydomain.com/johndoe/Default.aspx" is treated as "www.mydomain.com/Default.aspx" -->
<add key="RemoveAngleBrackets" value="false" />
<!--optionally strip angle brackets on public login and registration screens-->
<add key="PersistentCookieTimeout" value="0" />
<!--use as persistent cookie expiration. Value is in minutes, and only active if a non-zero figure-->
<!-- set UsePortNumber to true to preserve the port number if you're using a port number other than 80 (the standard)
<add key="UsePortNumber" value="true" /> -->
</appSettings>

How to add Application name to <appSettings/>?

This is my app.config file looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Application Name" value="/MyApplication" />
</appSettings>
<connectionStrings>
<add name="frmStartup.My.MySettings.HDIMembershipProviderConnectionString"
connectionString="Data Source=.\sqlexpress;Initial Catalog=HDIMembershipProvider;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<membership defaultProvider="HDIMembershipProvider">
<providers>
<clear/>
<add name="HDIMembershipProvider" type="MyApplication.HDIMembershipProvider, MyApplication"/>
</providers>
</membership>
</system.web>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
</configuration>
I am trying to use HDI membership provider and this is my Users table structure:
And in my recent question asked here Oded helped me out trying to figure the problem with my Insert statement and I re-modified it and I have an ApplicationName column in my database structure I need to specify it.(As it should not be null value)
Now I need to add my application name by default to enter to database as we do it for web.config.
This is what I mean.
I need to add that MyApplication to my app.config file.
So How do I do that?
This is how I'm trying to enter user details to database but it is not enetering a single value
Try
Dim connectionString As String = "Data Source=.\sqlexpress;Initial Catalog=HDIMembershipProvider;Integrated Security=True"
Using cn As New SqlConnection(connectionString)
cn.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Users ( Username, Password, Email, PasswordQuestion, PasswordAnswer) VALUES(#Username,#Password,#Email,#PasswordQuestion,#PasswordAnswer)"
Dim param1 As New SqlParameter()
param1.ParameterName = "#Username"
param1.Value = txtUsername.Text.Trim()
cmd.Parameters.Add(param1)
Dim param2 As New SqlParameter()
param2.ParameterName = "#Password"
param2.Value = txtPassword.Text.Trim()
cmd.Parameters.Add(param2)
Dim param3 As New SqlParameter()
param3.ParameterName = "#Email"
param3.Value = txtEmail.Text.Trim()
cmd.Parameters.Add(param3)
Dim param4 As New SqlParameter()
param4.ParameterName = "#PasswordQuestion"
param4.Value = txtSecurityQuestion.Text.Trim()
cmd.Parameters.Add(param4)
Dim param5 As New SqlParameter()
param5.ParameterName = "#PasswordAnswer"
param5.Value = txtSecurityAnswer.Text.Trim()
cmd.Parameters.Add(param5)
cmd.Connection = cn
cmd.ExecuteNonQuery()
cn.Close()
End Using
Successlbl.show
Successlbl.show.Text = "Regisration Success."
Catch
Errolbl.Show()
Errolbl.Text = "Your account was not created.Please try again."
End Try
Can anyone point me out where I'm making mistake.
And this is the final result I'm getting while entering to database:
To be more short and clear I need to enter the above shown User details to my database using the HDI membership provider.
There is a better way by using the following steps:
1) Add a reference to System.Configuration to your application.
2) Add the following block to your app.config file, within the configuration section:
<appSettings>
<add key="ApplicationName" value="/MyApplication" />
</appSettings>
3) Retrieve the value and use it where needed with the following code (example from your answer shown):
param6.Value = System.Configuration.ConfigurationManager.AppSettings("ApplicationName")
Add a AppSettings section to your web.config
<appSettings>
<add key="ApplicationName" value="/website1" />
</appSettings>
I have figured out the problem Myself after a lot of hair pulling and found the solution that I have changed my code in this way:
cmd.CommandText = "INSERT INTO Users ( Username,ApplicationName,Password,
Email, PasswordQuestion, PasswordAnswer) VALUES(#Username,#ApplicationName,#Password,
#Email,#PasswordQuestion,#PasswordAnswer)"
And add another Param this way:
Dim param6 As New SqlParameter()
param6.ParameterName = "#ApplicationName"
param6.Value = txtApplicationName.Text.Trim()
cmd.Parameters.Add(param6)
I know this not the best way If anyone found any other best solution let me know.