我有CrystalReport页面,下面的代码只是通过FormLoad加载一次并使用!Page.IsPostBack并且如果我再次尝试重新加载它给我错误“加载报告失败。”
if (!Page.IsPostBack)
{
cmd = new SqlCommand("SELECT * FROM [tbJournals]", cnn);
cnn.Open();
da = new SqlDataAdapter(cmd);
dt = new DataTable("tbl");
da.Fill(dt);
cnn.Close();
_rdStudents = new ReportDocument();
_rdStudents.Load(Server.MapPath("~\\Accounting\\Journal_Report.rpt"));
_rdStudents.SetDatabaseLogon("sa", "password", ".\\SQLEXPRESS", "GoldenDeveloper");
_rdStudents.SetDataSource(dt);
CrystalReportViewer1.ReportSource = _rdStudents;
CrystalReportViewer1.RefreshReport();
}
但是我想要做的是把这个代码放在CommandButton下来改变搜索变量以使SQL语句成为例如
SELECT * FROM [tbJournals] where [Date] = '"+ txtDate.Text +"'
但是,一旦我将代码而不是FormLoad放到CommandButton中,就会给出错误“加载报告失败。”
提前致谢....
答案 0 :(得分:0)
我想下一次,当你重装时那么
(!IsPostBack)
{
//This works only when the first time page loads.
}
因此编写的代码将不会被执行。尝试在Page_Load中编写代码并在此函数之后编写代码 像:
protected void Page_Load(object sender, EventArgs e)
{
cmd = new SqlCommand("SELECT * FROM [tbJournals]", cnn);
cnn.Open();
da = new SqlDataAdapter(cmd);
dt = new DataTable("tbl");
da.Fill(dt);
cnn.Close();
_rdStudents = new ReportDocument();
_rdStudents.Load(Server.MapPath("~\\Accounting\\Journal_Report.rpt"));
_rdStudents.SetDatabaseLogon("sa", "password", ".\\SQLEXPRESS", "GoldenDeveloper");
_rdStudents.SetDataSource(dt);
CrystalReportViewer1.ReportSource = _rdStudents;
CrystalReportViewer1.RefreshReport();
}
答案 1 :(得分:0)
最后解决了,问题是在报告路径中,我将“CrystalReportViewer”报告路径留空,并使用Server.MapPath将_rdStudents.Load(Server.MapPath("Journal_Report.rpt"));
推送到报告路径