You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2019中Crystal Report始终显示首次打开订单不刷新问题求助

修复方案

你遇到的是VS2019对应版本Crystal Reports运行时的默认缓存机制导致的旧数据残留问题,可按以下步骤修改:

  • 关闭参数复用配置
    你当前代码中ReuseParameterValuesOnRefresh = true会让报表刷新时复用历史参数,直接修改为false即可。
  • 每次加载前清空旧缓存
    执行报表加载逻辑前,先清空之前的报表源、参数集合,避免旧数据残留。
  • 优先给报表文档直接传参
    只给Viewer设置参数可能出现和底层报表文档不同步的问题,改为直接给reportDocument实例设置参数更稳定。
  • 调整页面生命周期逻辑
    确认从QueryString取值给文本框赋值的逻辑,在每次页面加载时都会执行,不要包裹在if(!IsPostBack)判断中,避免使用ViewState存储的旧参数值。
  • 释放报表实例资源
    页面卸载时主动关闭、释放报表文档实例,避免会话中缓存旧报表。

修改后参考代码

if (Convert.ToInt32(TXTDEPTID.Text) == 0)
{
    // 清空历史报表资源与参数
    CrystalReportViewer1.ReportSource = null;
    ParameterFields parameterFields = new ParameterFields();
    ParameterField parameterField1 = new ParameterField();
    ParameterDiscreteValue parameterDiscreteValue1 = new ParameterDiscreteValue();

    parameterField1.Name = "@ORDER_ID";
    parameterDiscreteValue1.Value = TXTORDERID.Text.Trim();
    parameterField1.CurrentValues.Add(parameterDiscreteValue1);
    parameterFields.Add(parameterField1);

    ReportDocument reportDocument = new ReportDocument();
    reportDocument.Load(Server.MapPath("~/RPT/RPTCREDITRESULTS.rpt"));
    // 直接给报表文档设置参数
    reportDocument.SetParameterValue("@ORDER_ID", TXTORDERID.Text.Trim());
    reportDocument.SetDatabaseLogon("sa", "123456");

    ConnectionInfo connectionInfo = new ConnectionInfo();
    connectionInfo.ServerName = "server";
    connectionInfo.DatabaseName = "DB";
    connectionInfo.Password = "123456";
    connectionInfo.UserID = "sa";
    connectionInfo.Type = ConnectionInfoType.SQL;
    connectionInfo.IntegratedSecurity = false;
    for (int index = 0; index < CrystalReportViewer1.LogOnInfo.Count; ++index)
    {
        CrystalReportViewer1.LogOnInfo[index].ConnectionInfo = connectionInfo;
    }

    CrystalReportViewer1.ReuseParameterValuesOnRefresh = false;
    CrystalReportViewer1.ToolPanelView = ToolPanelViewType.None;
    CrystalReportViewer1.ParameterFieldInfo = parameterFields;
    CrystalReportViewer1.ReportSource = reportDocument;
    CrystalReportViewer1.RefreshReport();

    // 页面卸载时释放资源避免缓存
    this.Unload += (s, e) => {
        reportDocument.Close();
        reportDocument.Dispose();
    };
}

如果上述修改后还有问题,可在web.config的appSettings节点添加配置,禁用报表缓存:

<appSettings>
  <add key="CrystalImageCleaner-AutoStart" value="true" />
  <add key="CrystalImageCleaner-Sleep" value="60000" />
  <add key="CrystalImageCleaner-Age" value="120000" />
</appSettings>

内容的提问来源于stack exchange,提问作者Ziad Adnan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 12:36:04