Search code examples
c#asp.netcrystal-reportscrystal-reports-2013

Call session in crystal report


I call this static web method in jquery and I populate the table with the help of jquery Data is successfully display in table . In web form page there is one drop-down and two date-picker (from date & to date) according to selection of these values data is display in table now I want to create crystal report when I click on export button then table data ,drop down value & date-picker value will be display in report

I store dt in session and call that want to call in crystal report and I add crystalreportviewer in crystal report

I have web static webmethod i.e

[WebMethod]
public static string search_data(DateTime fromdate, DateTime todate, string region)
{
        try
        {
            string result = "";
            Ts1 td = new T1();
            DateTime frDate = new DateTime(fromdate.Year, fromdate.Month, fromdate.Day, 0, 0, 0);
            DateTime to_Date = new DateTime(todate.Year, todate.Month, todate.Day, 23, 59, 59);
            List<griddataresult_Result> dq = td.griddataresult(frDate, to_Date, 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 dq)
                {

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

             DataTable dtt= (DataTable)HttpContext.Current.Session["datagrid"];               
                result = DataSetToJSON(dt);              
            return result;
        }
        catch (Exception)
        {
            throw new Exception();
        }
 }

UPDATE

now i add this in webform

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


        TrackDataEntities1 t = new TrackDataEntities1();
        crystalReport.Load(Server.MapPath("data.rpt"));
        List<griddataresult_Result> dsc = t.griddataresult(fromdate, todate, region).ToList();
        crystalReport.SetDataSource(dsc);
        CrystalReportViewer1.ReportSource = crystalReport;
    }
    protected void Report_Click(object sender, EventArgs e)
    {

        DataTable dt = Session["datagrid"] as DataTable;
        ReportDocument crystalReport = new ReportDocument();
        //crystalReport.SetParameterValue("@fromdate", fromdate.Value);
        //crystalReport.SetParameterValue("@todate", todate.Value);
        //crystalReport.SetParameterValue("@region", regiondrop.SelectedValue);
        BindReport(crystalReport,Convert.ToDateTime(fromdate.Value), Convert.ToDateTime(todate.Value), regiondrop.SelectedValue);

    }

when i click on search then table data is display and then i when i click on report button table is disappear and blank report is display


Solution

  • Just add following code inside your ButtonClick Event

     protected void Button5_Click(object sender, EventArgs e)
         { 
        dynamic rpt = new ReportDocument();
        ApplyCRLogin(rpt);
        DataSet dtu = new DataSet();
            // Your Stored Procedure Here 
           // Bind dtu to the ObjReport
           rpt .Database.Tables["Command"].SetDataSource(dtu.Tables[0]);
         // Here Command is name which you can see inside Field Explorer in Crystal Report Design Mode 
           // Then Pass your SP parametrs 
            rpt.SetParameterValue("@dtFromDate", txtFromDate.Text);
            rpt.SetParameterValue("@dtToDate", txtToDate.Text);
    
            //After this Export your Crstal Report  to  PDF
          ExportToPDF(rpt);
         }
    
         public void ApplyCRLogin(CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt)
                {
                    CrystalDecisions.CrystalReports.Engine.Database oCRDb = oRpt.Database;
                    CrystalDecisions.CrystalReports.Engine.Tables oCRTables = oCRDb.Tables;
                    CrystalDecisions.Shared.TableLogOnInfo oCRTableLogonInfo;
                    CrystalDecisions.Shared.ConnectionInfo oCRConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
                    oCRConnectionInfo.DatabaseName = "DatabaseName";
                    oCRConnectionInfo.ServerName = "DBSRV_NAME";
                    oCRConnectionInfo.UserID = "UserID";
                    oCRConnectionInfo.Password = "Password";
                    foreach (CrystalDecisions.CrystalReports.Engine.Table oCRTable in oCRTables)
                    {
                        oCRTableLogonInfo = oCRTable.LogOnInfo;
                        oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo;
                        oCRTable.ApplyLogOnInfo(oCRTableLogonInfo);
                    }
                }
    

    How to Export Crystal Report to PDF