Search code examples
crystal-reportscrystal-reports-2013

Pass value in Crystal report


I add formula field region,fromdate,todate in report

i.e. UPDATED IMAGE

UPDATED IMAGE

Also I try this another method:

 protected void Report_Click(object sender, EventArgs e)
        {

  data crystalReport = new data();    
 crystalReport.DataDefinition.FormulaFields["region"].Text = regiondrop.SelectedValue;
        crystalReport.DataDefinition.FormulaFields["fromdate"].Text = fromdate.Value;
        crystalReport.DataDefinition.FormulaFields["todate"].Text = todate.Value;

BindReport(crystalReport,Convert.ToDateTime(fromdate.Value), Convert.ToDateTime(todate.Value), regiondrop.SelectedValue);

        }

This show error on page:

Error

error image

This field name is not known. Details: errorKind Error in File temp_2c6994eb-49ef-432f-bfd7-af0eb80dc7ec 4032_6896_{5E54477E-F078-41DF-BD52-AF042B96DA53}.rpt: Error in formula fromdate: '{DataTable1.StartDate}' This field name is not known. Details: errorKind

Now this is working finally with the help of @Furtiro

public  void BindReport(ReportDocument crystalReport, DateTime fromdate, DateTime todate, string region)
    {

        T1 t = new T1();

        DateTime fdate = new DateTime(fromdate.Year, fromdate.Month, fromdate.Day, 0, 0, 0);
        DateTime tdate = new DateTime(todate.Year, todate.Month, todate.Day, 23, 59, 59);
        List<griddataresult_Result> dsc = t.griddataresult(fdate, tdate, region).ToList();
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("OwnerName", typeof(string));
        dt.Columns.Add("RegNo", typeof(string));


        foreach (var c in dsc)
        {

            dt.Rows.Add(c.ID, c.OwnerName, c.RegNo, c.total_voilatio, c.MileageAccumlation, c.MaxSpeed);
        }

        crystalReport.DataDefinition.FormulaFields["region"].Text = "'" + region + "'";
        crystalReport.DataDefinition.FormulaFields["fromdate"].Text = "'" + fromdate + "'";
        crystalReport.DataDefinition.FormulaFields["todate"].Text = "'" + todate + "'";

        crystalReport.SetDataSource(dt);
        CrystalReportViewer1.ReportSource = crystalReport;
        CrystalReportViewer1.DataBind();
    }

Solution

  • You can instanciate your report like an object , replace your line :

     ReportDocument crystalReport = new ReportDocument();
    

    By :

    data crystalReport = new data();
    

    Here data is the name of you .rpt class.

    *Update *

    You don't understand that ReportDocument is an object, your object here will be your data class. So all the lines when you set doc = crystalreport are useless because you don't work on your crystalReport object but on other objects.

    Second update :

    I didn't see your access to the formula, you're not supposed to write the "@' in the name of your formula. My bad sorry , here's your working code :

    protected void Report_Click(object sender, EventArgs e)
        {
    
            data crystalReport = new data();
    
             crystalReport.DataDefinition.FormulaFields["region"].Text = "'" + regiondrop.SelectedValue.ToString() + "'";
    
             crystalReport.DataDefinition.FormulaFields["fromdate"].Text = "'" + fromdate.Value.ToString() +"'";
    
             crystalReport.DataDefinition.FormulaFields["todate"].Text = "'"+ todate.Value.ToString() + "'";
    
    
             BindReport(crystalReport,Convert.ToDateTime(fromdate.Value), Convert.ToDateTime(todate.Value), regiondrop.SelectedValue);
    
        }
    

    Be careful about the dataType and parse when you write a formula in c# code, for example if you want to display some text in a formula dynamically you'll need to cast your data with quotes

    Like report.DataDefinition.FormulaFields["yourformula"].Text ="'Hello world'"; Dont forget the quotes, simple or double quotes will works.

    Best regards,