NET 2.0 introduced a number of new Web controls designed for accessing and modifying data. NET 2.0's data source controls make it possible to retrieve, insert, update, and delete data without having to write the tedious dedata access code that was so commonplace in ASP. Previous installments of this article series have looked at returning, inserting, and deleting data using the Sql Data Source control. These controls can work in unison with the data source controls to facilitate inserting, updating, and deleting data.
These controls allow page developers to declaratively access and modify data without writing any code to perform the data access. In this article we will examine basics of using the Sql Data Source and Grid View controls to update records from a database.
AND Primary Key Column Name N = @Primary Key Value To Update N , in this instance - has its value specified via the data source control's Select Parameters, which can specify a source for the parameter value.
The source may be: a hard-coded value like "3.95", which would return all products less than .95; the value of a Web control on the page, allowing the user to enter a price bound in a Text Box; from the querystring; from session state; and so on.
Future installments will explore more advanced updating scenarios, such as customizing the Grid View's editing interface and programmatically setting the updated values. The UPDATE Table Name SET Column Name1 = @Parameter Value1 Column Name2 = @Parameter Value2 ...
Column Name N = @Parameter Value N WHERE Primary Key Column Name1 = @Primary Key Value To Update1 AND Primary Key Column Name2 = @Primary Key Value To Update2 AND ...
We can easily create the Web controls necessary to facilitate an editing interface and it is simple enough to use a Sql Data Source control with appropriate Update Parameters to execute the appropriate statement with values based on the Web controls' values.To demonstrate this functionality, let's look at using a Grid View to list all of the records from the SELECT [Product ID], [Product Name], [Products].[Category ID], [Category Name], [Unit Price], [Discontinued] FROM [Products] INNER JOIN [Categories] ON [Products].[Category ID] = [Categories].[Category ID] ORDER BY [Product Name] can be specified through the data source's wizard, via the Select Query property in the Properties window, or declaratively.Previous installments in this article series have covered these steps in detail.In particular, Bound Fields turn into Text Box Web controls; Check Box Fields, which are displayed as unclickable checkboxes in the read-only status, become clickable.This interface allows the user to make changes to the selected row's values.