ASP.NET beginner question: How to get rid of this error? - c#

I am trying to create a simple login page in ASP.NET C# but am getting some error.
I googled about the error, and tried all the solutions but to no avail.
What am I doing wrong? The error is:
"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 think it's something to do with my connection string which is as follows:
<add name="cn"
connectionString="server=HCL;DataBase=GS;Integrated Security=True" />
"HCL" is another machine connected to my PC through the LAN and this database, "GS", resides on it. Is the problem with the value I am giving in "Server" attribute of the above code? If not what else may I be doing wrong?
I can tell all solutions I tried. Let me know if that's required.

I don't think "Server" is a valid property for a connection string. Try "Data Source" instead. "Integrated Security" should also be set to SSPI:
<add name="cn" connectionString="Data Source=HCL;Initial Catalog=GS;Integrated Security=SSPI"/>
UPDATE:
I just noticed that you have also used "Database". This should be "Initial Catalog".
UPDATE2:
There is a neat trick for creating connection strings by using .udl files. If you create an empty file called "something.udl" and double click it, Windows will open a nice dialog for defining connections. To create a connection string for a SQL Server, choose "Microsoft OLEDB Provider for SQL Server" on the "Provider" tab and then fill in your server name, login credentials, and database name on the "Connection" tab. Finish by testing the connection and click "OK".
After the dialog is closed, you can drag the .udl file into Notepad or Visual Studio and you will see that the .udl file actually contains a connection string ready for you to use (note that if you wish to use the connection string with SqlConnection in .NET you must remove the "Provider=SQLOLEDB.1" part of the string).

The error is definitely connectivity. Usually in firewall rules, remote connections disabled or just general connectivity issues.
However using integrated security is probably your issue. If you are in a domain/workgroup this could work,w ith properly set permissions, but if not then you probably need to pass your credentials. Integrated security will pass the credentials of your logged in account and is the normal method if the database in on the same PC as the application.
<add name="cn" connectionString="Data Source=HCL;Initial Catalog=GS;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;"/>
See http://www.connectionstrings.com/ for more connection string examples.

Try adding "Integrated Security=SSPI;" or specify your UID, Pwd for connecting to the server. You might be getting struck at the authentication.

Open management studio and connect with SQL Server using SQL Server Authentication. Enter userId and password to connect. Use that userid and password in your connection string.
connectionString="Data Source=HCL;Initial Catalog=GS;User Id=UserId;Password=password;
User Id -> UserId you want to login
using. like 'sa'
Password -> password of you user.
If you get error Login failed for user" 'yourusername' then this link will help. This is to enable user sa.
SQL Server 2005 "Login failed for user" sa
EDIT:
connectionString="Data Source=HCL;Initial Catalog=GS;Persist Security Info=True;User ID=sa;Password=pass"
This link might help
sql-network-interfaces-error-26-error-locating-server-instance-specified

I think you should try trusted_connection = true instead of integrated security in your connectionstring.
See here: http://connectionstrings.com/sql-server-2005
Second reason could be that you have several instances of your database which you have to write into the connectionstring.

Related

Issue connect to SQL Server 2000 from C#

