protected void Page_Init(object sender, EventArgs e)
{
//开始加载数据时,该条件成立,如果点击打印按钮后,该条件不成了,参数为null
if (Request.Form["sex"] != null) //接收上一页面传递的参数
{
string sex = Request.Form["sex"].ToString(); //根据上一个页面传递的参数查询数据
string sqlStr = "select * from Users where Usex='" + sex + "'";
DataSet ds = SqlHelper.Query(sqlStr, "Users"); //SqlHelper查询数据,返回DataSet对象 // 数据集
UserDataSet obj = new UserDataSet(); //将查询数据合并到数据集中的表
obj.Tables["Users"].Merge(ds.Tables["Users"]); CrystalReportSource1.ReportDocument.SetDataSource(obj.Tables["Users"]);
CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
}
}
显示数据后,点击水晶报表工具条上的打印按钮之后打印,出现以下错误:
注:打印机是正常的。
如果SQL查询条件中直接查询数据,不带传递参数,这样是可以正常打印的,如下:
protected void Page_Init(object sender, EventArgs e)
{
string sqlStr = "select * from Users where Usex='男'";
DataSet ds = SqlHelper.Query(sqlStr, "Users"); //SqlHelper查询数据,返回DataSet对象 // 数据集
UserDataSet obj = new UserDataSet(); //将查询数据合并到数据集中的表
obj.Tables["Users"].Merge(ds.Tables["Users"]); CrystalReportSource1.ReportDocument.SetDataSource(obj.Tables["Users"]);
CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
}但是如果根据上一页传递的参数,查询数据,就会出错!!! 这是什么原因,如果解决???
{
if (!IsPostBack) //接收上一页面传递的参数
{
string sex = Request.Form["sex"] != null?Request.Form["sex"].ToString():""; //根据上一个页面传递的参数查询数据
string sqlStr = "select * from Users where Usex='" + sex + "'";
DataSet ds = SqlHelper.Query(sqlStr, "Users"); //SqlHelper查询数据,返回DataSet对象 // 数据集
UserDataSet obj = new UserDataSet(); //将查询数据合并到数据集中的表
obj.Tables["Users"].Merge(ds.Tables["Users"]); CrystalReportSource1.ReportDocument.SetDataSource(obj.Tables["Users"]);
CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
}
}使用if (!IsPostBack) 判断,虽然可以解决参数为null的问题,但是这样在打印时会报错:
int MemberId = Program.Num_id;
da = new MySqlDataAdapter("select a.MemberId as MemberId,MemberName,Score,Type from member as a left join memscore as b on a.memberid = b.memberid where a.memberid =" + MemberId + "", conn);
da.Fill(Mydata, "MyReportDataTable");
crystalReport11.Load(Application.StartupPath + "CrystalReport1.rpt");//加载报表
crystalReport11.SetDataSource(Mydata);
crystalReportViewer1.ReportSource = crystalReport11;
crystalReportViewer1.ShowPrintButton = false;
try
{
PrintDocument print = new PrintDocument();
crystalReport11.PrintOptions.PrinterName = print.PrinterSettings.PrinterName;
crystalReport11.PrintToPrinter(1, true, 1, 1);//打印
}
catch (Exception ex)
{
MessageBoxEx.Show(ex.Message);
//throw;
}
如果不用IsPostBack判断判断,打印的过程中,页面重新加载,参数会变成null。这样就没有数据了。
private void Page_Init(object sender, EventArgs e)
{
CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport1.rpt"));
CrystalReportSource1.ReportDocument.SetParameterValue("", "");
CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
}protected override void OnInit(EventArgs e)
{}