The Delete Group Page

There is a mechanism with which users can delete groups that they've created. Deleting groups is carried out with the DeleteGroup.aspx page, and the code behind this page can be seen in Listing 22.7.

The first method you'll see in Listing 22.7 is the ShowGroupName() method. This method simply takes the group name that was passed to this page and outputs it to the HTTP stream. The parameter name is Group. The method checks to make sure that the Request.QueryString() method doesn't return a null (or Nothing in VB) indicating that there was no HTML parameter. In the case of a null, the string name strGroupName will be set to contain an empty string and therefore avoid any runtime errors.

The Cancel_Click() method is fired when users click the Cancel button. This method just redirects the user to the Manage Files page (MngFiles.aspx).

The OK_Click() method is fired when the user clicks the OK button. Its purpose is to delete the group and all of its associated files. It must delete the records in the database and the files from disk.

One of the dilemmas for Web developers who give users the chance to perform operations is that, once the operation is done, users are redirected to another page without any confirmation that the operation has been performed successfully. To provide a solution to this, the Delete Group page performs the operation, gives feedback in the form of text on the screen, and then sets the button to contain the text Back. Because of this, the first thing the OK_Click() method does is to see whether or not the button text contains the text OK. If it does not contain the text OK, then the operation has already been performed and the user is simply redirected to the Manage Files page.

Let's say, though, that the button text contains OK and we need to delete the group. We'll need a try/catch/finally construct to deal with any exceptions that might be thrown by both the database access methods and the disk I/O operations. The catch block takes the exception message and puts it into the Label object named ErrorMessage. The finally block closes the SqlConnection object if it is open.

In the try block, the SqlConnection object is opened. A string with the group ID is obtained using the Request.QueryString() method. If the group ID can't be obtained (and this will result in the string named strGroupID being null), then the string is assigned the value of 1 so that no groups will actually be deleted. A SQL statement is formed to delete the group from the FileGroup table. The following SQL statement would be formed if the group ID is 16:

 Delete from FileGroup where ID=16 

A SqlCommand object is created with the string containing the SQL object and the SqlConnection object as parameters to the SqlCommand constructor. The SQL to delete the group from the FileGroup table is performed with the SqlCommand object's ExecuteNonQuery() method.

Now we have to go through each file that belongs to the group and delete the disk files. First, we must get a recordset back that contains the files that must be deleted from disk. The following SQL statement will be used if the group ID is 16:

 Select Directory,Filename from FileInfo where GroupID=16 

The SQL statement is used to create a new SqlCommand object. The SqlCommand object's ExecuteReader() method is called, which returns a SqlDataReader object containing the recordsets that were retrieved from the database. A string variable named strPath will contain the path to the files. This variable is assigned the first time through the while loop. The static File.Delete() method is used to delete each file. The following code shows how the path is created, and then how the File.Delete() method is used:

C#
 if( strPath.Length == 0 ) {   strPath = Request.MapPath( "." ) + "\\" +     ConfigurationSettings.AppSettings["DirectoryName"] + "\\" +     Convert.ToString( objReader["Directory"] ); } File.Delete( strPath + "\\" +   Convert.ToString( objReader["Filename"] ) ); 
VB
 If strPath.Length = 0 Then   strPath = Request.MapPath( "." ) + "\" + _     ConfigurationSettings.AppSettings("DirectoryName") + "\" + _     Convert.ToString( objReader("Directory") ) End If File.Delete( strPath + "\" + _   Convert.ToString( objReader("Filename") ) ) 

After looping through the list of files, if the strPath variable contains a directory (which it always should), the directory is deleted. A simple try/catch construct is used to catch any exceptions that might be thrown by the static Directory.Delete() method. The user won't be notified of the error, but the directory will not be deleted.

Last, the records for all of the files will be deleted with the following SQL:

 Delete from FileInfo where GroupID=16 

The SQL statement is used to create a SqlCommand object. The ExecuteNonQuery() method is used to execute the SQL. A message is shown to users in the ErrorMessage Label object so they know that the operation was performed successfully. The button text is set to Back, and the Cancel button is made invisible by setting its Visible property to false.

Listing 22.7 This Code Is behind the DeleteGroup.aspx Page.
 // This method executes to display the group name. public void ShowGroupName() {   string strGroupName = Request.QueryString["Group"];   if( strGroupName == null )   {     strGroupName = "";   }   Response.Write( strGroupName ); } private void Cancel_Click(object sender, System.EventArgs e) {   Response.Redirect( "MngFiles.aspx" ); } private void OK_Click(object sender, System.EventArgs e) {   if( OK.Text != "OK" )   {     Response.Redirect( "MngFiles.aspx" );   }   // Create a connection object.  SqlConnection objConnection =   new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);   try   {     // Open the connection.     objConnection.Open();     string strGroupID = Request.QueryString["ID"];     if( strGroupID == null )     {       strGroupID = "-1";     }     // Create a command object.     SqlCommand objCommand = new SqlCommand( "Delete from FileGroup " +        "where Select Directory,Filename " +       "from FileInfo where Group";     while( objReader.Read() )     {       if( strPath.Length == 0 )       {         strPath = Request.MapPath( "." ) + "\\" +           ConfigurationSettings.AppSettings["DirectoryName"] + "\\" +           Convert.ToString( objReader["Directory"] );       }       File.Delete( strPath + "\\" +         Convert.ToString( objReader["Filename"] ) );     }     if( strPath.Length > 0 )     {       try       {         Directory.Delete( strPath );       }       catch       {       }     }     objReader.Close();     // Create a command object.     objCommand = new SqlCommand( "Delete from FileInfo where The group has been deleted.";     OK.Text = "Back";     Cancel.Visible = false;   }   catch( Exception ex )   {     // Alert the user to the error.     ErrorMessage.Text = ex.Message.ToString();   }   finally   {     // Close the connection.     if( objConnection.State == ConnectionState.Open )     {       objConnection.Close();     }   } } 


ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net