This documentation is archived and is not being maintained.

Populating the DropDownList Control

Visual Studio 2005

In this section, you write code to populate a DropDownList control with a list of non-embedded reports contained within a subdirectory.

Note   In this tutorial, you will populate the DropDownList control with reports contained within a subdirectory of your web site. You can also address folders elsewhere on your file system. If you already have a folder that contains several reports, you can skip the next procedure and move directly to Populating the DropDownList Control.

To create a Reports folder

  1. In Solution Explorer, right-click the project name that is in bold type, point to Add Folder, and then click Regular Folder.
  2. Name this folder Reports.
  3. In Solution Explorer, right-click the Reports folder, click Add Existing Item.
  4. In the Add Existing Item dialog box, set Files of type to All Files (*.*).
  5. Navigate to the Hierarchical Grouping.rpt file in the Feature Examples folder of the Crystal Reports sample reports directory. (See Sample Reports' Directory for the locations of the sample reports.)
    Note   The Hierarchical Grouping report retrieves its data from the Access database xtreme.mdb. If you have not verified the location of this database and its ODBC DSN configuration, see What Needs to be Verified?.
  6. Click the Hierarchical Grouping.rpt file to select it, and then click Open.

    The Hierarchical Grouping.rpt file is added to your project.

  7. Add two more reports from the Sample Reports directory before moving to the next procedure.

This completes the section on adding a Reports folder.

Populating the DropDownList Control

In this procedure, you populate a sorted list of the reports contained within the Reports subdirectory.

  1. Open the Web Form.
  2. From the View menu, click Code.
  3. Above the class signature, add an "Imports"[Visual Basic] or "using"[C#] declaration to the top of the class for the System.IO and System.Collections namespaces.
    [Visual Basic]
    Imports System.IO
    Imports System.Collections
    [C#]
    using System.IO;
    using System.Collections;
  4. At the class level, create a new ReportDocument variable, reportDocument.
    [Visual Basic]
    Private myReportDocument As ReportDocument
    [C#]
    private ReportDocument reportDocument;
  5. Within the page_init() method, create a Not IsPostBack conditional block.
    [Visual Basic]
    If Not IsPostBack Then
    Else
    End If
    [C#]
    if(!IsPostBack)
    {
    }
    else
    {
    }
    Note   The Not IsPostBack conditional block is used to encapsulate code that should only be run the first time the page loads. Controls are typically bound to data values within Not IsPostBack conditional blocks so that their data values (and any subsequent control events) are not reset during page reloads.
  6. Within the Not IsPostBack conditional block, create a String variable that will hold the path to your reports folder.
    [Visual Basic]
    Dim myReportPath As String = Server.MapPath("Reports/")
    [C#]
    string reportPath = Server.MapPath("Reports/");
    Note   If you skipped the previous procedure, and you have an existing directory of reports, enter the file path to that directory in place of "Reports/".
  7. Use the Directory.GetFiles() method to retrieve the contents of the Reports folder. Assign the result to the reports array.
    [Visual Basic]
    Dim reports() As String = Directory.GetFiles(myReportPath, "*.rpt")
    [C#]
    string[] reports = Directory.GetFiles(reportPath, "*.rpt");
  8. On the next line, instantiate the SortedList class and pass it to its parent interface, IDictionary.
    [Visual Basic]
    Dim mySortedList As IDictionary = New SortedList
    [C#]
    IDictionary sortedList = new SortedList();
  9. Next, create a For Each loop that loops through each element of reports.
    [Visual Basic]
    For Each path As String In reports
    Next
    [C#]
    foreach (string path in reports)
    {
    }
  10. The next three lines of code use string manipulation methods to remove the file path from the name of each report. Insert these lines inside of the For Each loop.
    [Visual Basic]
    Dim reportNamePrefix As Integer = path.LastIndexOf("\") + 1
    Dim reportNameLength As Integer = path.Length - reportNamePrefix
    Dim reportName As String = path.Substring(reportNamePrefix, reportNameLength)
    [C#]
    int reportNamePrefix = path.LastIndexOf(@"\") + 1;
    int reportNameLength = path.Length - reportNamePrefix;
    string reportName = path.Substring(reportNamePrefix, reportNameLength);
  11. Still inside the For Each loop, add a line of code that adds the truncated report name to the sortedList IDictionary.
    [Visual Basic]
    mySortedList.Add(path, reportName)
    [C#]
    sortedList.Add(path, reportName);

Binding the Sorted List to the DropDownList Control

In this section, you learn how to bind the IDictionary data source to the DropDownList control. First, you define a relationship between the elements of the IDictionary collection and the DropDownList fields. Then, you bind the IDictionary data source to the DropDownList control.

  1. Outside of the foreach loop, add code to assign the Value field of the DropDownList to the key property of the sortedList instance.
    [Visual Basic]
    reportsList.DataTextField = "value"
    [C#]
    reportsList.DataTextField = "value"; 
  2. Next, assign the Value field of the DropDownList control to the key property of the sortedList instance.
    [Visual Basic]
    reportsList.DataValueField = "key"
    [C#]
    reportsList.DataValueField = "key"; 
  3. Assign the SortedList instance to the dataSource property of the DropDownList control.
    [Visual Basic]
    reportsList.DataSource = mysortedList
    [C#]
    reportsList.DataSource = sortedList;
  4. Finally, bind the data source to the DropDownList.
    [Visual Basic]
    reportsList.DataBind()
    [C#]
    reportsList.DataBind(); 

    You have now completed the contents of the If Not IsPostBack conditional block. In the next section, you will write code inside of the Else conditional block to define what happens if the page is refreshed without assigning a new value to the ReportDocument instance.

  5. Inside of the else statement, assign the report document instance to the report that is currently held in session.
    [Visual Basic]
    myReportDocument = CType(Session("myReportDocument"), ReportDocument)
    [C#]
    reportDocument = (ReportDocument)Session["reportDocument"]; 
    Note    Because Session only returns generic objects, you must cast the report to a report type. Whether you use embedded or non-embedded reports, cast the retrieved object to the ReportDocument type.
  6. On the next line, bind the ReportDocument instance to the CrystalReportViewer control.
    [Visual Basic]
    myCrystalReportViewer.ReportSource = myReportDocument
    [C#]
    crystalReportViewer.ReportSource = reportDocument;
  7. From the File menu, select Save All.
  8. From the Build menu, select Build Solution.
  9. If you have any build errors, go ahead and fix them now.

In the next section, you add a click event to bind the report based on selections from the reportsList DropDownList control.

Continue to Adding a Click Event to Bind the Report.

See Also

Other Tutorials | Tutorials and Sample Code | Tutorials Sample Code Directory

Show: