Library Settings - c#

I have a library project that accesses the database configured in app.config
I have a web project that accesses the database configured in web.config
Both are working properly
Now I want to use the library from the web project.
In Visual Studio works fine.
In production server the web project access the database but the library not is capable of accessing the database from the project web.
The ConnectionString in app.config and web.config are identical.
In production server should not take the web.config settings?
Thanks and sorry for my English
EDIT
web.config and app.config connectionStrings
<connectionStrings>
<add name="IntranetConnectionString"
connectionString="Data Source=(local)\SQLExpress;Initial Catalog=Intranet;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<add name="Database.Properties.Settings.IntranetConnectionString"
connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=Intranet;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
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)

If I understand correctly, try copying local the reference of the libraries, to do this, right-click the reference in your project, then, in the properties, set True to Copy Local, now you will have the libraries locally in your server.
This should work if the problem is related to the references of the libraries, it won't help if it's a web.config/app.config problem

The DLL generally takes the app.config of the host application that is calling it and not its own config. Is there some meat here?

The solution:
In the DLL create a class to access the DataTable with a constructor that receives the ConnectionString
public TestBLL(String ConnectionString)
{
TestTableAdapter ta = new TestTableAdapter();
ta.Connection = new System.Data.SqlClient.SqlConnection(ConnectionString);
}
In the Web project:
String cs = System.Configuration.ConfigurationManager.ConnectionStrings["IntranetConnectionString"].ToString();
TestBLL taBLL = new TestBLL(cs);
In taBLL have the methods to access each Query

Related

ASP.NET connectionString for mdf file not working

I am using I am using Visual Studio 2017. I just created a brand new mdf file in my project (Right click on App Data -> Add New Item -> SQL Server Database) and I called the file TestDatabase.mdf
Now I am trying to connect to it via a connection string:
<add name="TestConnection" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|TestDatabase.mdf;Database=TestDatabase; Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />
Next I created a Model and then Created a EntityFramework Controller, but now when I goto the controller index method I get this 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)
What am I doing wrong?
Make sure you testDatabase.mdf available in App_Data folder.
<connectionStrings>
<add name="ConnectionName"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
This connection string that you have written is incorrect.
<add name="TestConnection" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|TestDatabase.mdf;Database=TestDatabase; Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />
I assume that you have SQL Express installed in your system. This will provide a local instance of SQL to run on your machine.
As you said you are using Visual Studio 2017, you can follow these steps to get the correct connection string
Open Server Explorer
Right Click on your Database and click Properties
A property window will open, copy the ConnectionString and make sure it looks like this:
<add name="YourDbName" connectionString="Data Source=YourComputerName\SQLEXPRESS;Initial Catalog=YourDbName;Integrated Security=True" />
And now talking about this:
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)
You are getting this error because it is not getting the instance name to connect to the database. In my example, SQLEXPRESS serves as an instance name. If you don't have SQLEXPRESS you can download it from here:
https://www.microsoft.com/en-in/sql-server/sql-server-editions-express
You can follow the prompt and install this package in your system.
Once you install this, your instance will get activated. Connect your Db once again.I am sure it will work.
In case, if this error comes again, you can follow these steps:
Hit Windows + R and type services.msc and hit OK
From the given lists, search for SqlServer (SQLEXPRESS) and click start the service. This will activate your instance once again and your database is now ready to connect to your application.
Hope this helps.

A network-related or instance-specific error occurred while establishing a connection to SQL Server in EF 6.2.0

