Display record based on condition in ASP.Net Crystal Report

Ikramshams
 
on Nov 12, 2021 10:43 PM
Sample_596799.zip
1130 Views

I am displaying records in Crystal Report from HeaderTable and LinesTable.

These tables are linked with ID field.

In LinesTable, there is field status.

Its values are 1, 2, 3 and 4. I want to show the only status=2 records in Crystal Report.

How I will do it.

Please suggest me.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Nov 14, 2021 10:18 PM

In the select query apply where condition to fill the dataset with record having status=2.

dharmendr
 
on Nov 19, 2021 01:09 AM
on Nov 19, 2021 01:11 AM

Hi Ikramshams,

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

In this example i am displaying records having Country with value USA.

Database

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

Download Northwind Database

HTML

<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />

Namespaces

C#

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

VB.Net

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

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
    Customers dsCustomers = GetData("SELECT * FROM Customers WHERE Country = 'USA'");
    crystalReport.SetDataSource(dsCustomers);
    CrystalReportViewer1.ReportSource = crystalReport;
}
 
private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con; 
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim crystalReport As New ReportDocument()
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
    Dim dsCustomers As Customers = GetData("SELECT * FROM Customers WHERE Country = 'USA'")
    crystalReport.SetDataSource(dsCustomers)
    CrystalReportViewer1.ReportSource = crystalReport
End Sub
 
Private Function GetData(query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con 
            sda.SelectCommand = cmd
            Using dsCustomers As New Customers()
                sda.Fill(dsCustomers, "DataTable1")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function

Refer below article for more details on binding Crystal Report.

Generate Crystal Reports using Strongly Typed DataSet in ASP.Net using C# and VB.Net