子报告“Subreport4”的数据检索失败,仅发生在1台PC上,但在其他PC上却没有

时间:2014-10-31 01:45:25

标签: c# sql-server

我有一个VS2013(用C#x32编写)桌面应用程序访问托管的SQL Server 2012.它有一个包含4个子报告的报告。将其安装到几台PC上后,其中一台PC出现上述问题,但其他PC正确显示报告。奇怪的是问题PC通过subreport4中途显示此错误。所有PC都在Windows 7(x32 / 64)上运行。我真的很遗憾如何找出它为什么只发生在PC上,这与其他PC基本相似。

报告编码如下: -

    public partial class ReportProject : Form
{

    cl_Class1 mySettings = new cl_Class1();
    SqlConnection conReport = new SqlConnection();

    public ReportProject()
    {
        InitializeComponent();
        this.Text = "Test Report";
        // Add a handler for SubreportProcessing
        reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
    }

    // data set at the class level to access by all methods
    DataSet dsReport = new dsTestReport();

    private void ReportProject_Load(object sender, EventArgs e)
    {
        // connection to database
        conReport.ConnectionString = mySettings.myConnString;
        SqlCommand cmdReport = new SqlCommand();
        SqlDataReader drReport;

        try
        {
            // open connection
            conReport.Open();
            cmdReport.CommandType = CommandType.Text;
            cmdReport.Connection = conReport;

            // get query string from builder
            string strMain = "aaaaa";
            string strSub1 = "bbbbb";
            string strSub2 = "ccccc";
            string strSub3 = "ddddd"; 

            cmdReport.CommandText = strMain + strSub1 + strSub2 + strSub3;

            // execute query and load result to dataset
            drReport = cmdReport.ExecuteReader();
            dsReport.Load(drReport, LoadOption.OverwriteChanges, dsReport.Tables[0], dsReport.Tables[1], dsReport.Tables[2], dsReport.Tables[3]);
            // close connection
            drReport.Close();
            conReport.Close();

            // prepare report for view
            reportViewer1.LocalReport.ReportEmbeddedResource = "myProgram.rptMain.rdlc";

            ReportDataSource rds = new ReportDataSource();
            rds.Name = "DataSet1";

            rds.Value = dsReport.Tables[0];
            reportViewer1.LocalReport.DataSources.Add(rds);

            // preview the report
            reportViewer1.RefreshReport();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        finally
        {
            if (conReport.State == ConnectionState.Open)
            {
                conReport.Close();
            }
        }

    }
    void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
    {  
        string myReportName = e.ReportPath;

        if (myReportName == "rptSubReport1") 
            {
                e.DataSources.Add(new ReportDataSource("DataSet1", dsReport.Tables[1]));
            }
        if (myReportName == "rptSubReport2")
            {
                e.DataSources.Add(new ReportDataSource("DataSet1", dsReport.Tables[2]));
            }
        if (myReportName == "rptSubReport3")
        {
            e.DataSources.Add(new ReportDataSource("DataSet1", dsReport.Tables[3]));
        }
        if (myReportName == "rptSubReport4")
        {
            e.DataSources.Add(new ReportDataSource("DataSet1", dsReport.Tables[3]));
        }

    }
}

注意:SubReport3和SubReport4使用相同的数据集(dsReport.Tables [3])

愿意建议/帮助解决这个问题。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

作为最后的手段,我从这台PC卸载了reportviewer运行时(这是唯一一台带有reportviewer副本的PC)。卸载后,报告现在可以正常运行!虽然这似乎解决了这个问题,但我仍然感到疑惑为什么会出现这种情况 - 也许在reportviewer运行时中有一些东西与编译后的软件有冲突。