2016-08-09 49 views
0

我在jquery中調用了這個靜態web方法,並使用jquery填充表格 數據成功顯示在表格中。在web表單頁面中有一個下拉式選擇器和兩個日期選擇器(從date &到date)根據這些值的選擇數據顯示在表格中現在我想創建水晶報表的時候我點擊導出按鈕,然後是表格數據,下拉值&日期選擇器值將在報告在水晶報告中調用會話

我存儲在會話和呼叫DT希望在水晶報表打電話,我在水晶報表中添加的CrystalReportViewer

我有網靜態的WebMethod即

顯示
[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

現在我在網頁表單

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); 

    } 

添加此當我在搜索點擊率則表數據顯示,然後我當我點擊報告按鈕表消失,空報告顯示

+0

首先創建水晶報告,然後創建ReportDocument對象,通過給你添加水晶報告的路徑加載你的crstalreport並傳遞參數,如objreport.SetParameterValue(「@ frmdate」,frmdate);然後綁定crstalreportviewer.ReportSource = objreport; crstalreportviewer.Databind(); –

+0

檢查更新請@ManishGoswami – user6628729

+0

檢查更新@ManishGoswami – user6628729

回答

0

剛添加您的ButtonClick事件中下面的代碼

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

+0

但我從日期時間格式的日期..我的意思是日期時間選擇器 – user6628729

+0

沒有問題,請將datetime數據類型傳遞給SP中的frmdate和@todate或保持原樣varchar –

+0

爲什麼要創建ApplyCRLogin?我使用LINQ和數據庫名稱用戶ID等已經在web.congif配置 – user6628729