Share via


Creating Three Methods That Perform the Export

In this section, you create the three private helper methods that perform the export.

  • ExportSetup()
  • ExportSelection()
  • ExportCompletion()

These methods are called later in this tutorial, from a button click event method. To begin, you create the ExportSetup() helper method.

To create the ExportSetup() method

  1. Open the Web or Windows Form.

  2. From the View menu, click Code.

  3. At the top of the class, add three class declarations.

``` vb
Private exportPath As String
Private myDiskFileDestinationOptions As DiskFileDestinationOptions
Private myExportOptions As ExportOptions
```

``` csharp
private string exportPath;
private DiskFileDestinationOptions diskFileDestinationOptions;
private ExportOptions exportOptions;
```

You later instantiate those helper classes in the ExportSetup() method.
  1. At the bottom of the class, create a private helper method named ExportSetup() with no return value.

    Public Sub ExportSetup()
    
    End Sub
    
    private void ExportSetup()
    {
    }
    
  2. Within the method, set the exportPath string variable to the root directory of the hard drive.

``` vb
exportPath = "C:\Exported\"
```

``` csharp
exportPath = "C:\\Exported\\";
```


> [!NOTE]
> <P>If you want to place the Exported folder within the Web directory of your Web server, prefix the folder name with the Request.PhysicalApplicationPath property.</P>
  1. Create a conditional block that tests whether the directory in the exportPath string already exists.

    If Not System.IO.Directory.Exists(exportPath) Then
    
    End If
    
    if (!System.IO.Directory.Exists(exportPath))
    {
    }
    
  2. Within the conditional block, call the CreateDirectory() method of System.IO.Directory to create the directory in the exportPath string.

``` vb
System.IO.Directory.CreateDirectory(exportPath)
```

``` csharp
System.IO.Directory.CreateDirectory(exportPath);
```
  1. Outside the conditional block, instantiate the DiskFileDesintationOptions class.

    myDiskFileDestinationOptions = New DiskFileDestinationOptions()
    
    diskFileDestinationOptions = new DiskFileDestinationOptions();
    
  2. Populate the ExportOptions instance with the ExportOptions property of the hierarchicalGroupingReport instance.

``` vb
myExportOptions = hierarchicalGroupingReport.ExportOptions
```

``` csharp
exportOptions = hierarchicalGroupingReport.ExportOptions;
```


> [!NOTE]
> <P>You have instantiated and bound the hierarchicalGroupingReport instance to the CrystalReportViewer control in <A href="ms227530(v=vs.90).md">Additional Setup Requirements</A> of <A href="ms227453(v=vs.90).md">Project Setup</A>.</P>
  1. Set the ExportDestinationType property of the ExportOptions instance to the enum selection ExportDestinationType.DiskFile.

    myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
    
    exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
    
  2. For a Windows project, clear the values in the ExportFormatOptions property of the ExportOptions instance. (That line of code is not needed for a Web Site, because the variable is automatically cleared on each click event.)

    myExportOptions.ExportFormatOptions = Nothing
    
    exportOptions.ExportFormatOptions = null;
    

You now create the ExportSelection() helper method.

To create the ExportSelection() method

  1. Open the Web or Windows Form.

  2. From the View menu, click Code.

  3. At the top of the class, add a Boolean declaration that is used to test if no export format is selected.

    Private selectedNoFormat As Boolean = False
    
    private bool selectedNoFormat = false;
    
  4. At the bottom of the class, create a private helper method named ExportSelection() with no return value.

``` vb
Public Sub ExportSelection()

End Sub
```

``` csharp
private void ExportSelection()
{
}
```
  1. Within the method, create a "Select Case" [Visual Basic] or "switch" [C#] statement that references the members of the ExportFormatType enum. The enum is based on the SelectedIndex of the exportTypesList DropDownList control that you created in the previous procedure.
``` vb
Select Case exportTypesList.SelectedIndex

    Case ExportFormatType.NoFormat

    Case ExportFormatType.CrystalReport

    Case ExportFormatType.RichText

    Case ExportFormatType.WordForWindows

    Case ExportFormatType.Excel

    Case ExportFormatType.PortableDocFormat

    Case ExportFormatType.HTML32

    Case ExportFormatType.HTML40

End Select
```

``` csharp
switch ((ExportFormatType)exportTypesList.SelectedIndex)
{
   case ExportFormatType.NoFormat:
      break;
   case ExportFormatType.CrystalReport:
      break;
   case ExportFormatType.RichText:
      break;
   case ExportFormatType.WordForWindows:
      break;
   case ExportFormatType.Excel:
      break;
   case ExportFormatType.PortableDocFormat:
      break;
   case ExportFormatType.HTML32:
      break;
   case ExportFormatType.HTML40:
      break;
}
```

You now create the ExportCompletion() helper method.

To create the ExportCompletion() method

  1. Open the Web or Windows Form.

  2. From the View menu, click Code.

  3. At the bottom of the class, create a private helper method named ExportCompletion() with no return value.

``` vb
Public Sub ExportCompletion()

End Sub
```

``` csharp
private void ExportCompletion()
{
}
```
  1. Within the method, create a try/catch block with the Exception class that is referenced as a variable named "ex."

    Try
    
    Catch ex As Exception
    
    End Try
    
    try
    {
    }
    catch (Exception ex)
    {
    }
    
  2. Within the try block, create a conditional block to test the Boolean variable, selectedNoFormat.

    If selectedNoFormat Then
    
    Else
    
    End If
    
    if (selectedNoFormat)
    {
    }
    else
    {
    }
    
  3. Within the If block, set the Text property of the message Label control to the FORMAT_NOT_SUPPORTED constant of the MessageConstants class.

> [!NOTE]
> <P>You created the MessageConstants class for this tutorial in <A href="ms227530(v=vs.90).md">Additional Setup Requirements</A> of <A href="ms227453(v=vs.90).md">Project Setup</A>.</P>


``` vb
message.Text = MessageConstants.FORMAT_NOT_SUPPORTED
```

``` csharp
message.Text = MessageConstants.FORMAT_NOT_SUPPORTED;
```
  1. Within the Else block, call the Export() method of the hierarchicalGroupingReport instance.
``` vb
hierarchicalGroupingReport.Export()
```

``` csharp
hierarchicalGroupingReport.Export();
```
  1. Still within the Else block, set the Text property of the message Label control to the SUCCESS constant of the MessageConstants class.

    message.Text = MessageConstants.SUCCESS
    
    message.Text = MessageConstants.SUCCESS;
    
  2. Within the catch block, set the Text property of the message Label control to the FAILURE constant of the MessagesConstants class, and then append to it the Message property of the Exception parameter.

    message.Text = MessageConstants.FAILURE & ex.Message
    
    message.Text = MessageConstants.FAILURE + ex.Message;
    
  3. Outside the try/catch block, set the Visible property of the message Label control to "True."

    message.Visible = True
    
    message.Visible = true;
    
  4. For a Windows project, reset the selectedNoFormat boolean variable to false. (That line of code is not needed for a Web Site, because the variable is automatically reset to False on each click event.)

    selectedNoFormat = False
    
    selectedNoFormat = false;
    

You have finished creating the three private helper methods that perform the export.