[Download Code]
In Part 1 of this series, we built a DataGrid helper control that would drastically simplify the programming of a read-only data access page. In Part 2, we continue our quest to enhance the control and use it to simplify the programming of a data access page with update and delete capability. We will push the idea to the extreme and show that we can also develop a data access page with sorting, paging, deleting and updating capabilities without writing a single line of code in ASP.NET 1.x Just Like ASP.NET 2.0 HAS Promised.
Again, we will start by examining a typical data access page with update and delete capability and identify repetitive code that can be moved into our control. We will then enhance the DataGridHelper control to handle update and delete. We will show how to use the control And conclude the article.
NOW, Let us Take a Look of a Typical Data Access Page First.
A Typical Data Access Page with Update and Delete Capability [Download Code] We will first look at what is involved in creating a typical data access page with update and delete functionality by examining an example A live demo can be found at http:. // www.dotneteer.com/projects/DataGridHelper/v2/TypicalDataAccessPage.aspx The page is an enhancement over the same page demonstrated in the first part of this article I made the following enhancements:.. 1. Add a button column for the Edit / Update / Cancel commands. Add another button column for the Delete command.2. Set the AutoGenerateColumns property to false. Instead of letting the DataGrid control to generate the columns automatically, I added some bound columns to the DataGrid control at design time. This allows . us to have finer control over the behavior of the DataGrid, such as setting user-friendly headers, the format for the data displayed, as well as the sort expression More importantly, we can now make some columns read-only - we do not want users to edit an identity column.3. Set the DataKeyField property to the name of the primary key column of the table we are going to edit. The DataGrid control will then provide the value of the primary key through the DataKeys property at run -time, so we can use it to update and delete a record.4. Add code to handle the EditCommand, CancelCommand, UpdateCommand and DeleteCommand events of the DataGrid. The SqlDataAdapter member in the page was configured using the "Data Adapter Configuration Wizard. "Using" Advanced Options, "We Turned Off The" Use Optimistic Concurrency "and" refresh Dataset "
options because we do not use them The code that handles the EditCommand and CancelCommand events is essentially repetitive, as shown below:. private void dataGridProducts_EditCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGridProducts.editItemIndex = E.Item.itemindex;
Bindgrid ();
}
Private void DataGridProducts_Cancelcommand (Object Source,
System.Web.ui.WebControls.DataGridCommandEventArgs E)
{
DataGridProducts.editItemIndex = -1;
Bindgrid ();
} We can easily move this code to the DataGridHelper control using the same technique used in Part 1 of this series Now let us take a look of the code that handles the DeleteCommand event:. Private void dataGridProducts_DeleteCommand (object source,
System.Web.ui.WebControls.DataGridCommandEventArgs E)
{
Sqlcommand cmd = sqldataadapterproducts.deleteCommand;
Object key = DataGridProducts.DataKeys [E.Item.ItemIndex];
Try
{
CMD.Parameters ["@ productID"]. value = key;
SqlConnection.Open ();
cmd.executenonquery ();
LBLMSG.Text = "Product" key.tostring ()
"SuccessFully deleded."
Bindgrid ();
}
Catch (Exception EX)
{
LBLMSG.Text = "failed to delete product" key.tostring ()
"Due to Following Error:
" EX.TOSTRING ();
}
} This code basically retrieves the value of primary key column from the DataKeys property of the DataGrid control and sets it to the parameters of the delete command and then executes the command. At last, let us take a look of the code that handles the UpdateCommand Event: Private Void DataGridProducts_UpdateCommand (Object Source,
System.Web.ui.WebControls.DataGridCommandEventArgs E)
{
Sqlcommand cmd = sqldataadapterProducts.UpdateCommand; object key = dataGridProducts.DataKeys [E.Item.ItemIndex];
Try
{
CMD.Parameters ["@ Original_ProductID"]. Value = KEY;
String [] cols = {"@ORIGINAL_PRODUCTID", "@PRODUCTNAME", "@supplierid",
"@Categoryid", "@quantityperunit", "@unitprice", "@unitsInstock",
"@Unitsonorder", "@reorderlevel", "@discontinued"};
INT Numcols = E.Item.cells.count;
For (int i = 3; i { String colvalue = ((System.Web.ui.WebControls.Textbox) E.Item.cells [i] .controls [0]). TEXT; CMD.Parameters [cols [i-2]]. value = colvalue; } SqlConnection.open (); cmd.executenonquery (); LBLMSG.Text = "Product" key.toString () "successfully updated." DataGridProducts.editItemIndex = -1; Bindgrid (); } Catch (Exception EX) { LBLMSG.Text = "failed to update products key.toString () "Due to Following Error: } } The code for the UpdateCommand event was borrowed from the ASP.NET QuickStart tutorial. It is no doubt the ugliest part of DataGrid programming. The first part of the task is to locate the table cell that corresponds to the database table column of interest. Once the cell is located, we need to find the control in the cell that was used for data input. We can find the control by either looping through the Controls collection or using the FindControl method. We can then update the database using the retrieved values . Enhance the DataGridHelper Control with Update and Delete Capabilities [Download Code] We intend to achieve the following goals with our enhancements:. Eliminate repetitive coding for the EditCommand, CancelCommand, and DeleteCommand Simplify the coding for UpdateCommand by extracting user inputs from bound columns Make. use of data adapters to achieve code-free programming. The several data adapters and their configuration wizards is the closest thing to code-free programming offered by Microsoft in ASP.NET 1.x. Although data adapters were designed to synchronize data between DataSet and data sources, the data adapters contain the necessary commands as well as the mapping information we can use to retrieve data and update the data sources from our ASP.NET page. use with any data source. In fact, the control does not use the System .Data.sqlclient namespace. The DataGridHelper.cs file in the WebDataControls Project Contains The code for the enhanced DataGridHelper Control. The Most Impor tant new property of the enhanced DataGridHelper control is the DataAdapter property. If the DataAdapter property is assigned, the DataGridHelper control will attempt to use the data adapter automatically. In the following, we will show the events exposed by the DataGridHelper control and how they behave Depending on WHether The DataAdapter Property is set. Event DescriptionLoadData If the DataAdapter property is set, the DataGridHelper control will load the data automatically and the event will not fire. If the DataAdapter property is not set, it is necessary to handle the LoadData event and set the DataSource property of the event argument. BeforeUpdateData This event will fire when the UpdateCommand event of the DataGrid is fired and before the data is updated. The event argument has a few properties. The values property contains the name / value pair extracted from the DataGrid. We can validate and modify the values . We can also extract additional values using the Item property if the DataGrid contains template columns. Set the Cancel property if we want to cancel the update.UpdateData If the DataAdapter property is not set, we need to handle this event to update the data. THE EVENT ARGUMENT HAS A VALUES AS Name / Value PAIRS. IF The DataAdapter Property Is Set and There I Update Command, The DataGridh elper control will update the data automatically. The event will then fire after the update and we can use it to handle any error occurred.BeforeDeleteData If event will fire when the DeleteCommand event of the DataGrid is fired and before the data is deleted. I include this event mostly for completeness. This event could be used to check external conditions and cancel the delete if the conditions are not met.DeleteData If the DataAdapter property is not set, we need to handle this event to delete the data. If the DataAdapter property IS set and there is a delete command, The DataGridHelper Control Will Delete The Data Automatic or. The Event Will Fire After The delete and we can use it to handle any error occurred. The event arguments for all the events except the first event have an Item property and a DataKey property. The Item property points to the current DataGridItem of the DataGrid control. The DataKey property is of the type of object array. In this version, the array only has one element but we create it as an array so that we can support multiple-column keys in the future The following table contains the properties of the DataGridHelper:. Property PurposeAlternateSortDirection Alternate the sort direction on subsequent clicks of the same column header.AutoSortDataView If this property is true and the data source is DataSet, DataTable or DataView, the DataGridHelper control will sort the data automatically.DataAdapter If the DataAdapter property is assigned, the DataGridHelper control will attempt to use it to load, update and delete data. If NOT Assigned, We Have to Handle The LoadData, Updatedata and DeleteData Events. We can assist from DBADAPTER CLASS to this Prop erty.DataGrid The DataGrid control to bind to. Without binding to a DataGrid control. The DataGridHelper control is useless.MessageLabel If assigned, the DataGridHelper will automatically display the error message occurred during update or delete. It is possible to modify the message in the UpdateData and DeleteData event.SortExpression The current sort expression. This property is useful when the user of the control wants to soft the data themselves in the LoadData event.ResetPageIndexOnSorting If this property is True, the control will reset the page index whenever the sort expression Is Changed. The DataGridHelper Control Also Exposes A Single Method. Method DescriptionGridLoad Refresh the data if we have post-back from another control. We also add a new component, DataGridHelperConnector, to the WebDataControl.dll. This component is used to connect a DataGridHelper control to a DataGrid control, a data adapter, and a Label control at design time. It was created entirely for the sake code-free programming.In the next section, we will show how to use the enhanced DataGridHelper control.Using the DataGridHelper Control [Download code] The DataGridHelper.aspx page demonstrates the use of DataGridHelper control. A live demo can be accessed at http://www.dotneteer.com/projects/DataGridHelper/v2/DataGridHelperTest.aspx. This page is functionally identical to the TypicalDataAccessPage.aspx. The DataGrid control in the page has the Same configuration. The page also contrap and a datagridHelperConnector Component. The page does not contact! IF we do not use the datagridhelperConnector Component All we need is to add three lines of code in the point { DataGridHelper.DataGrid = DataGridProducts; DataGridHelper.DataAdapter = SqlDataAdapterProducts; DataGridHelper.MessageLabel = lblmsg; } Apart from the code-free programming, the DataGridHelperConnector control does not save much coding. In the next section, we will discuss more about the limitations of DataGridHelper control and see how it compares with ASP.NET 2.0 and conclude this article. Final Notes [Download Code] We have successfully realized the promise of code-free programming a complete data access page in ASP.NET 1.x. Now let us examine the limitation of our implementation and compare it to the proposed offering in ASP.NET 2.0.
" EX.TOSTRING ();