[Solved] Missing Parameter values error while exporting ASP.Net Crystal Report to PDF

Bhavesh23
 
on Aug 24, 2022 05:00 AM
841 Views

i have displayed the between date (From and to date) on crystal report using below code and added textboxes on crystal report, but when i export the crystal report the  From and To Date are disappear from the exported pdf file.

when the crystal report display then the From and To Date are Display but when i press crystal report's print button or export button then the From and To Date are disappear from the exported pdf file.

i have not used any update panel.

how to solve this kindly help me.

TextObject fromdate = (TextObject)crpt.ReportDefinition.Sections["Section1"].ReportObjects["txtstartdate"];
fromdate.Text = txtstartdate.Text;
TextObject todate = (TextObject)crpt.ReportDefinition.Sections["Section1"].ReportObjects["txtenddate"];
todate.Text = txtenddate.Text;

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
PrinceG
 
on Aug 25, 2022 12:39 AM
on Aug 25, 2022 12:45 AM

Hi Bhavesh23,

Please Refer Below Sample.

Note:I have created sample by refering the below Article. For more details pleaser refer below Article.

Crystal Report ASP.Net Example using DataSet or DataTable in C# VB.Net and Visual Studio 2010

Report Design

You need right click on the rpt -> Insert and add text object inside section 2 for displaying the From and To date.

HTML

<asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox>
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />

Namespaces

C#

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

VB.Net

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

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    this.BindCrystalReport();
}

protected void Search(object sender, EventArgs e)
{
    this.BindCrystalReport();
}

private void BindCrystalReport()
{
    ReportDocument crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/Sales.rpt"));
    DataSet1 dsCustomers = GetData(txtFromDate.Text, txtToDate.Text);
    crystalReport.SetDataSource(dsCustomers);
    CrystalReportViewer1.ReportSource = crystalReport;

    ((TextObject)crystalReport.ReportDefinition.Sections["Section2"].ReportObjects["txtFromDate"]).Text = txtFromDate.Text;
    ((TextObject)crystalReport.ReportDefinition.Sections["Section2"].ReportObjects["txtToDate"]).Text = txtToDate.Text;
}

public DataSet1 GetData(string fromDate, string toDate)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        string query = "SELECT bno,name,cdate,totalgst,disc,paidamt,pymtmode FROM Sales";
        using (SqlCommand cmd = new SqlCommand())
        {
            if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
            {
                query += " WHERE CONVERT(DATETIME,cdate,103) BETWEEN CONVERT(DATETIME,@FromDate,103) AND CONVERT(DATETIME,@ToDate,103)";
                cmd.Parameters.AddWithValue("@FromDate", fromDate);
                cmd.Parameters.AddWithValue("@ToDate", toDate);
            }
            cmd.CommandText = query;
            cmd.Connection = con;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataSet1 dsCustomers = new DataSet1())
                {
                    sda.Fill(dsCustomers, "DataTable1");
                    return dsCustomers;
                }
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Me.BindCrystalReport()
End Sub

Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
    Me.BindCrystalReport()
End Sub

Private Sub BindCrystalReport()
    Dim crystalReport As ReportDocument = New ReportDocument()
    crystalReport.Load(Server.MapPath("~/Sales.rpt"))
    Dim dsCustomers As DataSet1 = GetData(txtFromDate.Text, txtToDate.Text)
    crystalReport.SetDataSource(dsCustomers)
    CrystalReportViewer1.ReportSource = crystalReport
    CType(crystalReport.ReportDefinition.Sections("Section2").ReportObjects("txtFromDate"), TextObject).Text = txtFromDate.Text
    CType(crystalReport.ReportDefinition.Sections("Section2").ReportObjects("txtToDate"), TextObject).Text = txtToDate.Text
End Sub

Public Function GetData(ByVal fromDate As String, ByVal toDate As String) As DataSet1
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Dim query As String = "SELECT bno,name,cdate,totalgst,disc,paidamt,pymtmode FROM Sales"
        Using cmd As SqlCommand = New SqlCommand()
            If Not String.IsNullOrEmpty(fromDate) AndAlso Not String.IsNullOrEmpty(toDate) Then
                query += " WHERE CONVERT(DATETIME,cdate,103) BETWEEN CONVERT(DATETIME,@FromDate,103) AND CONVERT(DATETIME,@ToDate,103)"
                cmd.Parameters.AddWithValue("@FromDate", fromDate)
                cmd.Parameters.AddWithValue("@ToDate", toDate)
            End If
            cmd.CommandText = query
            cmd.Connection = con
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dsCustomers As DataSet1 = New DataSet1()
                    sda.Fill(dsCustomers, "DataTable1")
                    Return dsCustomers
                End Using
            End Using
        End Using
    End Using
End Function

Screenshot