While
Saturday, February 23, 2008
Creating Custom Configuration Sections in Web.config
Most ASP.NET applications include a number of configuration settings, such as connection strings, mail server settings, system-wide default settings, and so forth. While these settings could be hard-coded in the source code, it's usually a wiser idea to place them in a configuration file, such as Web.config. The reasoning being that if these values need to be modified, editing a configuration file is a lot easier than updating the code, rebuilding, and re-deploying. In fact, Web.config provides an section intended to hold application-wide configuration settings (see Specifying Configuration Settings in Web.config for more details).
While work well for in-house applications, if you are building an application that will be deployed by end users a more professional approach is to use a custom configuration section. , for example, requires that all values be simple scalars provided through elements, like:
While
Working with Databases in ASP.NET 2.0 and Visual Studio 2005
When you install any of the Visual Studio 2005 editions you'll be asked if you also want to install SQL Server 2005 Express edition. If you choose Yes, this will install SQL Server 2005 Express edition on your development box, where you're installing Visual Studio 2005. (SQL Server 2005 Express edition can be installed side-by-side with other versions of SQL Server, including SQL Server 2000 and other editions of SQL Server 2005.)
If you use SQL Server 2000 in your current projects you are probably most comfortable working with the database through Enterprise Manager. While you can still use Enterprise Manager for SQL Server 2000, or SQL Server 2005's Management Studio for your SQL Server 2005 databases, you can also manage these databases through Visual Studio 2005's Data Connections. I mention this because for SQL Server 2005 Express edition there is not a GUI tool like SQL Server 2000's Enterprise Manager or SQL Server 2005's Management Studio; instead, you have to create and manage your SQL Server 2005 Express edition databases through Visual Studio 2005. (Microsoft is working on a SQL Server Management Studio Express Edition for SQL Server 2005 Express; currently the software is released as a community technical preview edition, although an "official" version should be released in due time - keep your eyes on the SQL Server 2005 Express homepage for the latest news.)
Managing SQL Server 2005 Express Edition Databases
If you have any other SQL Server 2005 edition other than Express, you can install the client tools on your machine, which includes Management Studio, the GUI tool for managing SQL Server 2005 databases. If you have this tool installed you can use it to also manage SQL Server 2005 Express edition databases.
To manage a database through Visual Studio 2005, go to the Server Explorer; there you'll find a Data Connections node (shown in the screenshot to the right). You can add new database connections by right-clicking on Data Connections node and choosing Add Connection. This will bring up a dialog box that prompts you for information such as the database server, authentication information, what database to use, and so on. If you installed SQL Server 2005 Express edition on your machine, the database was installed, by default, using the instance name SQLExpress. Therefore, the server name to connect to would be YourMachineName\SQLExpress or .\SQLExpress. In addition to connecting to an existing database, you can also create a new database by right-clicking on the Data Connections node and choosing Create New SQL Server Database.
Once a database has been added to the Data Connections tab, you can add, delete, or modify tables, stored procedures, views, and so on through the appropriate folders. To create a new table or stored procedure, right-click on the appropriate folder and choose Add New X menu option; to modify an existing table, stored procedure, or view, double-click the table, stored procedure, or view. This will bring it up in Visual Studio, where you can modify it as needed. You can also view and modify the data in individual tables by right-clicking on a table name and choosing Show Table Data.
Adding a Database to the App_Data Folder
In addition to adding existing databases through the Data Connections tab, you can also add a site-specific database to the website's App_Data folder. App_Data is a new, reserved folder in ASP.NET 2.0 that is designed to hold data-related content, including SQL database files (.mdf files), Access database files (.mdb files), XML files, and so on. From an ASP.NET website project, you can effortlessly create and add a new SQL Server 2005 Express database to your project by right-clicking on the App_Data folder in the Solution Explorer and choosing Add New Item. Then, from the Add New Item dialog box (shown below), choose to add a new SQL Database.
If you want to follow along with this article, create a SQL Server 2005 Express database in the App_Data folder called Customers.mdf. Add a single table to this database named Customers with the following columns: CustomerID (an auto-increment primary key field), Name, City, State, and ZipCode. Then add some records to this table through VS 2005. (The database file and complete ASP.NET code is available for download at the end of this article...)
You can also add existing Access database files and even SQL Server 2000 database files. (Note: In order to add an existing SQL Server 2000 .mdf file you'll need to ensure that it has first been detached via the Enterprise Manager; to accomplish this, right-click on the database name in Enterprise Manager, select All Tasks, and choose Detach Database. Once you've detached the database and added it to the ASP.NET 2.0 project, you can reattach it through Enterprise Manager.)
Connecting to a Database Using the SqlDataSource Control
Now that we've seen how to create and work with databases through Visual Studio's interface, let's turn our attention to accessing and displaying data from a database in an ASP.NET 2.0 web page. ASP.NET 2.0 includes a number of new datasource controls, whose sole purpose is to provide declarative access to data. There are five built-in datasource controls, all of which can be found in the "Data" portion of the Toolbox in Visual Studio (see the screenshot to the right).
* SqlDataSource - used to retrieve and modify data from a relational database. The "Sql" in the name doesn't mean that this datasource only works with Microsoft SQL Server; rather, it can be used against any relational database: SQL Server, Access, Oracle, and so on. The control is intelligent enough to, internally, use the more efficient SqlClient classes if you are connecting to a SQL Server database.
* AccessDataSource - used to retrieve and modify data from an Access database file. You may be wondering why this control exists if SqlDataSource can work with Access database files. The AccessDataSource control makes it easier to connect to an Access database - you just specify the path to the Access database's .mdb file. With the SqlDataSource you need to use a fully qualified connection string that specifies the data provider.
* ObjectDataSource - used for retrieving and modifying data through a business object. Ideally your ASP.NET application includes a set of classes that constitute the middle tier (rather than having the ASP.NET pages work directly with the database). If you have such an architecture, the ObjectDataSource can be used to query the middle tier.
* XmlDataSource - used for retrieving data from an XML file.
* SiteMapDataSource - used to provide read-only access to the site structure as defined in the site map. This control is used when you want to display a site's structure in a TreeView or Menu control; for more information on ASP.NET 2.0's site navigation features refer to Creating a Site Navigation Hierarchy.
In this article we'll be examining just the SqlDataSource control, and just its basic functionality.
If you use SQL Server 2000 in your current projects you are probably most comfortable working with the database through Enterprise Manager. While you can still use Enterprise Manager for SQL Server 2000, or SQL Server 2005's Management Studio for your SQL Server 2005 databases, you can also manage these databases through Visual Studio 2005's Data Connections. I mention this because for SQL Server 2005 Express edition there is not a GUI tool like SQL Server 2000's Enterprise Manager or SQL Server 2005's Management Studio; instead, you have to create and manage your SQL Server 2005 Express edition databases through Visual Studio 2005. (Microsoft is working on a SQL Server Management Studio Express Edition for SQL Server 2005 Express; currently the software is released as a community technical preview edition, although an "official" version should be released in due time - keep your eyes on the SQL Server 2005 Express homepage for the latest news.)
Managing SQL Server 2005 Express Edition Databases
If you have any other SQL Server 2005 edition other than Express, you can install the client tools on your machine, which includes Management Studio, the GUI tool for managing SQL Server 2005 databases. If you have this tool installed you can use it to also manage SQL Server 2005 Express edition databases.
To manage a database through Visual Studio 2005, go to the Server Explorer; there you'll find a Data Connections node (shown in the screenshot to the right). You can add new database connections by right-clicking on Data Connections node and choosing Add Connection. This will bring up a dialog box that prompts you for information such as the database server, authentication information, what database to use, and so on. If you installed SQL Server 2005 Express edition on your machine, the database was installed, by default, using the instance name SQLExpress. Therefore, the server name to connect to would be YourMachineName\SQLExpress or .\SQLExpress. In addition to connecting to an existing database, you can also create a new database by right-clicking on the Data Connections node and choosing Create New SQL Server Database.
Once a database has been added to the Data Connections tab, you can add, delete, or modify tables, stored procedures, views, and so on through the appropriate folders. To create a new table or stored procedure, right-click on the appropriate folder and choose Add New X menu option; to modify an existing table, stored procedure, or view, double-click the table, stored procedure, or view. This will bring it up in Visual Studio, where you can modify it as needed. You can also view and modify the data in individual tables by right-clicking on a table name and choosing Show Table Data.
Adding a Database to the App_Data Folder
In addition to adding existing databases through the Data Connections tab, you can also add a site-specific database to the website's App_Data folder. App_Data is a new, reserved folder in ASP.NET 2.0 that is designed to hold data-related content, including SQL database files (.mdf files), Access database files (.mdb files), XML files, and so on. From an ASP.NET website project, you can effortlessly create and add a new SQL Server 2005 Express database to your project by right-clicking on the App_Data folder in the Solution Explorer and choosing Add New Item. Then, from the Add New Item dialog box (shown below), choose to add a new SQL Database.
If you want to follow along with this article, create a SQL Server 2005 Express database in the App_Data folder called Customers.mdf. Add a single table to this database named Customers with the following columns: CustomerID (an auto-increment primary key field), Name, City, State, and ZipCode. Then add some records to this table through VS 2005. (The database file and complete ASP.NET code is available for download at the end of this article...)
You can also add existing Access database files and even SQL Server 2000 database files. (Note: In order to add an existing SQL Server 2000 .mdf file you'll need to ensure that it has first been detached via the Enterprise Manager; to accomplish this, right-click on the database name in Enterprise Manager, select All Tasks, and choose Detach Database. Once you've detached the database and added it to the ASP.NET 2.0 project, you can reattach it through Enterprise Manager.)
Connecting to a Database Using the SqlDataSource Control
Now that we've seen how to create and work with databases through Visual Studio's interface, let's turn our attention to accessing and displaying data from a database in an ASP.NET 2.0 web page. ASP.NET 2.0 includes a number of new datasource controls, whose sole purpose is to provide declarative access to data. There are five built-in datasource controls, all of which can be found in the "Data" portion of the Toolbox in Visual Studio (see the screenshot to the right).
* SqlDataSource - used to retrieve and modify data from a relational database. The "Sql" in the name doesn't mean that this datasource only works with Microsoft SQL Server; rather, it can be used against any relational database: SQL Server, Access, Oracle, and so on. The control is intelligent enough to, internally, use the more efficient SqlClient classes if you are connecting to a SQL Server database.
* AccessDataSource - used to retrieve and modify data from an Access database file. You may be wondering why this control exists if SqlDataSource can work with Access database files. The AccessDataSource control makes it easier to connect to an Access database - you just specify the path to the Access database's .mdb file. With the SqlDataSource you need to use a fully qualified connection string that specifies the data provider.
* ObjectDataSource - used for retrieving and modifying data through a business object. Ideally your ASP.NET application includes a set of classes that constitute the middle tier (rather than having the ASP.NET pages work directly with the database). If you have such an architecture, the ObjectDataSource can be used to query the middle tier.
* XmlDataSource - used for retrieving data from an XML file.
* SiteMapDataSource - used to provide read-only access to the site structure as defined in the site map. This control is used when you want to display a site's structure in a TreeView or Menu control; for more information on ASP.NET 2.0's site navigation features refer to Creating a Site Navigation Hierarchy.
In this article we'll be examining just the SqlDataSource control, and just its basic functionality.
Click here to view complete article
Subscribe to:
Posts (Atom)