You are currently browsing the daily archive for December 8th, 2007.
Objective: This article give step-by-step instructions to adding a MSSQL database connection string to your site.
Background: This is a auxiliary article to my previous post called “Adding The Membership Provider To An Existing Database”. Using a wizard based interface and a few clicks, we will add and register a connection string to our web.config file. This is a very basic task, but if you are just starting with ASP.NET coming from a different background (classic ASP, PHP etc.), some of the simplest tasks can become the most frustrating ones.
Procedure: Follow instructions below. This article uses MSSQL Server Express 2005 and Visual Web Developer Express 2008.
- If you haven’t already, create a new web site using Visual Web Developer Express 2008.
- Add a new web form (page) and name it anything.
- From the “Data” section of the toolbox, drag and drop an SqlDataSource onto your page.
- If the smart tag is not automatically open, click on the little arrow on top to open it.
- Click on the “configure data source” menu to launch the wizard.
- Click on the “new connection” button on the right side
- You will see the following window open:
- You have several options here. If you intend to add a custom membership provider to your existing database, work with a local database illustrated on the image above. If however you want to add a connection string to a remote database hosted by a web host provider then look at the image below to see what your configuration panel should look like before clicking the OK button.
You would simply enter the address of the location of your database provided to you by your web host, and type in your user name, password and the name of your database. The default site structure created by Visual Studio or Visual Web Developer Express doesn’t always work with web hosting providers. This is because their database server is probably not located on the same server as your site files. Therefore you may not be able to to use your database located in the App-Data folder as a default setup in provided by Visual Developer Express. With a remote connection, however we would not have to worry about this. The connection string would automatically find your database no matter where it is hosted, provided you have the appropriate permissions.
Once you have decided which way you want to go and proceed with it, click the “Test Connection” button to make sure everything is configured properly.
- Click OK
- Click Next
- Here the wizard will ask you if you want to save this connection string into your web.config file. Make sure the “Yes” is selected and click Next.
- Configure your SQL query by using the wizard and follow the screen instructions to finish the wizard. If this a new site, a web.config file will be created for you with your connection string already inside. If this site already had a web.config file, then the new connection string will be appended to it.
- Once you are done, you can keep this web form or delete it, and it will have no effect on your connection string which is safe in your web.config file.
That’s all there is to it.
