views:

121

answers:

6

Trying to create a simple login page in Asp.net C# but getting some error...

I googled about the error... 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 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 what all solutions I tried.. lemme know if that's required..thnx

A: 

Use full address of SQL Server may be something like HCL.hclgrp.net and if there is a specific port than specify that port also

saurabh
how do I find out the full name ? The one that appears in the Object Explorer in MS SQL Management Studio Express under which DBs etc are listed ? Is that the one ? I tried that too..giving the same error
Serenity
+1  A: 

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.

Dustin Laine
tried that..its throwing an exception now...."System.Data.SqlClient.SqlException: Login failed for user 'HCL\Guest'."
Serenity
Have you verified the HCL\Guest has permission to connect to SQL server?
Dustin Laine
How to veriify that ? I guess it does...its just not connecting..may be I am giving the server name incorrectly or something...I am logged into Management Studio on my PC thru HCL server onlyusing server name as HCL only
Serenity
On the HCL machine, check the Security -> Login section to see if the appropriate account is there.
Dustin Laine
+1  A: 

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

Jakob Christensen
have always been using "Server" and "Database " properties only..these work fine in the institute..just not working at workplace..dunno what's wrong..oh I use the Express edition btw
Serenity
Reading your error it does seem that it is indeed trying to connect to your SQL server so I guess "Server" and "Database" should work. Please see my update for at trick on how to create connection strings.
Jakob Christensen
tried what u said ie Data Source and Initial Catalogue..it gives "Login Failed exception"
Serenity
@Jakob I see no option to create a .udl file when I click "Add new items" option..I created one manually..db clicked it but no dialogue box was opened..What do I need to select from the list inside "Add New Item" window ?
Serenity
This has nothing to do with Visual Studio. Just create a file in Explorer anywhere on your harddisk by right clicking an choosing "New". I usually just create a .txt file and then rename it to .udl. Note that Explorer must be set up to show all file extensions for this to work.
Jakob Christensen
@Jakob ..ok I just tried this udl thing..got the connection string alright..used it in my web config file..it still saying "Login failed for HCL/guest"..I mean connection was successful when I tested it in this udl tool..why wont it connect in my .net project ?? Shouldnt it ?
Serenity
Where is your asp.net project hosted? Are you running it on your local machine or from another server?
Jakob Christensen
on my local machine only..need to use Database off machine named "HCL"
Serenity
Is your workstation on the same domain as the SQL Server or is it just a simple LAN? Windows Authentication will only work on a domain. If you do not have a domain the easiest solution will be to create a login on the Sql Server and use that instead of Windows Authentication.
Jakob Christensen
It is the same domain
Serenity
I made it work ! had to delete this property "Integrated Security=SSPI" coz it's not window login
Serenity
+1  A: 

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

Sidharth Panwar
I did give username pswd..now it says "Login failed for user 'HCL\Guest"...also set it as SSPI..then it gives the SAME error..argh! whats wrong ? IS it the server name ? How do I find out the IP address of that machine ? How abt me giving the IP address of machine named HCL instead ?
Serenity
Try a simple thing. Go to Server Explorer in visual studio and put the Server/Data Source string in it as you've put in your constring. See if the connection is successful. If yes then something else is the problem. Else, the path is incorrect. Check this first.
Sidharth Panwar
A: 

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.

MUG4N
already tried this ..giving the same error 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
Serenity
+1  A: 
  • 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

Muhammad Kashif Nadeem
I am logged into Management Studio thru these:: Server=HCL,username=sa,password=pass only
Serenity
Try edited connection string
Muhammad Kashif Nadeem
@Muhammad..I tried the steps in that link u gave..user SA was disabled...Enabled it but still no progress..getting the same exact error
Serenity
1- Have you tried the connection string under Edit?2- Are you able to connect to SQL Server from your system using management studio?
Muhammad Kashif Nadeem
Yes tried that..this is what I get again "Login failed for user 'HCL\Guest'."
Serenity