# .NET and More > Silverlight >  [RESOLVED] SilverLigh with WCF throwing an exception while connecting database

## ERUM

I m using win7 and VS2008 and sql server 2005

I have an application of silver light with WCF ,(i need to bind grid with database records) but its giving me an excpetion while opening conenction to database..Although i make changes in sql server Express to connect locally and remotely (asi m using WFCF). my connection string in web.config is 

```
	<connectionStrings>
		<add name="NorthwindConnectionString" connectionString="Data Source=(IRAM-PC);Database=Northwind;User Id=sa;Password=sa"  providerName="System.Data.SqlClient" />
      </connectionStrings>
```

other piece of code is here

```
using System.Data;
using System.Data.SqlClient;
 
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;

namespace SilverlightWithWCFService.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SampleService
    {
        [OperationContract]
        public List<Customer> CustomerList()
        {
            string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
            var custList = new List<Customer>();
            using (SqlConnection conn = new SqlConnection(nwConn))
            {
                const string sql = "SELECT TOP 10 CustomerID, CompanyName, ContactName FROM Customers";
                conn.Close();
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    SqlDataReader dr = cmd.ExecuteReader(
                        CommandBehavior.CloseConnection);
                    if (dr != null)
                        while (dr.Read())
                        {
                            var cust = new Customer
                            {
                                CustomerName = dr.GetString(0),
                                CompanyName = dr.GetString(1),
                                ContactName = dr.GetString(2)
                            };
                            custList.Add(cust);
                        }
                    return custList;
                }
            }
        }

        // Add more operations here and mark them with [OperationContract]
    }
}
```

giving me an exception on the line  conn.Open();

any help!!

----------


## ERUM

sorry i forgot to to mention the exception in my last post

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

----------


## chris128

That exception just simply means that it cannot connect to the SQL database using that connection string. Are you sure the connection string is correct? Can you confirm that the SQL database is actually accessible with that username and password remotely - you could try using SQL Management Studio to connect from whichever machine you are testing the silverlight app from and seeing if you can connect into the remote SQL database from there.

----------


## ERUM

in fact user name and password is correct.and  i m doing that application locally at my machine.but still I would check what are other possibilites

----------


## ERUM

Ok ,ResolveD, just  barckets was creating problem in webconfig

----------


## chris128

ahh, glad you got it fixed  :Smilie:

----------

