I am using Crystal Reports inside Visual Studio 2010 with ASP.NET.
I have a DataSet that retrieves data from a database and seems to load into the report just fine. When I view a report in my browser, all records load correctly.
I need to add a parameter for user input in order to filter out entries in the report. The problem is that when I add any parameter to the report, the viewer cannot find any records. This is BEFORE . I do something using the selection wizard.
I added a drop-down list option that simply contains the numbers 1, 2, and 3. Without anything in the selection expert, this should not affect anything. But for any reason, when this is indicated in the report, records are not selected. As soon as I remove the parameter, it works again.
I add the parameter and its values through the field explorer, if that matters.
It is odd that the implementation was completed when I had a report that retrieved data directly from the database, rather than creating the DataSet first. The reason I cannot use this implementation is because it constantly requested information to enter the database. Even when I predetermined the information. I switched to a DataSet to avoid a login prompt.
I believe that there should be something wrong with my DataSet implementation. The relevant code is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
public partial class reportPage : System.Web.UI.Page
{
private ReportDocument rpt;
private string mapPath;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
mapPath = "CrystalReport1.rpt";
ConfigureCrystalReport();
}
}
private void ConfigureCrystalReport()
{
myDataSet ds = new myDataSet();
myDataSetTableAdapters.myTableTableAdapter dsTA = new myDataSetTableAdapters.myTableTableAdapter();
dsTA.Fill(ds.myTable);
rpt = new ReportDocument();
string reportPath = Server.MapPath(mapPath);
rpt.Load(reportPath);
DataTable dt = ds.myTable;
Response.Write("<script>alert('Table has "+ dt.Rows.Count.ToString() +" rows');</script>");
rpt.SetDataSource(dt);
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.ParameterPanel;
CrystalReportViewer1.HasToggleGroupTreeButton = false;
}
response.write , , . , , , .
, .
EDIT. , , . , , .
, , .
EDIT 2. DataSet, . , DataSets, ... .
:
string sConnectionString;
sConnectionString = "Password=mYp@ssw0rd*;User ID=sa;" + "Initial Catalog=databaseName;" + "Data Source=serverName";
SqlConnection conn = new SqlConnection(sConnectionString);
conn.Open();
SqlDataAdapter sDA = new SqlDataAdapter("Select * from myTable", conn);
conn.Close();
myDataSet ds = new myDataSet();
sDA.Fill(ds, "myTable");
rpt = new ReportDocument();
string reportPath = Server.MapPath(mapPath);
rpt.Load(reportPath);
rpt.SetDataSource(ds);
: . - , . ( ), .
3: :
rpt.SetParameterValue("par1", 1);
, DataSet... . , , 1. , , .
, . , rpt.SetDataSource() CrystalReportsViewer1.ReportSource = rpt.
4. user4663200 , mapPath null, . , , : ", , ". . , Page_Init() Page_Load().
, - , , . , , .
Page_Init, , .
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
private ReportDocument rpt;
private myDataSet ds;
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ds = new myDataSet();
myDataSetTableAdapters.myTableTableAdapter dsTA = new myDataSetTableAdapters.myTableTableAdapter();
dsTA.Fill(ds.myTable);
rpt = new ReportDocument();
string reportPath = Server.MapPath("myReport.rpt");
rpt.Load(reportPath);
Session.Add("dataSet", ds);
Session["dataSet"] = ds;
rpt.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rpt;
}
else
{
ds = (myDataSet)Session["Report"];
rpt = new ReportDocument();
string reportPath = Server.MapPath("myReport.rpt");
rpt.Load(reportPath);
rpt.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rpt;
}
}
, REPORT , , Sessionstate InProc. "StateServer". , DataSet , . .
EDIT 4.5 sessionState InProc, . , , .
EDIT 4.75 Page_Init(), .
[ "" ]; .
Page_Init(), rpt = (ReportDocument) [ "" ]; [ "" ] . , , . , .
, , .
EDIT 4.825. . :
protected void Page_UnLoad(object sender, EventArgs e)
{
try
{
rpt.Close();
rpt.Dispose();
}
catch { }
}
Session [ "Report" ] null.
, Page_UnLoad , . rpt.Close(); rpt, Session rpt. , rpt is Disposed(), Session [ "Report" ] .
, , , .
5: , , .
, , .
. .
private ReportDocument rpt;
private myDataSet ds;
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Report"] = null;
}
if (Session["Report"] == null)
{
ds = new myDataSet();
myDataSetTableAdapters.myTableTableAdapter dsTA = new myDataSetTableAdapters.myTableTableAdapter();
DataTable dt = dsTA.GetData();
rpt = new ReportDocument();
rpt.Load(Server.MapPath("myReport.rpt"));
rpt.SetDataSource(dt);
Session.Add("Report", rpt);
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.ParameterPanel;
CrystalReportViewer1.HasToggleGroupTreeButton = false;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
rpt = (ReportDocument)Session["Report"];
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.ParameterPanel;
CrystalReportViewer1.HasToggleGroupTreeButton = false;
}
}
protected void butReport_Click(object sender, EventArgs e)
{
if (Session["Report"] == null)
{
ds = new myDataSet();
myDataSetTableAdapters.myTableTableAdapter dsTA = new myDataSetTableAdapters.myTableTableAdapter();
DataTable dt = dsTA.GetData();
rpt = new ReportDocument();
rpt.Load(Server.MapPath("myReport.rpt"));
rpt.SetDataSource(dt);
Session.Add("Report", rpt);
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.ParameterPanel;
CrystalReportViewer1.HasToggleGroupTreeButton = false;
}
else
{
rpt = (ReportDocument)Session["Report"];
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.ParameterPanel;
CrystalReportViewer1.HasToggleGroupTreeButton = false;
}
}
: DataTable dt = dsTA.GetData();
, DataSet rpt. , , DataTable GetData() .
. , , , .