Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have the following code segment :

C#
using (SqlConnection con = new SqlConnection("Server=USER_PC;Database=CompanyName;User Id='';Password=''"))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand("SELECT CompanyName FROM CompanyTable", con))
                {
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string strCompanyName = "";

                            Console.WriteLine("Company Name: {0}\n    {1}", strCompanyName);
                        }
                    }
                }
            }


I am using sql server 2008 Management studio

When I run I get this error :

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

And the green arrow points to this   : con.Open();

What am I missing ?

Please let me know if I must put screen shots of the problem to make it easier.

Thanks
Posted
Comments
berrymaria 15-Jul-13 1:34am    
The problem is your connection string. Please check again your connection string.
zenspace 15-Jul-13 1:36am    
double check your connection string.
Sanjay K. Gupta 15-Jul-13 2:01am    
Your Database name is "CompanyName" or Table name is "CompanyName"????

As has been suggested, it's your connection string.
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

BTW: It's not a good idea to "hardwire" connections strings - they are liable to change, particularly when you move to a production environment. Try putting the string into a config file or similar, where it can be changed without needing a recompile of your application!
 
Share this answer
 
if you are using SQL authentication , User ID cannot be blank
 
Share this answer
 
This error depends on your instance name that is used.Suspected code is marked here ("Server=USER_PC;Database=CompanyName;User Id='';Password=''").Try using USER_PC\SQLEXPRESS.

Regards.. :laugh:
 
Share this answer
 
check this. problem in your connection string, and also you not specified Integrated Security
Create the .udl file. create the connection with .udl and the connectionstring from that .udl file
C#
new SqlConnection("Data Source=(local);Database=CompanyName;User Id='';Password=''")
 
Share this answer
 
The problem is in your connection string. User Id can't be empty. Check your user id and password.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900