database connection in windows application - c#

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

Related

C# Update Connection String Visual Studio

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...

Database Connection: Web.config Asp.net

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" />

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

Create Simple Website using DevExpress Templates

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.

Entity framework 4 Underlaying provider failed on Open

I am trying to connect to the database but I am getting this exception:
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)
I've googled a lot, but I could not find the answer.
Here's my configuration:
I use MS SQL Server 2008 Express. Both Sql Server and Sql Server Browser use my account (I am an administrator). I've also used local system and local service. Without success.
Here is my connectionstring in app.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="EventManagerEntities" connectionString="metadata=res://*/Entities.RelationModel.csdl|res://*/Entities.RelationModel.ssdl|res://*/Entities.RelationModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=martijn-laptop\sql2008;Initial Catalog=EventManager;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
I've also tried to use the sql server username and password, also without success.
I am using winforms. What else can I try?
When I check netstat -an I get this:
TCP 0.0.0.0:1433 0.0.0.0:0 LISTENING
TCP [::]:1433 [::]:0 LISTENING
I don't think this is correct, right? But how do I set it right?
It cannot find instance/server. Telnet the 1433th or the configured port of martijn-laptop\sql2008 to verify its accessible
Go into the SQL Server Configuration Manager and make sure you have the protocols you want to use enabled under "SQL Server Network Configuration" > "Protocols for Sql2008"
From the error it seem like there is connectivity to database issue. Assuming this solution has been working elsewhere have you setup and connected to a different database?
Is so validate that the code generated with edmx has the correct default database connection name or that you are overriding it:
var context = new EventManagerContext("EventManagerEntities");
Or whatever the correct systax for EF is.

Categories