Linq to Sql connection strings in a class library

So after writing the last post, I thought my SQL connection problems were solved.  After all, I could remotely connect to my server through SSMS, but then when I actually tried to connect through my asp.net web application, I got the dreaded:

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

I googled and messed around with my new server for 6 hours.  I got so desperate to figure out why I couldn't connect to my new server that I signed up for some cheapie SQL 2008 hosting, attached my database, and tried to connect to that one.  Same exact issue.  I had played around with IIS security, file and folder security, SQL permissions, asp.net permissions, but nothing worked.  I read the message....it says it can't find my server..hmm..  So I set a breakpoint in my code and ran in debug mode.  I looked at the datacontext and bingo!!!  I don't know why I hadn't thought of this earlier.  I have three seperate class libraries in my web app that use Linq to Sql.  I never pay attention to how it actually works, I just drag on my tables and go.  But I discovered that it was using a connection string that was located in my application settings file that was NOT the correct connection string!  It was using the connection that I set up during development.  So obviously I need to change the connection string, but what a pain that would be.  Seems like there should be a better way.  After all, you need to move your site through development, testing, staging, and production, so you would need to change these connections.

Well, here is the trick:

1.  Open up your dbml designer and look in the properties windows.  Expand Connection.  Change Application Settings to "False". 

2.  Next to Connection select "(None)" from the drop down.  This will clear out the connection string that was being used from your Application Settings.  You can delete the Settings.cs file under Properties too now and also the App.Config.

3.  If you compile, you'll get errors.  This is because you need to somewhere pass in a connection string.   To do this, create a partial class in your class library.  

In this class, you are pointing to a connection string that is located in your web.config file.

That's it.  Do this for all your class library projects and they will all point to a single web.config located in your web project.   I must give credit to Rick Strahl, whose blog I read regulaly.  He got me thinking about Linq to SQL and connection string when reading his blog this afternoon.  Much headbanging and pulling my hair out and it was something as simple as trying to connect to the wrong server!   I really hope this helps someone.

Connecting to a remote development SQL Server 2008

I have a nice low-end Dell PowerEdge SC1430 server that I run out of my house to host some testing and development sites.  It's running Windows Server 2008 with SQL Server 2008 and is set up as a web server.  This has happen a few times now but I forget how to fix it every time, so I'm blogging about it.  I just reinstalled SQL 2008 because I botched it up somehow and it's sometimes easier to just start over.  After reininstalling and installing the server packs, I restored my databases and was back in action -- at least locally.  My IIS sites were running great.  So today I try to connect to my SQL Server 2008 instance through my laptop and SSMS and I'm shut out - connection failed.  I figure it's a firewall issue.  Nope, I already have port 1433 opened on both machines.  Some googling led to me a "allow remote connections" checkbox in the server instance properties.  Nope, already checked.  After an hour of racking my brain, I remembered one last thing -  enable the TCP/IP protocol on the SQL Server!

And bingo.  It works.

So, the next time I install a new SQL Server and need to allow remote TCP/IP access to it (through SSMS or a tool like SQL Compare), I'll take the following steps:

1.  Allow access to TCP port 1433 access on both the client and server machines through Windows Firewall (or whatever firewall that is used)

2.  In the properties of the server instance, under Connections, check the "Allow remote connections to this server":

3.  Enable TCP/IP access in the SQL Server Configuration Manager.

Hope this helps someone who was stuck like me.

Scott