Display (Bind) Crystal Report based on DropDownList selection using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

How to use c# and asp.net to work with 7 crystal reports

From the list select 1 then use a button to view it.

 

Posted one month ago

Hi KatieNgoc,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:DropDownList ID="ddlReports" runat="server">
    <asp:ListItem Text="Select" Value="0"></asp:ListItem>
    <asp:ListItem Text="Employee" Value="Employee"></asp:ListItem>
    <asp:ListItem Text="Customer" Value="Customer"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnView" runat="server" Text="View Report" OnClick="FiViewReportlter" />
<hr />
<CR:CrystalReportViewer ID="crDetails" runat="server" AutoDataBind="true" EnableDatabaseLogonPrompt="False"
    EnableParameterPrompt="False" />

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

Code

C#

protected void FiViewReportlter(object sender, EventArgs e)
{
    ReportDocument crystalReport = new ReportDocument();
    if (ddlReports.SelectedItem.Text.ToLower() == "employee")
    {
        crystalReport.Load(Server.MapPath("~/EmployeeReport.rpt"));
        Employee dsEmployees = GetEmployeeReportData();
        crystalReport.SetDataSource(dsEmployees);
    }
    else if (ddlReports.SelectedItem.Text.ToLower() == "customer")
    {
        crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
        Customer dsCustomers = GetCustomerReportData();
        crystalReport.SetDataSource(dsCustomers);
    }
    crDetails.DisplayGroupTree = false;
    crDetails.ReportSource = crystalReport;
}

private Employee GetEmployeeReportData()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand("SELECT EmployeeID,LastName + ' ' + FirstName Name,Country FROM Employees");
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (Employee dsEmployees = new Employee())
            {
                sda.Fill(dsEmployees, "EmployeeTable");
                return dsEmployees;
            }
        }
    }
}

private Customer GetCustomerReportData()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand("SELECT TOP 10 CustomerID,ContactName Name,Country FROM Customers");
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (Customer dsCustomers = new Customer())
            {
                sda.Fill(dsCustomers, "CustomerTable");
                return dsCustomers;
            }
        }
    }
}

VB.Net

Protected Sub FiViewReportlter(ByVal sender As Object, ByVal e As EventArgs)
    Dim crystalReport As ReportDocument = New ReportDocument()
    If ddlReports.SelectedItem.Text.ToLower() = "employee" Then
        crystalReport.Load(Server.MapPath("~/EmployeeReport.rpt"))
        Dim dsEmployees As Employee = GetEmployeeReportData()
        crystalReport.SetDataSource(dsEmployees)
    ElseIf ddlReports.SelectedItem.Text.ToLower() = "customer" Then
        crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
        Dim dsCustomers As Customer = GetCustomerReportData()
        crystalReport.SetDataSource(dsCustomers)
    End If

    crDetails.DisplayGroupTree = False
    crDetails.ReportSource = crystalReport
End Sub

Private Function GetEmployeeReportData() As Employee
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,LastName + ' ' + FirstName Name,Country FROM Employees")
    Using con As SqlConnection = New SqlConnection(conString)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsEmployees As Employee = New Employee()
                sda.Fill(dsEmployees, "EmployeeTable")
                Return dsEmployees
            End Using
        End Using
    End Using
End Function

Private Function GetCustomerReportData() As Customer
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 10 CustomerID,ContactName Name,Country FROM Customers")
    Using con As SqlConnection = New SqlConnection(conString)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsCustomers As Customer = New Customer()
                sda.Fill(dsCustomers, "CustomerTable")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function

Screenshot