Insert Error:Directory lookup for the file - c#

I am have a register page on my website built in VS2010 and when I fill the form in a click register I am getting the following error. Help would be appreciated.
System.Exception was unhandled by user code
Message=Insert Error:Directory lookup for the file "C:\Users\myname\Documents\myname\Project\App_Data\SJDatabase.mdf" failed with the operating system error 5(error not found).
Cannot attach the file 'C:\Users\myname\Documents\myname\Project\App_Data\SJDatabase.mdf' as database 'SJDatabase'.
connection string:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<add name="MyConsString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SJDatabase.mdf;
Initial Catalog=SJDatabase;
Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>

I think you are missing a backslash on |DataDirectory|\SJDatabase.mdf
also, you have Integrated Security=SSPI twice and I dont think you need the AttachDBFileName part
try something more simple like this:
"data source=ServerName;Initial Catalog=SJDatabase;Integrated Security=SSPI" providerName="System.Data.SqlClient"

Related

.net service - how to set more than 1 database in web.config

I've created a .net service that work with a database declared in web.config file. Now the project evolved and i need to query more than 1 database, how can i do this in web.config and then specify the database i want to query in the c# files?.
Here's how the current database is specified:
<connectionStrings>
<add name="stringName" connectionString="Data Source=XXXXXXXXX;Initial Catalog=DBName;Persist Security Info=True;User ID=UsrID;Password=bazinga;Application Name=AppName;Connect Timeout=20" providerName="System.Data.SqlClient"/>
</connectionStrings>
Thanks!
You just add another item to the connectionStrings section with a different name specification.
How your program calls the database is another story and I do not have the section of code which is defining the existing one. You would just need to find that and use the same method, just adding another variable to identify it.
<connectionStrings>
<add name="stringName" connectionString="Data Source=XXXXXXXXX;Initial Catalog=DBName;Persist Security Info=True;User ID=UsrID;Password=bazinga;Application Name=AppName;Connect Timeout=20" providerName="System.Data.SqlClient"/>
<add name="stringName2" connectionString="Data Source=XXXXXXXXX;Initial Catalog=DBName;Persist Security Info=True;User ID=UsrID;Password=bazinga;Application Name=AppName;Connect Timeout=20" providerName="System.Data.SqlClient"/>
</connectionStrings>

Entity Framework crashed Visual Studio 2015?

I just added EntityFramework to my MVC5 project. I`ve created classes and set up the connections string:
<connectionStrings>
<add name="UserDBContext"
connectionString="server=.; database = DDP; integrated security = true;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
After debug Visual Studio is not responding and I have to remove it by Task Manager. I canot even debug to see what`s happen. What could do that ?
UPDATE
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"></defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="UserDBContext" connectionString="Data Source=.; Integrated Security=True; Initial Catalog = DDP;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
You used wrong connection string in your web.confing in you case it looks like code for SqlConnection.ConnectionString when it is used from C# directly.
But in ASP.NET MVC with EF connection string in web.confing looks different, where you use Data Source instead of server and Initial Catalog instead of database, so in the end you connection string will look something like that:
<add name="UserDBContext" connectionString="Data Source=SERVERNAME\SQLEXPRESS;Initial Catalog=DDP;Integrated Security=True" providerName="System.Data.SqlClient" />
Update
Put your <add ../> on 1 line, move Initial Catalog before Integrated Security and then delete ; after Integrated Security=True like below:
<add name="UserDBContext" connectionString="Data Source=.;Initial Catalog=DDP;Integrated Security=True" providerName="System.Data.SqlClient" />
If this code doesn't work, then if you installed SQL Server Express with default settings use .\SQLEXPRESS instead of .:
<add name="UserDBContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DDP;Integrated Security=True" providerName="System.Data.SqlClient" />

What should be the datasource while uploading file to the real server in asp.net

This is what I have tried to do
<add name="str" connectionString="Data Source=XXX.XX.XXX.XXX;Initial Catalog=immenseshop; Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>

Cannot connect to local DB at another PC. ASP .NET MVC4

I have created an mvc 4 database application on "PC1". I just have chosen by right click "Database SQL Server" and created Entity Framework with Database First approach. It works okay at "PC1". But when I copied my project to another PC, then the project failed.
I do not have an SQL Server at the PC. I just copied the mvc 4 project with local database.
The error is: "...(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
I suppose that I should rewrite connection string. I've done this steps:
http://msdn.microsoft.com/en-us/library/hh873188.aspx
And I have tried to change a connection string from:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
To:
I have added name of PC(TomPC) to connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.TomPC\SQLEXPRESS;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.TomPC\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.TomPC\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
However, it fails with the same error.
I would greatly appreciate any help!
You will also need to ensure that TCP/IP is enabled in SQL Server config on TomPC, and also open up port 1433 (the default port, but it might be different, e.g. if there's more than one instance of SQL running on that PC).
Data Source=.\SQLEXPRESS
The . means "localhost" or 127.0.0.1
So adding the . means localhost\SQLEXPRESS or 127.0.0.1\SQLEXPRESS
You need to change it to:
Data Source=TomPC\SQLEXPRESS
I've found an answer. The application could not connect as connection string should be modified as LocalDB:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
http://www.asp.net/mvc/tutorials/mvc-5/introduction/creating-a-connection-string

How to encript connectionstring section into the web config

I tried to use integrated security SSIP but it takes anonymous user so any developer can access the database. So anybody have any solution for this problem?
<connectionStrings>
<add name="Local2" connectionString="Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=NEOPLAST_LIVE;username="abc" password="****" providerName="System.Data.SqlClient" />
</connectionStrings>

Categories