I am using a Winforms C#, .Net Framework 4.0 application and I'm trying to connect to SQL Server 2000 using Data.SqlClient. Database and app are running on the same server machine.
Before, I ran app on 2 other servers successfully.
My connection string in config is:
ConnectionString="Data Source=xyz;Initial Catalog=xyz;Integrated Security=True;"
but a 3rd server causes an error:
Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=3998; handshake=0; [Login] initialization=0; authentication=0; [Post-Login] complete=10000; "
Then, I changed the connection string to
ConnectionString="Data Source=xyz;Initial Catalog=xyz;Integrated Security=True;Connection Timeout=30;"
and I get a new error
A transport-level error has occurred during SSPI handshake. (provider: Named Pipes Provider, error: 0 - The pipe has been ended.)
Update: I tried using the udl file to create the connection string and put it into the config file but there is still error
So what am I missing here ?
Thanks in advance.
Update: I fixed this issue when replace sqlclient by OLEDB lib
ConnectionString="Data Source=xyz;Initial Catalog=xyz;Integrated Security=True;"
This may be correct try to close your connection after you open
I think you are not missing anything. Your connection string looks good to me.
ConnectionString="Data Source=xyz;Initial Catalog=xyz;Integrated Security=True;
May be due to any reason the connection was placed using Named Pipes instead of Shared Memory. Shared Memory is the default option for local SQL connection so SqlClient could not recognize the connection was local, and placed through Named Pipes over TCP. Result is connection was refused or not connected.
I am not sure but please check you server name, it should be qualified domain name or IP address. If you specify it by the hostname or "." or "(local)", SqlClient should recognize the local connection, and you should not see the error.
I've just been through this problem and nothing I found anywhere helped, so I thought I'd share the solution that helped me.
Turns out my client had used "username#domain.com" as their login instead of just "username". Looks like there is some functionality internal to excel that tries to authenticate the user via that domain, and thus, the connection times out without a response. Don't create logins with "#domain.com" in SQL Server.
Hope this helps someone in future.

Connection string is not valid or Error Locating Server/Instance

I reinstaled my PC and now I have problems with my SQL connection string or with SQL Server 2012 Enterprise
Error I get:
"... (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)" or
"... (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
In SQL Server Management Studio server instance looks like this:
TOM-PC (SQL Server 11.0.3128 - Tom-PC\Tom)
And My SQL connection string in ASP.NET XML config. file:
<add name="ApplicationServices" connectionString="Data Source=.\TOM-PC;Database=D:\Programming\InvoiceManager\invocemanagerSQL.mdf;" providerName="System.Data.SqlClient" />
Tried everything, in firewall added port 1434 or 1433 tested both, not working, "SQL Server Browser" Service is running, TCP/IP Enabled, Allow remote connections to this server, even tried 4 different connection strings, nothing worked.
Before it worked like a charm, not fully but worked. Why not fully? Because I couldn't work on my local machine with both studios "VS2010" and "SQL Management studio" at the same time, if I needed change something in database manually, I needed to stop or restart SQL Server process and service, then I get in database, and read tables. Now, nothing is working!
What I do or did wrong? Where seems to be the problem? Why always are problems with SQL servers and it's connectionstrings?
What you are seeing in Management Studio is as follows:
Instance Name (SQL Server version - Domain\LoggedInUser)
You are logged in using integrated authentication (i.e. your windows logon account) but not specifying it in the connection string. This will try to use the currently logged on user for the ASP.Net application, which will almost certainly not be running as your windows account. It will either be one of the local or network service accounts or a built-in ASP.Net user.
You're also using the wrong variable name to attach the database file, if this is actually what you want rather than connecting to an actual database on the server.
What I suggest you do is enable SQL Server authentication on your instance, create a new user and give it permissions to the database as required.
You would then log in using the following format:
Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;
Where myServerAddress would be localhost or TOM-PC; Database would be the name of the database as it appears in Management Studio; User Id would be the username for the user you just created and Password would be the password you set for them during creation.
If you do actually want to attach a database file then your connection string changes to use this format:
Server=myServerAddress; AttachDbFilename=C:\Path\To\Database\File.mdf; Database=myDataBase; Trusted_Connection=Yes;
Typically, you can get most connection string variants that you will need from: http://www.connectionstrings.com/sql-server/

Writing a connection string to access a remote SQL Server database

