渲染rdlc文件时抛出异常

时间:2013-10-18 12:23:50

标签: c# exception dataset datasource rdlc

以下代码在调试时抛出异常 -

 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说:“本地报告处理期间发生错误”。怎么遇到这个?

1 个答案:

答案 0 :(得分:0)

这里的问题在于声明

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

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