Visual studio 2010 SQL Reporting Services报告仅在第二次单击时加载

Visual studio 2010 SQL Reporting Services报告仅在第二次单击时加载,visual-studio-2010,reporting-services,asp.net-3.5,Visual Studio 2010,Reporting Services,Asp.net 3.5,我有一个本地的.rdlc报告,它被操纵成在点击按钮时显示,但由于某种原因,该报告只在第二次点击按钮时显示。我不知道为什么第一次点击按钮时报告没有显示。。。这是我在单击按钮事件时调用的函数 private void ShowReport(string accountingCompanyId, string companyId, string approvalUnitId, DateTime startDate, DateTime finishDate, string supplierId,

我有一个本地的.rdlc报告,它被操纵成在点击按钮时显示,但由于某种原因,该报告只在第二次点击按钮时显示。我不知道为什么第一次点击按钮时报告没有显示。。。这是我在单击按钮事件时调用的函数

private void ShowReport(string accountingCompanyId, string companyId, string approvalUnitId, DateTime startDate, DateTime finishDate, string supplierId,
                    string documentNumber, string documentType, string documentState, string costCenterId, string chargingKeyId,
                    string dim1Value, string dim1Description, string dim1Id, string dim2Value, string dim2Description, string dim2Id,
                    string dim3Value, string dim3Description, string dim3Id, bool showDetails) {

        //this.ReportViewer1.Reset();

        //Set report mode for local processing.
        this.ReportViewer1.ProcessingMode = ProcessingMode.Local;

        ISettingsReader settingsReader = SettingsReaderFactory.Instance.CreateSettingsReader();
        this.ReportViewer1.LocalReport.ReportPath = settingsReader.GetSetting("ReportViewer", "FinancialReportPath" + (showDetails ? "" : "Small"), true, null);

        ReportsBL reports = new ReportsBL();

        // Clear out any previous datasources.
        this.ReportViewer1.LocalReport.DataSources.Clear();

        // Load the company dataSource.
        DataTable company = reports.GetCompanyDataSet(accountingCompanyId).Tables[0];
        ReportDataSource dataSourceCompany = new ReportDataSource(company.TableName, company);
        this.ReportViewer1.LocalReport.DataSources.Add(dataSourceCompany);

        // Load the dataSource.
        DataTable report = reports.GetReportFinanceiroSmallDataSet(companyId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, approvalUnitId, documentState, accountingCompanyId).Tables[0];
        ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report);
        this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport);

        this.ReportViewer1.LocalReport.Refresh();

        this.pnlReport.Visible = true;
    }
奇怪的是,如果我取消注释this.ReportViewer.Reset()行;那么无论我点击多少次,报告都不会显示。。。有人知道这是否正常吗?如何解决这个问题?
提前感谢,

我想问题可能是在呈现页面后触发了click事件。尝试在
Page\u Load
事件中调用该方法

protected void Page_Load(object sender, EventArgs e)
{
    if (IsCallback)
    {
        ShowReport(
            // params
        );
    }
}
如果这样做有效,您就知道这与执行顺序有关。

试试下面的代码:

  protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pstrType;
            pstrType = Request.QueryString["Type"];           
            LoadReport();
        }

        public class CustomReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
        {
            // local variable for network credential.
            private string _UserName;
            private string _PassWord;
            private string _DomainName;
            public CustomReportCredentials(string UserName, string PassWord, string DomainName)
            {
                _UserName = UserName;
                _PassWord = PassWord;
                _DomainName = DomainName;
            }
            public System.Security.Principal.WindowsIdentity ImpersonationUser
            {
                get
                {
                    return null;  // not use ImpersonationUser
                }
            }
            public System.Net.ICredentials NetworkCredentials
            {
                get
                {
                    // use NetworkCredentials
                    return new NetworkCredential(_UserName, _PassWord, _DomainName);
                }
            }
            public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
            {
                // not use FormsCredentials unless you have implements a custom autentication.
                authCookie = null;
                user = password = authority = null;
                return false;
            }
        }

        void LoadReport()
        {
            string strCompanyName = objSession.SelCompanyName;
            string strHeading = "";
            string strBranchName = objSession.SelBranchName;
            rptvwMain.ProcessingMode = ProcessingMode.Remote;
            rptvwMain.ServerReport.ReportServerCredentials = new CustomReportCredentials(AppConfig.ReportServerUserName, AppConfig.ReportServerPassword, AppConfig.ReportServerDomain);
            string strReportServerUrl = AppConfig.ReportServerUrl + AppConfig.ReportServerFolder;
            rptvwMain.ServerReport.ReportServerUrl = new Uri(strReportServerUrl);
            List<ReportParameter> parameters = new List<ReportParameter>();
    if (pstrType == "OB")
            {
                strHeading = "Ledger Opening Balance";
                rptvwMain.ServerReport.ReportPath = "/Account/OpeningBalance";
            }


            parameters.Add(new ReportParameter("FyId", Convert.ToInt16(objSession.FyId).ToString()));
            parameters.Add(new ReportParameter("AccountGroupId", cmbAccountGroup.SelectedValue));
            parameters.Add(new ReportParameter("LedgerId", cmbLedgerId.SelectedValue));
            parameters.Add(new ReportParameter("BranchId", Convert.ToInt64(objSession.BranchId).ToString()));
            parameters.Add(new ReportParameter("StDate", Convert.ToDateTime(RadDtpFromDate.SelectedDate).ToString()));
            parameters.Add(new ReportParameter("EnDate", Convert.ToDateTime(RadDtpToDate.SelectedDate).ToString()));
            parameters.Add(new ReportParameter("CompanyName", strCompanyName.ToString()));
            parameters.Add(new ReportParameter("BranchName", strBranchName.ToString()));
            parameters.Add(new ReportParameter("Heading",strHeading.ToString()));
            rptvwMain.ServerReport.SetParameters(parameters);

            rptvwMain.ServerReport.SetDataSourceCredentials(new[] { new DataSourceCredentials() { Name =AppConfig.ReportServerDataSource , UserId = AppConfig.ReportServerDSUserName, Password = AppConfig.ReportServerDSPassword } });
            rptvwMain.ShowZoomControl = true;
            rptvwMain.ServerReport.Refresh();
        }
    }
protectedvoid btnSubmit\u单击(对象发送方,事件参数e)
{
字符串pstrType;
pstrType=Request.QueryString[“Type”];
LoadReport();
}
公共类CustomReportCredentials:Microsoft.Reporting.WebForms.IReportServerCredentials
{
//网络凭据的局部变量。
私有字符串\u用户名;
私有字符串\u密码;
私有字符串_域名;
公共CustomReportCredentials(字符串用户名、字符串密码、字符串域名)
{
_用户名=用户名;
_密码=密码;
_域名=域名;
}
public System.Security.Principal.WindowsIdentity ImpersonationUser
{
得到
{
返回null;//不使用ImpersonationUser
}
}
public System.Net.ICredentials网络凭据
{
得到
{
//使用网络凭据
返回新的网络凭据(\u用户名、\u密码、\u域名);
}
}
public bool GetFormsCredentials(out Cookie authCookie、out string user、out string password、out string authority)
{
//除非您实现了自定义身份验证,否则不要使用FormsCredentials。
authCookie=null;
user=password=authority=null;
返回false;
}
}
void LoadReport()
{
字符串strCompanyName=objSession.SelCompanyName;
字符串strHeading=“”;
字符串strBranchName=objSession.SelBranchName;
rptvwMain.ProcessingMode=ProcessingMode.Remote;
rptvwMain.ServerReport.ReportServerCredentials=新的CustomReportCredentials(AppConfig.ReportServerUserName、AppConfig.ReportServerPassword、AppConfig.ReportServerDomain);
字符串strReportServerUrl=AppConfig.ReportServerUrl+AppConfig.ReportServerFolder;
rptvwMain.ServerReport.ReportServerUrl=新Uri(strReportServerUrl);
列表参数=新列表();
如果(pstrType==“OB”)
{
strHeading=“分类账期初余额”;
rptvwMain.ServerReport.ReportPath=“/Account/OpeningBalance”;
}
parameters.Add(新的ReportParameter(“FyId”,Convert.ToInt16(objSession.FyId).ToString());
添加(新报告参数(“AccountGroupId”,cmbAccountGroup.SelectedValue));
parameters.Add(新报告参数(“LedgerId”,cmblegrid.SelectedValue));
parameters.Add(新报告参数(“BranchId”,Convert.ToInt64(objSession.branchhid.ToString());
parameters.Add(新的ReportParameter(“StDate”,Convert.ToDateTime(RadDtpFromDate.SelectedDate).ToString());
parameters.Add(新的ReportParameter(“EnDate”,Convert.ToDateTime(RadDtpToDate.SelectedDate).ToString());
Add(新的ReportParameter(“CompanyName”,strCompanyName.ToString());
Add(新的ReportParameter(“BranchName”,strBranchName.ToString());
parameters.Add(新的ReportParameter(“Heading”,strHeading.ToString());
rptvwMain.ServerReport.SetParameters(参数);
rptvwMain.ServerReport.setDataSourceRedentials(new[]{new DataSourceRedentials(){Name=AppConfig.ReportServerDataSource,UserId=AppConfig.ReportServerDSUserName,Password=AppConfig.ReportServerDSPassword}});
rptvwMain.ShowZoomControl=true;
rptvwMain.ServerReport.Refresh();
}
}

经过多次尝试和错误后,我通过调用pageload事件的databind()方法使其正常工作。在pageload数据绑定(未设置数据源)之后,后续 单击按钮将按预期开始工作

我包含了代码,以防其他人遇到此错误。(想知道为什么我需要在页面加载中进行数据绑定…)

更新2

我终于解决了这个问题。。。原来.rdlc是从一个旧的2005.rdl报表迁移而来的,新的.rdlc包含旧的报表参数+sql,这在某种程度上扰乱了报表加载。在我删除了未使用的报表参数+sql之后,一切都开始正常工作。。。我已经更新了下面的代码,以反映我现在在项目中使用的内容

protected void Page_Load(object sender, System.EventArgs e) {

}

protected void btGenStats_Click(object sender, System.EventArgs e) {

    ...

    this.ShowReport(accountingCompanyId, companyId, approvalUnitId, startDate, finishDate, supplierId, documentNumber, documentType, documentState,
                    costCenterId, chargingKeyId, dim1, dim1Desc, dim1Id, dim2, dim2Desc, dim2Id, dim3, dim3Desc, dim3Id, showDetails);
}

private void ShowReport(string accountingCompanyId, string companyId, string approvalUnitId, DateTime startDate, DateTime finishDate, string supplierId, string documentNumber, string documentType, string documentState, string costCenterId, string chargingKeyId, string dim1Value, string dim1Description, string dim1Id, string dim2Value, string dim2Description, string dim2Id, string dim3Value, string dim3Description, string dim3Id, bool showDetails) {

    this.ReportViewer1.Reset();

    //Set report mode for local processing.
    this.ReportViewer1.ProcessingMode = ProcessingMode.Local;

    ISettingsReader settingsReader = SettingsReaderFactory.Instance.CreateSettingsReader();
    string reportPath = settingsReader.GetSetting("ReportViewer", "FinancialReportPath"+( showDetails? "" : "Small" ), true, null);
    this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(reportPath);

    ReportsBL reports = new ReportsBL();

    // Clear out any previous datasources.
    this.ReportViewer1.LocalReport.DataSources.Clear();

    // Load the company dataSource.
    DataTable company = reports.GetCompanyDataSet(accountingCompanyId).Tables[0];
    ReportDataSource dataSourceCompany = new ReportDataSource(company.TableName, company);
    this.ReportViewer1.LocalReport.DataSources.Add(dataSourceCompany);

    if (showDetails)
    {
        // Load the dataSource.
        DataTable report = reports.GetReportFinanceiroDataSet(companyId, approvalUnitId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, documentState, accountingCompanyId).Tables[0];
        ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report);
        this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport);
    }
    else
    {
        // Load the dataSource.
        DataTable report = reports.GetReportFinanceiroSmallDataSet(companyId, approvalUnitId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, documentState, accountingCompanyId).Tables[0];
        ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report);
        this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport);
    }

    this.pnlReport.Visible = true;
}

我从未调用过ReportViewer.DataBind();以下是我通常做的事情:

IEnumerable<ReportClass> ds = DataTranslator.GetReportData(Int64.Parse(<someId>));
report.LocalReport.ReportPath = "<some_path_to_report.rdlc>";
report.LocalReport.DataSources.Add(new ReportDataSource("DataSet", ds));
report.Visible = true;
report.LocalReport.Refresh();
IEnumerable ds=DataTranslator.GetReportData(Int64.Parse());
report.LocalReport.ReportPath=“”;
Add(newreportdatasource(“DataSet”,ds));
report.Visible=true;
report.LocalReport.Refresh();

但您是在pageload方法上执行此操作,还是在onclick方法上执行此操作?您使用什么版本的ReportViewer?我通过回发控件调用该方法。我是专门从DropDownList\u SelectedIndexChanged事件中提取的。我使用的是ReportViewer.WebForms版本10.0.0。