Update & Delete Members

 

In the sample, you will play with updating and deleting members. You will learn to use DataGrid for update and delete. Do as follows:

 

1)      Copy the script in aspx to a file named SampleUpdateDeleteMembers.aspx, and copy the script in code to a file named SampleUpdateDeleteMembers.aspx.cs.

 

2)      Modify the connection string in the file SampleUpdateDeleteMembers.aspx.cs, so that initial catalog=your-databasename, user id=your-username and password=your-mssql-password.

 

3)      Create a subdirectory 5thApp under \\Development\net\home\your-username\www, and upload the two files SampleUpdateDeleteMembers.aspx and SampleUpdateDeleteMembers.aspx.cs  to \\Development\net\home\your-username\www\5thApp.

 

4)      Open IE browser, try http://www.development.ccs.neu.edu/home/your-username/5thApp/SampleUpdateDeleteMembers.aspx, and you will see all members are listed. Click the link button “Edit” to enter edit mode for the corresponding row, or click the button “Delete” to delete the corresponding row. In edit mode, after inline modification is made, click the link button “Update” to send the modification to the backend database, or click the link button “Cancel” to cancel the modification. 

 

 

Script Explanation

 

SampleUpdateDeletetMembers.aspx

 

1)      There is only a DataGrid in the web form. The data source is specified statically via the attributes of the DataGrid: DataSource="<%# members1 %>" DataKeyField="SSN" DataMember="members".

 

2)      The Data Columns for “lname” and “fname” are not read-only, which indicate these two fields are editable through this DataGrid.

 

3)      Button types: push and link.

 

SamplesUpdateDeletetMembers.aspx.cs

 

1)      In Page_Init(),  the event handlers for “Edit” button click, “Delete” button click, “Update” button click and “Cancel” button click are specified.  In addition to the select command associated with SqlDataAdatper, commands for update and delete are also created.

 

2)      In Page_Load(),  when this page is not posted back, the DataSet members1 will be populated and the DataGrid DataGrid1 will be bound to this DataSet.

 

3)      In DataGrid1_EditCommand(),  the row to be updated is specified. After DataGrid1 is re-bound to the re-populated DataSet members1, that row will appear in edit mode.

 

4)      In DataGrid1_CancelCommand(),  the row number to be updated is set to -1. After DataGrid1 is re-bound to the re-populated DataSet members1, no row will appear in edit mode.

 

5)      In DataGrid1_UpdateCommand(),  the updated values are assigned to the parameters required by the SqlCommand for update. Those values are trimmed to fit the size required by the backend database table members.  The update involves only one row, which is specifies by the field SSN.  A connection is opened to execute the command for update. After the update is finished, the connection is closed and DataGrid1 is re-bound to reflect the modification.

 

6)      In DataGrid1_DeleteCommand(), similar to update,  after the command for delete is finished, DataGrid1 is re-bound to reflect the deletion.

 

 

Exercise

 

The above sample does not allow updating the phone number. Please extend this sample to allow users to update the member’s phone number, in addition to the last name and the first name.

 

This exercise is optional. If you have any question, please contact me.