Populating the DropDownList Control

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

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

  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](ms225622\(v=vs.90\).md) for the locations of the sample reports.


> [!NOTE]
> <P>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 <A href="ms225528(v=vs.90).md">What Needs to be Verified?</A>.</P>
  1. Click the Hierarchical Grouping.rpt file to select it, and then click Open.
The Hierarchical Grouping.rpt file is added to your project.
  1. Add two more reports from the Sample Reports directory before moving to the next procedure.

To populate 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" or "using" declaration to the top of the class for the System.IO and System.Collections namespaces.

    Imports System.IO
    Imports System.Collections
    
    using System.IO;
    using System.Collections;
    
  4. At the class level, create a new ReportDocument variable, reportDocument.

``` vb
Private myReportDocument As ReportDocument
```

``` csharp
private ReportDocument reportDocument;
```
  1. Within the page_init() method, create a Not IsPostBack conditional block.
> [!NOTE]
> <P>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.</P>


``` vb
If Not IsPostBack Then
Else
End If
```

``` csharp
if(!IsPostBack)
{
}
else
{
}
```
  1. Within the Not IsPostBack conditional block, create a String variable that will hold the path to your reports folder.
> [!NOTE]
> <P>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/.</P>


``` vb
Dim myReportPath As String = Server.MapPath("Reports/")
```

``` csharp
string reportPath = Server.MapPath("Reports/");
```
  1. Use the Directory.GetFiles() method to retrieve the contents of the Reports folder. Assign the result to the reports array.
``` vb
Dim reports() As String = Directory.GetFiles(myReportPath, "*.rpt")
```

