Now let’s look at the steps to enable users to remove a record from our data store.
Similarly, as when editing a record, we let the user choose which record to delete, except in this case, we need not provide an entry form. To confirm that the selection is correct, though, we render the record’s data and provide a form with just Delete and Cancel buttons.
Basically, the page is made up of two panel controls: one to contain the interface for user selection, which is shown on page load, and one for displaying the data of the record to be deleted.
The loadPanel is the same as that described in the edit example, while the zapPanel consists of a DataList control that is used to display the data of the record to delete. The DataList in this panel is basically similar to the DataList in the postPanel described in the edit example. The only difference is in how the Delete button is set up.
<input type=submit value="Delete" runat="server"
onServerClick="zapMessage">
Now let’s examine each part of the page logic in detail.
Likewise, the page flow is controlled by three methods: the page load handler, a method to fetch the data of the selected record, and a method to delete the record in our data store.
The Page_Load event handler is fairly straightforward. On each load, the method simply initializes the connection to the database, retrieves the message id from the querystring, and renders the appropriate panel.
When the user makes a selection ( effectively "posting" the form ), control is passed to the getMesage method, which essentially fetches a single-row DataReader to populate the DataList in the zapPanel.
When the user clicks on the Delete button ( which is really a submit button ), the form posts back to the same page, wherein we simply issue an SQL Delete command in the button’s event handler.
public void zapMessage ( object src, EventArgs e ) {
myCmd.CommandText = "DELETE FROM aspx_messages where MessageID=" + id;
myConn.Open ( );
myCmd.ExecuteNonQuery ( );
myConn.Close ( );
bindGrid ( );
}
Well, that’s about it for this session. Hopefully at least, you’ve gained insight on the "conventional" ways to add, edit or delete records from a database in the .NET framework. Whilst outwardly basic, the methods described here can be adapted to any application with similar data-based needs.
Whenever testing any update application, it is good practice to maintain backup copies of your data at all times. If you inadvertently update or delete the wrong records, you cannot undo the operation.
Again of course, the samples provided can further be improved, on performance grounds. Because here we use separate pages to clearly show each method, we need to redundantly open and close the database. Ideally, table update methods are grouped onto one page, which can have Add, Edit and Delete buttons, as well as provide for search, paging and/or record navigation. This way, opening and closing the connection is done only once.
In an upcoming article, we shall look into the ADO.NET concepts and methods to enable users to add, edit and delete information at the DataSet level. If you haven’t done so, I suggest you start with the ADO.NET Primer to help you "get connected".
Displaying information from a database | Selecting information from a database