1

以下代码在调试时引发异常 -

 class Program
    {
        static void Main(string[] args)
        {
            //Customise parameters for render method
            Warning[] warnings;
            string[] streamIds;
            string mimeType = "application/pdf";
            string encoding = String.Empty;
            string filenameExtension = String.Empty;
            string deviceInfo = "<DeviceInfo>" + "<OutputFormat>PDF</OutputFormat>" + "<PageWidth>8.5in</PageWidth>" + "<PageHeight>11in</PageHeight>" + "<MarginTop>0.5in</MarginTop>" + "<MarginLeft>1in</MarginLeft>" + "<MarginRight>1in</MarginRight>" + "<MarginBottom>0.5in</MarginBottom>" + "</DeviceInfo>";

            //Create a SqlConnection to the AdventureWorks2008R2 database. 
            SqlConnection connection = new SqlConnection("data source=localhost;initial catalog=AdventureWorks2008R2;integrated security=True");

            //Create a SqlDataAdapter for the Sales.Customer table.
            SqlDataAdapter adapter = new SqlDataAdapter();

            // A table mapping names the DataTable.
            adapter.TableMappings.Add("Table", "Sales.Customer");

            // Open the connection.
            connection.Open();
            Console.WriteLine("\nThe SqlConnection is open.");

            // Create a SqlCommand to retrieve Suppliers data.
            SqlCommand command = new SqlCommand("SELECT Sales.Customer.CustomerID,Sales.Customer.PersonID,Sales.Customer.StoreID,Sales.Customer.TerritoryID,Sales.Customer.AccountNumber,Sales.Customer.rowguid,Sales.Customer.ModifiedDate FROM Sales.Customer", connection);
            command.CommandType = CommandType.Text;

            // Set the SqlDataAdapter's SelectCommand.
            adapter.SelectCommand = command;
            command.ExecuteNonQuery();

            // Fill the DataSet.
            DataSet dataset = new DataSet("Sales.Customer");

            adapter.Fill(dataset);

            //set up Reportviewver
            Microsoft.Reporting.WinForms.LocalReport viewer = new Microsoft.Reporting.WinForms.LocalReport();
            viewer.ReportPath = @"C:\Documents and Settings\murali.madhava\My Documents\Visual Studio 2008\Projects\PdfReportGeneration\PdfReportGeneration\Report.rdlc";


           //add data source.
           viewer.DataSources.Clear();
           viewer.DataSources.Add(new ReportDataSource("dataset", dataset.Tables[0]));


           //Now render it to pdf
           try
           {
            byte[] bytes = viewer.Render("PDF", deviceInfo, out mimeType, out encoding, out filenameExtension, out streamIds, out warnings);
            using (System.IO.FileStream fs = new System.IO.FileStream("output.pdf", System.IO.FileMode.Create))
            {
                //file saved to bin directory
                    fs.Write(bytes, 0, bytes.Length);
                }

                //Save report to D:\
                // FileStream fsi = new FileStream(@"D:\output.pdf", FileMode.Create);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nCHEY!!!this Exception encountered:", e);
            }


                        // Close the connection.
                        connection.Close();
                        Console.WriteLine("\nThe SqlConnection is closed.");
                        Console.ReadLine();

        }
    }

使用的 Winform 版本是 9.0.0.0,我使用 Adventureworks2008R2.Exception 说:“本地报告处理期间发生错误”。遇到这种情况怎么办?

4

1 回答 1

1

这里的问题在于声明

viewer.DataSources.Add(new ReportDataSource("dataset", dataset.Tables[0]));

此处提到的“数据集”名称应与 .rdlc 报表中使用的数据集名称相同。单击报表元素属性以查看它使用的数据集。并提供与查询数据集“dataset.Tables[0]”完全相同的数据。有用。

于 2013-11-22T06:39:37.160 回答