Database Connection: Web.config Asp.net - c#

What's wrong with my connection string?
I copied my files from one computer to another. Then I altered my connection string because I moved my files from another computer, it is not connecting to database. When I tried connecting to SQL Management Studio, it successfully connected to my server and when I try running my codes, it didn't.
Here's my Connection String: (Web.config) - My server is Windows Authentication
<add name="ConnectionStringMe" connectionString="Data Source=ZOZOW-PC\MSSQLEXPRESS;Database=sample_db;Persist Security Info = True" providerName="System.Data.SqlClient" />
It gives me 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)
Is there any conflict if my database is from SQL Server Authentication then I moved it to Windows Authentication?
Thank you.

I believe you need this Integrated Security = true into your connection string and it should work. If you don't have this in your connection string you need to specify password and username.
<add name="ConnectionStringMe" connectionString="Data Source=ZOZOW-PC\MSSQLEXPRESS;Database=sample_db;Persist Security Info = True;Integrated Security=True" providerName="System.Data.SqlClient" />

Related

Problems with SQL Server connection in console app

The SQL server is located on a remote machine.
I can connect to it with MVC app.
I can connect to it with SSMS.
But I can not do it in console app.
Neither entity nor SqlConnection is working.
Connection string is the same as in console and mvc app (which works). What can it be? Any ideas?
Error I am getting
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)'
What can it be?
<add name="DBContext" connectionString="data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient";
cn.Open();
Console.WriteLine(cn.State.ToString());
The problem is that console app uses 445 and 139 port. In terms of security. But why MVC app doesn't use it. And how I can I use it without opening this port. Even dividing db part in different library didn't work.

I am unable to connect to my SQL Server 2008 on Remote Server

I am Trying to connect to my SQL Server but I keep getting 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) (.Net SqlClient Data Provider)
This is my connection string
<connectionStrings>
<add name="myDB"
providerName="System.Data.SqlClient"
connectionString="Server=Usman-PC\SQLExpress; Database=myDB; Integrated Security=True;"/>
</connectionStrings>
I have google for solution and tried many things but keep getting this error.
I was developing accounts app and now it is time for deployment on production server. Please somebody help me.
Below are my settings
Computer name: Usman-PC
WorkGroup: USMAN-SERVER
Ip address: 192.168.0.1
Server Name: Usman-PC\SQLExpress
Instance Name: SQLEXPRESS
What I have tried:
I have looked at connection setting in Server Properties and they are good.
I also have checked and server is running.
I also have enabled TCP/IP and set the port to 1433.
I also have added new rule for port 1433 in firewall.
I am also able to transfer files between both computers.

database connection in windows application

I am going through a windows application in C#.net using SQL Server 2008 as database server. The following is the code in my app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="myconnection" value="Data Source=ritesh-pc\SQLEXPRESS;Initial Catalog=dbname;Integrated Security=SSPI"/>
</appSettings>
</configuration>
Whenever I try to access that key
"myconnection" as string connectionString = ConfigurationSettings.AppSettings["myconnection"];
the following error occurs
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)
This could be that your connection string is wrong/inaccessible due to passwords/etc.
Is a firewall blocking the connection?
Can you access the database, with the settings in the configuration file, using SQL Server management studio?
Is the sql database server situated on the same host?
Can you show us more details, changing some parts obviously (pass/user/host)
You want something like
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Please refer to Store connection string in web.config I find the article very useful
I got the same error before and what I do is :
How to resolve Error 26 in SQL Server?
I tried using the (IP address of the server)\SQLEXPRESS. Sometimes instance name is unrecognizable better use the IP address of your sql server.
or maybe you've got a wrong connection string. In your app.config check if the syntax is like this:
<add name="conString"
connectionString="Data Source=10.99.89.80;Initial Catalog=EWB_FileDownloader;User ID = sa; Password = 12435#"
providerName="System.Data.SqlClient" />
The problem is in the connection string , I think you must set your account parameters (if you have) like this
Data Source=.\SQLEXPRESS;Initial Catalog=attendence;Integrated Security=SSPI;
User ID=myUsername;Password=myPassword;
For full details : check this

Getting SQL Server error in ASP.net application when using different computer

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

A network-related or instance-specific error occurred while establishing a connection to SQL Server. My server was not found or was not accessible

I know this issue is already on the forum, but my case is a bit special, I have a site that has two connection string in the web-config as follows:
<add name = "myConnString"
connectionString = "user id = user1, password = user1; Persist Security Info = False; Database = clarins_abs; Server = teck2012; Connect Timeout = 60; Trusted_Connection = False;"
providerName = "System.Data. SqlClient "/>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
When I put the site on my local iis server everything works fine, but when I put the site on another server I receive this error message:
A network-related or instance-specific error occurred while
Establishing a connection to SQL Server. The server was not found gold
Was Not available. Verify That the instance name is ok and That SQL
Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server / Instance Specified).
I have SQL Server 2008 R2
In your second string, the .\ indicates that the SQL server instance is located on the local machine. Your local iis server probably has this instance, while the other server does not.
Type "SQL Server Configuration Manager" in start menu (win vista / win 7) and check that TCP/IP protocol is enabled.
We have to check following thing for this
1.Check Tcp/IP settings in sqlserver configuration manager settings.If it is disable ,make sure we have to enable the status for TCP/IP
2.Add port to fire wall exception at fire settings. Details for sql port:
Name : SQL
Port Number: 1433
Protocol: Select TCP

Categories