System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code - asp.net-mvc-4

I am using Visual studio 2012 MVC 4 connecting to the database SQL SERVER Management Studio 2012.
I am getting below exception
System.Data.Entity.Core.EntityException occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: The underlying provider failed on Open.
public Actionresult Details(int id) {EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(x => x.EmployeeId == id); return View(employee);}
Also sometimes i get following exception while using connection string
<connectionStrings> <add name="EmployeeContext" connectionString="server=.; database=Sample; integrated security=SSPI"
providerName="System.Data.SqlClient"/>
System.Data.SqlClient.SqlException : Cannot open database "Sample" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\SYSTEM'.

Related

Failing VB.NET OleDbConnection. SQL Server does not exist or access denied

Realise this has been asked before but i have been unable to find a solution that works in this case.
I am trying to connect to a sql database over a network. The server is 2008 and the application is VB.Net (4).
The offending script is;
Dim strConnection As String = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=XXXX;Password=XXXX;Initial Catalog=MBB;Data Source=NETPC\SQLEXPRESS"
con = New OleDbConnection(strConnection)
con.Open()
The user id and password connect and has write permission to the db
using SQL Management Studio.
I have tested the connection string using the .udl quick test where
it worked fine and matched the string above.
The db is set up to accept both windows & sql server (password
protected) authentication.
Actual error is;
An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll Additional information:
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or
access denied.

How to solve "Login failed for user 'domain\Username'" in SQL Server 2008 R2

I have a website where I need to fetch data from a SQL Server 2008 R2 database. But I'm getting an error
Login failed for user 'domain\Username'
I googled for it then I get that to change property of SQL Server -> Properties -> Security -> Server authentication to SQL Server and Windows authentication mode only I'm still getting the same error. I don't want to use ID and password in connection string
My connection string:
<add name="ConnectionString"
connectionString="Data Source=servername;Initial Catalog=databasename;Integrated Security=True"
providerName="System.Data.SqlClient" />
Please help me.
add user id and passsword
or
1) Go to Server Explorer tab on VS
2) In Data Connections right click (click on properties) on your db
3) copy Connection String

Database connectionstring windows authentication but passing user and password

I need some help.
I'm trying to connect to my database.
This is my connectionstring :
<add name="DefaultConnection" connectionString="Data Source=KBV-SQL12-TEST\KBSQLSPTEST; Initial Catalog=KBSPT_aspnetmembership; User Id=KBOVDM01\KBSPT_EXTRANET; Password=SPExtr#n3t" providerName="System.Data.SqlClient" />
The user is an Active Directory user.
When I go to my SQLServer and log on as KBSPT_EXTRANET, I can op the SQL Management Studio and connect to the database.
But in my application when I use the connectionstring mentionned above, I keep getting the message "Underlying provider failed on open".
What am I doing wrong?
You are trying to use a Windows account when the connection string is expecting a SQL Server user account.

Sql Exception 0X80131904 Login failed for user ". The user is not associated with a trusted sql server connection

I'm developing a windows application using c# .Net Framework 4.
I have 2 machines; Test and production, connecting to a remote server which has the SQL Server here's my connection string:
<add name="MealConnectionString"
connectionString="Server=MySQLServer;Database=MEALDB;User Id=Meal;Password=Meal1;Trusted_Connection=True;"
providerName="System.Data.SqlClient" />`
When I run the program from the test machine there's no problem, but when I publish the program and run it from the production machine it gives the error mentioned in the title.
When I remove the trusted connection it gives me a different error: Login failed for user "Meal"
do you have any idea what could be the problem ??
Latest Update :
I tried the sa Sqlaccount and removed the trusted connection and it worked , when i try the same for the user meal it doesn't work whereas the MEAL suer has the same privileges as the sa account
I did it I just created a new sql account and gave it a db_owner permission over the database and it worked.

Entity Exception : the underlying provider failed to open

iv'e got a wcf service host hosting a service on IIS7,
iv'e added a database to it's App_Data folder ,
the service is referenced to a DAL project
which holds an Entity Framework model generated from my DB ( The DB from the WCF Service Host )
i keep getting the above entity exception with this inner message :
{"An attempt to attach an auto-named database for file C:\\Users\\eranot65\\Documents\\Visual Studio 2010\\Projects\\CustomsManager\\WcfManagerServiceHost\\App_Data\\CustomesDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}
iv'e copied the connection string from DAL/app.config to WcfManagerServiceHost/Web.config
add name="CustomesDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\eranot65\Documents\Visual Studio 2010\Projects\CustomsManager\WcfManagerServiceHost\App_Data\CustomesDB.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient"
this happens when i try to use my data source entity model:
public List<Employee> GetEmployees()
{
List<Employee> employees = null;
using (CustomesDBEntities entites = new CustomesDBEntities())
{
employees = entites.Employees.ToList<Employee>();
}
return employees;
}
it doesn't seem as if the DB is in use some where else ,
(1) how can i check if some other process is holding a handle to my DB ?
(2) in ideas this happens ?
I would consider checking one of two things:
Create a connection to your SQL Express, either in VS server explorer, or by using the SQL management studio, and verify you do not already have a database by that name attached to your server.
Move your project from it's current location to somewhere on the disk which is not user-specific (meaning not on the desktop, documents etc..), for example - c:\temp, c:\projects... The reason for that is that you are running a web application, and in case you run it in IIS, the identity of the worker process is a special identity other than yours which might not have permissions to access the database file since it is located in a private folder of your user
Most likely the problem is that you are opening the database with Visual Studio and your application at the same time. The connection string explicitely configures AttachDbFilename=... AttachDBFilename spins up a user instance of SQL Express attached to a specific DB Filename for single user mode. In single user mode, only one application can open the MDF at a time.
well the answer is always simpler then you would think
all i ended up doing is changing to automatically generated connection string
generated by the EntityFramework model , to a connection string to locate the DB in my App_Data folder
my original connection string :
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='Data
Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\eranot65\Documents\Visual Studio 2010\Projects\CustomsManager\WcfManagerServiceHost\App_Data\CustomesDB.mdf"
;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient"
my edited connection string :
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;
provider connection string='Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CustomesDB.mdf;Integrated Security=True;;User Instance=True;MultipleActiveResultSets=True'"