I am using Entity Framework 6.2.0 with an asp.net MVC code-first approach to do a sample application.
I have enabled-migrations and update-database successfully. Consequently, the database Vidly.Models.ApplicationDbContext.mdf has been created in my Solution Explorer within the App_Data folder.
I have declared a class in my Models folder as below:
public class ApplicationDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
In my Web.Config file, I have added the connection string as below:
<connectionStrings>
<add name="ApplicationDbContext" connectionString="Data Source=.;Initial Catalog=Vidly.Models.ApplicationDbContext;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
However, when I am starting the application, I am getting the following exception :
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: Named Pipes Provider, error: 40 -
Could not open a connection to SQL Server)'
I have consulted similar exceptions on stackoverflow, but could not figure out what is wrong.
Could someone advise what is wrong here please ? Thanks.
You have create your Database in your App_Data Folder but you trying to access
sql server that.
try this
<connectionstrings>
<add name="ApplicationDbContext" connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Vidly.Models.ApplicationDbContext.mdf;Integrated Security=True;User Instance=True " providername="System.Data.SqlClient" />
</connectionstrings>
I see that this is NOT your specific case (but it IS related to exception you are having - so added this answer for the sake of completeness)
I had the same error and the cause of the problem was that I had forgotten to add providerName to the connection string...
(in my case - coppied sql string from Azure portal, manually created connectionString parameter in Web.config file and forgot to add providerName parameter, so it is not that hard to have such a mistake...)
So if you are experiencing this type of exception, you should check if your connection string is correct / not missing provider name.

How can connect to host's db from host?

I bought a Windows hosting from GoDaddy. I have configured the configuration settings(Adding database and publishing project's files to host)
I can access the database while running the project on the local server.
But when I transfer the project to the server, the project on the server can't do it.
I wrote my connection string to web.config file, here it is:
<add name="xyz" connectionString="Data Source=IP ADDRESS;Initial Catalog=DBNAME;User ID=USERNAME;Password=PASS;Integrated Security=False" providerName="System.Data.SqlClient" />
I used MVC and Entity Framework, I also used layered structure
Thank You.
try to check more details about the connection string that should be used with your database provider. Here you can find several examples about how to create your connection string properly in SQL SERVER.
please notice that if you are working in your host machine, you have not to specify the IP ADDRES of your SQL SERVER but the SERVER NAME just like:
Data Source=(SERVER_NAME) \ DB_INSTANCE_NAME
I hope that it's gonna be useful.

LocalDB not found when deploying to a different machine

I'm having a problem switching a project I am working on over to another system. It's an ASP.Net web application and web form wrapped in a single solution, with a SQL .mdf DB file in the project as well. I'd like to be able to zip up the whole thing and move it around, but when trying to debug the solution on the other machine I get an error about the local DB not existing. My connection string in the Web.Config file looks like this:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-name-of-database.mdf;Initial Catalog=aspnet-name-of-database;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I know I must be making an obvious mistake, but I can't figure out what it is. Any advice, please? Thanks.
Edit: The specific error is:
"The specified LocalDB instance does not exist."
Run the following from your package manager console:
SqlLocalDb info
If the executable isn’t found, chances are you do not have LocalDb installed in your new machine. You can use the steps mentioned in the below link to debug as to why the connection is not successful to your localDB:
http://odetocode.com/blogs/scott/archive/2012/08/15/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True
Try setting the Datasource as below.
Data Source=.\;
If you are able to connect to your database, then the set the ServerName as DataSource in your connection string.
Make sure you have SSMS installed in your new system.
Download SQL Server Express 2014

Hosting ASP.NET Web App on IIS DB connection problems

I have used IIS before, but I've never used a database and I've never done a web app. I did the same steps for putting a site onto IIS and everything works EXCEPT for when I go to a page that has a DB connection on it. I've tried a lot and this is all I get.
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.
Windows Application Event Log:
Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.
This was built in Visual Studio and I have a SQL Express DB.
I posted my connection strings, any help is greatly appreciated.
I have added this to my applicationHost.config:
<add name="DefaultAppPool">
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
And now I get:
An attempt to attach an auto-named database for file |MY DB1 FILE| failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB1.mdf;Initial Catalog=aspnet-ICCAA_VIZIER-20150223043129;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB2.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>
I was able to figure it out. I changed all my data connections to .\SqlExpress. Which led me to another error of:
Login failed for user ‘NT AUTHORITYNETWORK SERVICE‘
To fix this I followed this guys article to figure it out.
Login Failed For user NetworkService

Categories

Resources