``` csharp
string[] reports = Directory.GetFiles(reportPath, "*.rpt");
```
  1. On the next line, instantiate the SortedList class and pass it to its parent interface, IDictionary.

    Dim mySortedList As IDictionary = New SortedList
    
    IDictionary sortedList = new SortedList();
    
  2. Next, create a For Each loop that loops through each element of reports and removes the file path from the name of each report..

    For Each path As String In reports
        Dim reportNamePrefix As Integer = path.LastIndexOf("\") + 1
        Dim reportNameLength As Integer = path.Length - reportNamePrefix
        Dim reportName As String = path.Substring(reportNamePrefix,reportNameLength)
        mySortedList.Add(path, reportName)
    Next
    
    foreach (string path in reports)
    {
        int reportNamePrefix = path.LastIndexOf(@"\") + 1;
        int reportNameLength = path.Length - reportNamePrefix;
        string reportName = path.Substring(reportNamePrefix, reportNameLength);
        sortedList.Add(path, reportName);
    }
    

To bind 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.
``` vb
reportsList.DataTextField = "value"
```

``` csharp
reportsList.DataTextField = "value"; 
```
  1. Next, assign the Value field of the DropDownList control to the key property of the sortedList instance.
``` vb
reportsList.DataValueField = "key"
```

``` csharp
reportsList.DataValueField = "key"; 
```
  1. Assign the SortedList instance to the dataSource property of the DropDownList control.
``` vb
reportsList.DataSource = mysortedList
```

``` csharp
reportsList.DataSource = sortedList;
```
  1. Finally, bind the data source to the DropDownList.

    reportsList.DataBind()
    
    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.

  2. Inside of the else statement, assign the report document instance to the report that is currently held in session.

> [!NOTE]
> <P>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.</P>


``` vb
myReportDocument = CType(Session("myReportDocument"),
ReportDocument)
```

``` csharp
reportDocument = (ReportDocument)Session["reportDocument"]; 
```
  1. On the next line, bind the ReportDocument instance to the CrystalReportViewer control.

    myCrystalReportViewer.ReportSource = myReportDocument
    
    crystalReportViewer.ReportSource = reportDocument;
    
  2. From the File menu, select Save All.

  3. From the Build menu, select Build Solution.

  4. If you have any build errors, go ahead and fix them now.

To create a Reports folder

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

  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](ms225622\(v=vs.90\).md) for the locations of the sample reports.


> [!NOTE]
> <P>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 <A href="ms225528(v=vs.90).md">What Needs to be Verified?</A>.</P>
  1. Click the Hierarchical Grouping.rpt file to select it, and then click Open.
The Hierarchical Grouping.rpt file is added to your project.
  1. Add two more reports from the Sample Reports directory before moving to the next procedure.

To populate 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" or "using" declaration to the top of the class for the System.IO and System.Collections namespaces.

    Imports System.IO
    Imports System.Collections
    
    using System.IO;
    using System.Collections;
    
  4. At the class level, create a new ReportDocument variable, reportDocument.

``` vb
Private myReportDocument As ReportDocument
```

``` csharp
private ReportDocument reportDocument;
```
  1. Within the page_init() method, create a Not IsPostBack conditional block.
> [!NOTE]
> <P>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.</P>


``` vb
If Not IsPostBack Then
Else
End If
```

``` csharp
if(!IsPostBack)
{
}
else
{
}
```
  1. Within the Not IsPostBack conditional block, create a String variable that will hold the path to your reports folder.
> [!NOTE]
> <P>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/.</P>


``` vb
Dim myReportPath As String = Server.MapPath("Reports/")
```

``` csharp
string reportPath = Server.MapPath("Reports/");
```
  1. Use the Directory.GetFiles() method to retrieve the contents of the Reports folder. Assign the result to the reports array.
``` vb
Dim reports() As String = Directory.GetFiles(myReportPath, "*.rpt")
```

``` csharp
string[] reports = Directory.GetFiles(reportPath, "*.rpt");
```
  1. On the next line, instantiate the SortedList class and pass it to its parent interface, IDictionary.

    Dim mySortedList As IDictionary = New SortedList
    
    IDictionary sortedList = new SortedList();
    
  2. Next, create a For Each loop that loops through each element of reports and removes the file path from the name of each report..

    For Each path As String In reports
        Dim reportNamePrefix As Integer = path.LastIndexOf("\") + 1
        Dim reportNameLength As Integer = path.Length - reportNamePrefix
        Dim reportName As String = path.Substring(reportNamePrefix,reportNameLength)
        mySortedList.Add(path, reportName)
    Next
    
    foreach (string path in reports)
    {
        int reportNamePrefix = path.LastIndexOf(@"\") + 1;
        int reportNameLength = path.Length - reportNamePrefix;
        string reportName = path.Substring(reportNamePrefix, reportNameLength);
        sortedList.Add(path, reportName);
    }
    

To bind 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.
``` vb
reportsList.DataTextField = "value"
```

``` csharp
reportsList.DataTextField = "value"; 
```
  1. Next, assign the Value field of the DropDownList control to the key property of the sortedList instance.
``` vb
reportsList.DataValueField = "key"
```

``` csharp
reportsList.DataValueField = "key"; 
```
  1. Assign the SortedList instance to the dataSource property of the DropDownList control.
``` vb
reportsList.DataSource = mysortedList
```

``` csharp
reportsList.DataSource = sortedList;
```
  1. Finally, bind the data source to the DropDownList.

    reportsList.DataBind()
    
    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.

  2. Inside of the else statement, assign the report document instance to the report that is currently held in session.

> [!NOTE]
> <P>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.</P>


``` vb
myReportDocument = CType(Session("myReportDocument"),
ReportDocument)
```

``` csharp
reportDocument = (ReportDocument)Session["reportDocument"]; 
```
  1. On the next line, bind the ReportDocument instance to the CrystalReportViewer control.

    myCrystalReportViewer.ReportSource = myReportDocument
    
    crystalReportViewer.ReportSource = reportDocument;
    
  2. From the File menu, select Save All.

  3. From the Build menu, select Build Solution.

  4. If you have any build errors, go ahead and fix them now.