Application throws error if I check the contents of database - c#

I seem to be having a problem with Entity Framework code-first. I managed to make it create the database in my project folder in App_data and everything works well if I do not try to check the contents of the generated database in the Server Explorer.
If I do that and try to open the application I get this error:
Cannot open database "SellCars" requested by the login. The login failed.
Login failed for user 'Aly-PC\Aly'.
While this behavior may be normal (not really sure), even if I detach the db and close the connection in the server explorer and I run this application it still does not work but instead in throws this error:
One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open database "SellCars" requested by the login. The login failed.
Login failed for user 'Aly-PC\Aly'.
Log file 'D:\Projects IDE\Visual Studio\MyWork\Websites\SellCars\SellCars\App_Data\SellCars.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously
This is my connection string:
<add name="CarsEntities"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='D:\Projects IDE\Visual Studio\MyWork\Websites\SellCars\SellCars\App_Data\SellCars.mdf';Initial Catalog=SellCars;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
I do not try to change anything inside of the database not even add any data but I still get these errors.
What should I do?

Add Aly-PC\Aly to the active directory users who can access the SQL server. Then add the SQL server login you just created to the database.

Related

Can't open databse requested by login, login failed, login failed for user

I have developed c# WPF application, I am using MDF file for database,
my connection string looks like :
Data
Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\appDataBase.mdf;Integrated
Security=True;Connect Timeout=30
here I am replacing |DataDirectory| with user's appdata folder.
the database file is in user's appdata folder, I created a setup which
copies .mdf file to _users appdata folder when I install it in
client's computer, it works perfectly
but when I uninstalls and
reinstall it, I am not able to connect that file, getting the error:
Can't open database
"C:/users/myusername/appdata/roaming/myapplication/database/appDataBase.mdf"
requested by login, login failed, login failed for user..
File is there, but somehow it is used by SQL server and can't
open it. also while starting application every time I am creating
backup of that file, that one also failing with error
System.IO.IOException: The process cannot access the file
'C:\Users\myusername\AppData\Roaming\myapplication\Database\appDataBase.mdf'
because it is being used by another process.
Can anybody give me exact solution, because i have to use localdb only on my client computer , i don't want them let to do any extra configuration for installation of my app, i.am still facing iasue after i changed my code to close db connection on main window closing event.
This type of things happens because one of your object which is using that database is still running in the background so please dispose all the database related instance also dispose and close all the database related object in catch().
Try catch will help you to detect exact line number where you are getting error and also show due to which object you are getting error.
If you want to check why this is happening then open task manager and look out for services there you definitely find service which is holding object for database.

First chance exception of 'System.Data.EntityException'. The underlying provider failed on Open

I've been given a site that was created by someone else and I'm now trying to test it. I can compile the system without any problems but when I try to log in to the website, I get the error:
"EntityException occured. A first chance exception of type 'System.Data.EntityException' occured in System.Data.Entity.dll. Additional info: The underlying provider failed on Open."
Furthermore, if I dig deeper, I see an InnerException of Cannot open database \"MyDB\" requested by the login. The login failed. Login failed for user 'name\\owner'.
I've read similar problems on the web and it seems like its a problem with database connections? I've tried multiple 'solutions' that include messing around with the connectionString but nothing works.
What I think the system wants to do is connect to a .mdf located in a separate project's App_Data. Anyway, here's the connectionString code that I received originally:
add name="NameServiceContext"
connectionString="Server=tcp:qiu5vg5yhv.database.windows.net,1433;Database=MyDB;User ID=MYID;Password=MYPASS;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
providerName="System.Data.SqlClient"
Quick question, what is the tcp:...... stuff? I'm assuming it was generated somehow, but how?
I've tried 'fixing' the problem and ended up with something like this:
add name="NameServiceContext"
connectionString="data source=./SQLEXPRESS;AttachDbFilename=C:\Users\owner\Documents\MyERP\App_Data\MyDB.mdf;Integrated Security=True;Connect Timeout=30;"
providerName="System.Data.SqlClient"
Both methods give the same errors and I'm out of ideas. How would I go about fixing this?
Also, when I connect to a db via tools>connect to database > MS SQL db file, I get an option between 2 data sources, ./SQLEXPRESS and (LocalDB)\v11.0. Do I have to include both of them? If so, how?
The original connection string refers to a Microsoft Azure instance. A typical connection string to Azure looks like:
Server=tcp:[serverName].database.windows.net;Database=myDataBase;User ID=[LoginForDb]#[serverName];Password=myPassword;Trusted_Connection=False;Encrypt=True;
Your server name is qiu5vg5yhv.database.windows.net. Most likely your credentials are incorrect.
It is as Andrew said, you don't seem to have access to the actual database. Simply download either "SQL Server Management Studio" or "SQL Server Management Studio Express" (depend on which version of database you are using) and try to connect.
If you connect successfully, check if you can query the database of your project.
If you connect unsuccessfully, contact your system admin to arrange access.
If you want to understand more about connectionstring or create one, use the following site for template: http://www.connectionstrings.com/sql-server/
You can get the necessary database details by viewing the Properties of your database via "SQL Server Management Studio" (right click and select Properties --> View Connection Properties)
I met the same question. The key step is here.
I used vs 2013 update 4.
When you configured your SQL in azure, it generated a connect string.

Unable to connect to SQL Server locally

I am getting the following exception when I am trying to connect to a web service
System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file DatabaseName.mdb failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
The connection string I am using is:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|WS_Assignment.mdb;uid=Admin;pwd="
providerName="System.Data.OleDb"
I am trying to access SQL Server on local system.
I have gone through a lot of articles and followed all the different methods. But I got no solution.
Can anyone help me out?
Before Question was EDITED:
are you deploying on a web server , instead of your localhost. If thats the case you need to publish the sql script in Server DB as servers dont allow attachDB file in your deployment and if thats the case then your problem is solved.
After Update
you can always try with fullpath and yes you need OLEDB call not sql make sure thats not the case with your .cs code
Correction in your question:
i am trying to access SQL server On local system
:
i dont see how you can do that with .mdb file; even if thats not the case make sure sql services are running properly in your system go to->start button->program files->microsoft sql server yourversion-> configuration manager-> check running services.
In your Solution Explorer, click on "Show All Files". then go to your App_Data folder and delete WS_assignment.mdb and then run your application.
Also, your provider is wrong
providerName="System.Data.SqlClient" is the right one.
Secondly, your database name should end in .mdf or .sdf for SQL Server.
So, your connection string will become:
<connectionStrings>
<add name="ConnectionStringName"
connectionString="Data Source=|DataDirectory|WS_Assignment.sdf"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Check this.
http://msdn.microsoft.com/en-IN/library/5ybdbtte%28v=vs.71%29.aspx
using Access requires System.Data.OleDb library

Database Error while launching application

I deployed C# application and created installer. Installer gets created successfully but when tried to launch the application it throws up following error:
An attempt to attach an auto-named database for file CampusPointe.mdf
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.
connection string in app.config is as follows:
<connectionStrings>
<add name="App_Key_Management.Properties.Settings.VendorKeyConnectionString1"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Vendor Key Management\VendorKey.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Any help would be appreciated.
Regards,
Sri
Sri,
First of all, you need to ensure a couple of things:
1. You have installed SQL Server
2. You need to deal with the creation or replacement of the file you created. I think this is your issue ... If the .mdf file exists then you need to either update the existing file or drop and replace it (USE [database] GO DROP TABLE... GO CRATE TABLE ... GO). You will also need to consider step 4 below.
3. If you are accessing a a file located in a database you need a fully formed name. You also need to add the database user to the connection string in ASP.NET Web.Config or App.config (depending on the app). I looks like you have started this ...
4. Make sure the user account that you are using in the connection string has the necessary privileges on the database side. If this is a full fledged database in SQL SERVER then the user account will need update, delete, create privileges. If you are replacing or creating databases on the fly then the user will need CREATE DATABASE or DROP / CREATE privileges.
I think to best answer your question we would need more information on the architecture of the application. My guess is that your code creates the database fine the first time and then when you try to run it the second time it fails because the database is already there. If this is the case you also need to address your code (with IF statements or SWITCH CASE ... your call) to handle situations where the table (1) does not exist and needs to be created; (2) exists and needs to be updated; and / or (3) exists and needs to be replaced. The correct answer again depends on your code and what you are trying to do.

What exactly is this error?

I was trying out making a website with asp.net to see how data binding works but when I launch the application, it throws this error.
Failed to generate a user instance of SQL Server due to failure in
retrieving the user's local application data path. Please make sure
the user has a local user profile on the computer. The connection will
be closed.
I checked the internet and I found a lot of possible solutions but none of them worked for me.
I created a custom database in the App_Data folder of the project and the connection string is
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True
It cannot locate the data file at the specified path, or the user does not have sufficient privileges to access the data file.
If the database is set up for Windows Authentication Mode, you should try changing your connection string to something like this:
Server=<COMPUTER NAME>\SQLEXPRESS;Database=<DATABASE NAME>;Trusted_Connection=True;
OR
Server=<COMPUTER NAME>\SQLEXPRESS;AttachDbFilename=<DIRECTORY PATH>\<FILE NAME>.mdf;Trusted_Connection=True;

Categories