My database is hosted on a remote server. I need to write the connection string which I have to include in the web.config file.
server name -- abcs.efgh.ed-1.eee.sss.com,1433 (it also contains a port as well)
username -- a
password -- a
db name -- mydb
I know how to connect to a local database and I use to refer to connectionstring.com for reference but, connecting to a remote database is a problem to me. Help please
UPDATE:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient"
connectionString="abcs.efgh.ed-1.eee.sss.com,1433;Integrated Security=True;User ID=a;Password=a" />
</connectionStrings>
Exception I get is :
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
Actually, it's not Windows authentication that I want to use. It's SQL Server authentication which I want.
You are encountering the error because you are using "integrated security=true" to connect. Use this website to construct your connection string. http://www.developerfusion.com/tools/sql-connection-string/
I used the website to generate this connection string using your inputs:
Data Source=abcs.efgh.ed-1.eee.sss.com,1433;Initial Catalog=mydb;Integrated Security=False;User ID=a;Password=a
Put Integrated security =false. in connection string
When false, User ID and Password are specified in the connection. When
true, the current Windows account credentials are used for
authentication. Recognized values are true, false, yes, no, and sspi
(strongly recommended), which is equivalent to true. If User ID and
Password are specified and Integrated Security is set to true, the
User ID and Password will be ignored and Integrated Security will be
used. SqlCredential is a more secure way to specify credentials for a
connection that uses SQL Server Authentication (Integrated
Security=false).
More at Here

error: 26 - Error Locating Server/Instance Specified. (Can't connect to my local Db From my host server)

I already asked this question before, but without any good response,
my problem is that I have a local database (aspnetdb.mdf) in my app_data folder, here is the connection string in the web.config :
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
so my site works fine in my local iis server, but when i put the site in my host server, I get this error message :
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).
In the other server I have sql server 2008 (the remote connection is accepted and i have the same instance name SQLEXPRESS). I really tried everything that I can but without any good results. I will really appreciate your help, thx.
This format indicates a local connection:
.\SQLEXPRESS
From another server, you'll need to specify a server name or IP address, e.g.:
192.168.1.205\SQLEXPRESS
YOURMACHINE\SQLEXPRESS
You'll also need to validate that the instance is SQL Server Express. From your other nearly identical question it looks like a default instance.
You should also stop using this silly user instance / attachDbFilename technique. Attach your database to the instance properly, then reference the logical database name using Initial Catalog=aspnetdb; in your connection string. This user instance feature was deprecated for multiple reasons, and this is certainly one of them.
Of course there may be other network issues that will prevent this, e.g. if the server is in a different domain, there are firewall settings interfering, etc. But your other server is never going to be able to access your machine using a dot for the server name.
EDIT providing baby steps because obviously my advice is not being followed. Assuming you have actually attached the aspnetdb database on the new server, try this please:
connectionString="data source=192.168.85.124\SQLEXPRESS;
Integrated Security=SSPI;
Initial Catalog=aspnetdb;"
If you aren't using SQL Express named instance after all, try:
connectionString="data source=192.168.85.124;
Integrated Security=SSPI;
Initial Catalog=aspnetdb;"
For SQL authentication use the same username/password you use in Management Studio:
connectionString="data source=192.168.85.124;
User ID=username; Password=password;
Initial Catalog=aspnetdb;"
This is troubleshooting 101 stuff. If you still can't get this to work, you may have to hire a consultant for 10 minutes to fix it for you since it's impossible to figure out what you've done and why you can't get it to work.

Error when connecting to .mdf from ASP.NET (C#)

When trying to connect to a .mdf databse in ASP.NET (using c#) I am given 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)
Does this mean my connection string is wrong? What should it be?
Aside from that, I am very new to ASP.NET. I am trying to connect to this database in the same way I would in c# normally (using data adapters and SqlCommands). Is this the right way to go about it, or is there a different way? When I started a new website adding user accounts worked (there was a wizard or something?) but I couldn't work out how to add more user information. What is the best way to connect to an SQL database and add user accounts with login and personal details in ASP.NET?
EDIT: My Connection String is data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true
If I read your connection string properly, you're never specifying a database to use!
data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\Users.mdf;User Instance=true
Add the database to the connection string - and also, loose the extra backslash after the |DataDirectory|:
server=.\SQLEXPRESS;database=YourDatabase;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|Users.mdf;User Instance=true
OK, I've fixed it. For reference for anyone else:
The correct connection string was given in the Web.config file.
The connection string I needed was Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;User Instance=True

Categories

Resources