I am writing a Windows Forms application, where I want to save data inside the application. For the reason I used a serviced-based database with a .mdf database file, and I use Entity Framework.
So far everything is working fine until I released the file and opened it on another computer. The application opened and everything worked fine but when it comes to the database interaction, it throws a big error like this:
************ Exception Text ************
System.Data.Entity.Core.EntityException: The underlying provider failed on Open.
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.)
Here a screenshot of the error: https://www.dropbox.com/s/gwban6ab97c6fya/22093543_10213995193571435_760919436_n.png?dl=0
In case you need the project its uploaded to here: https://www.dropbox.com/s/ubpc683ggtihh6k/usercontrol.zip?dl=0
I have tried in so many ways but nothing is working, same thing happens for the installer version as well.
Can anyone help me on this please?
Here are my connection strings:
<add name="DatabaseEntities"
connectionString="metadata=res://*/dbModel.csdl|res://*/dbModel.ssdl|res://*/dbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="OPLCheque.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
I'm pretty sure this post would be flagged as duplicate since a similar question has been answered multiple times already, but here you go.
You are using LocalDB which means it cannot be shared since it runs on SQL Server Express.
If you simply want a LocalDB, see this answer to implement it after publishing your WinForms app.
Basically you want to publish your app with prerequisite components so you should publish those along your application.
If you want to use a single database to be shared between instances of your application, learn how to setup one using SQL Server here.
Afterwards, learn what connection string format you should use.
Sample to connect via IP Address:
connetionString="Data Source=IP_ADDRESS,PORT;
Network Library=DBMSSOCN;Initial Catalog=DatabaseName;
User ID=UserName;Password=Password"
See this for more.
If ever you plan on using a shared database, better make sure that the connection string that your database context should use is specified.
Config:
<add name="DefaultConnection" connectionString="" providerName="" />
Database Context:
public DbContext()
: base ("DefaultConnection")
{
}
Related
I've being trying in vain to solve a problem on a newly created windows server with IIS hosting an ASP.NET application that was copied from another environment. The issue is that I cannot manage to connect to the database when the application tries to open a connection via entity framework.
If this is off topic for SO let me know so I can move to another channel, I thought it belonged here as seems specific to EF.
In detail, I have the following connection strings:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=LOCALCOMPUTER\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=yyy;Password=zzz" providerName="System.Data.SqlClient"/>
<add name="MembershipConnection" connectionString="metadata=res://*/App_Code.Membership.Membership.csdl|res://*/App_Code.Membership.Membership.ssdl|res://*/App_Code.Membership.Membership.msl;provider=System.Data.SqlClient;provider connection string="Data Source=LOCALCOMPUTER\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=yyy;Password=zzz;MultipleActiveResultSets=True;Application Name=EntityFramework"" providerName="System.Data.EntityClient"/>
...
xxx, yyy, zzz of course have the proper values. LOCALCOMPUTER is the FDQN of the local computer, which is not on a domain
The first connection string is for the Microsoft login library, while the second uses entity framework from our code.
When I browse to the site, I can successfully access the login form, and if I try to insert a wrong password i get a message saying so, meaning the application managed to check the database using the first connection string, but when i insert a correct one, i land on an exception as soon as the code tries to create a context.
The underlying provider failed on Open.:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - The wait operation timed out.)
In the event viewer it tells me that the user has been authenticated via Authentication Type: Forms , which I assume it means the auth system (the one using ApplicationServices connection string) is working, the stack trace confirms that the error occours when I try to use EF in code, meaning the DB has been used succesfully to check user credentials
note that:
The web app works in other environments
I can connect to the sql server using the credentials via SSMS
I've assigned a local windows account to the app pool that can also access the database, and tried changing to Integrated Security=SSPI
Both .Net 3.5 and .Net 4.8 are installed
The database is on the local machine and has been restored from a dump of the other one, its users have been mapped, and to rule out issues i gave it ample authorisations with dbowner datawriter and datareader
I've been trying to fix this since a couple days checking similar issues on SO with no avail and I don't really know how to proceed, is there something I can do to troubleshoot the issue?
Apologies, I've found out the answer, and it's specific to the code base. Apparently the connection string was being ignored (despite being perfectly usable) in favour of data saved in a table.
Had to contact the previous developer to find out, guess it's a good example of how important is to document your code
I'll close the thread, thanks for all who tried to help
I am having some connectivity issues with Visual Studio and SQL Server.
This is the connections string in my web.config file;
<connectionStrings>
<add name="WintipToysConnectionString" connectionString="Data
Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated
Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
According to my DB connection string found in Visual Studio, my connection string is;
Data Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated Security=True
Which seems to add up - but, I get this error when my program hits a certain line;
System.Data.SqlClient.SqlException: 'A network-related or
instance-specific error occurred while establishing a connection to
SQL Server. The server was not found or was not accessible. Verify
that the instance name is correct and that SQL Server is configured to
allow remote connections. (provider: SQL Network Interfaces, error: 50
- Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
I am actually following the WingTipToys tutorial on the Microsoft website and I am struggling to get it to use the Database I attached to SSMS.
I can write queries inside VS which works fine. So I don't really understand why my program is having this error when running?
Thanks.
Admittedly haven't used SQLExpress in a looong time. It looks like the syntax you're using is for an "instance" - which is pointed out in the error message:
...Cannot create an automatic instance...
What you have does look like server\instance conn string syntax...
Connect to the "default" instance of SQL Express like so: Data Source=.\\SQLExpress
Refer to this:
SSE Connection string sample
Hth...
I have a simple ASP.NET C# application which works fine on the computer it was created. However, whenever this project is compiled and run on any other computer, I get the following error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
On the following line:
bool admin = HttpContext.Current.User.IsInRole("Admin");
I could previously run the application on a different computer without error. I have made no changes to any SQL server settings.
In Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-INT422Project-20140320070224.mdf;Initial Catalog=aspnet-INT422Project-20140320070224;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="DataContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Lab5;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|INT422Project.mdf" />
</connectionStrings>
Your better change your connection String value to use a real sql server database.
SQL Server Express or LocalDB is not used for production web applications because it is not designed to work with IIS.
Please check the following:
Make sure the sql server service is up and running.
Make sure the TCP/IP ports are enabled.
If both are fine, I would suggest you to read the servername and database name from the app.config/ web.config which makes life easier.
Dude, Check this answer.
https://stackoverflow.com/a/16054490/3081461
you can read more about localdb at microsoft TechNet as well :
http://technet.microsoft.com/en-us/library/hh510202.aspx
I'm new to .net world and I started to create asp.net web site.
So I Created a New Project using DexExpress template like below image
When I was run, It appear as;
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
What is the reason for this ?
Is there any other way to use devExpress Templates ?
Sorry for wasting your valuable time for my basic problem here. Because I'm stuck here.
Thank you,
EDITED
here is my connection string,
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<add name="DataConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data.mdf;Integrated Security=True;User Instance=True;Connect Timeout=120" providerName="System.Data.SqlClient"/>
I suspect that either -
Instance names are not configured properly in connection string.
Or the concerned Sql Server instance is not running.
Can you please let us know which instance of Sql Server is running on your PC?
You can see SQL Server Services by typing services.msc in Run box. Please see snapshot to understand how to get instance name.
I'm cloning a local C#-SQL Windows Forms application to a new laptop. I've successfully cloned the C# project and back-up-and-restored the SQL database to the new machine, but the new project is unable to read/query any data from the new, cloned SQL database.
Since the SQL DB is local, the LINQ-to-SQL addressing is slightly different on account of the computer having a different name. So, I deleted the SQL table representations from the existing LINQ .dbml's graphical interface, removed the old server connection from the Server Explorer, added the new server connection, and click-and-dragged the table objects from the Server Explorer's new Data Connection object to the now-empty .dbml file.
Upon rebuilding and running the application in debug mode, I found that the dropdown menus which pull from the SQL DB are empty. Upon trying a button function which pulls from the database, I get the following error:
SqlException was unhandled
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
(The cloned SQL server is configured to allow remote connections, TCP/IP is enabled in SQL, and the original C#-SQL application works without a hitch.)
Check in your Web.Config and make sure that your Connection String is set up correctly. Also check to make sure that when you initialize your DataContext that you are using the correct Connection string from your Web.Config.
Sometimes when you remove dbs from the dbml, it doesn't completely clear the web.config of the old entries. Look for a line like this in your designer.cs file:
public SomeDbDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["SomeDbConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
Compare it with the connectionStrings in your web.config
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="someDBConnectionString" connectionString="Data Source=SomeServer;Initial Catalog=SomeDB;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="theOLDDBConnectionString" connectionString="Data Source=SomeServer;Initial Catalog=OldDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I would check a few things:
1) Is SQL Server started on the new machine?
2) Is the firewall configured to allow SQL connections?
3) Is the client connection mechanism (named pipes, tcp/ip) also configured in SQL Server.
4) Is there a valid network connection in the new machine (we used to have a lot of problems on laptops when they were not connected to the network).
You might have much better luck if you change the SQL Server machine name to . or (local) if you are always going to have the DB installed on the same machine as the app.