2013-10-18 73 views
1

下面的代碼拋出拋出異常而debugging-異常而呈現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說:「本地報表處理過程中出現的錯誤」。如何遇到這個?

+0

您是否能夠在報表設計器中運行報表?此外,異常是否有內部異常?如果是這樣,它說什麼? –

+1

內部異常說:報告處理期間發生錯誤。我在VS2008中創建了報告;一個Report.rdlc文件 - 它是同一個控制檯應用程序的一部分。如果我嘗試使用「Hello World」運行,沒有數據pdf生成 – flute

+0

內部異常本身是否有內部異常?如果是這樣,它說什麼? (在某些時候,它實際上應該說明錯誤是什麼)。另外,當您在VS 2008中創建報告文件時,是否將字段拖到項目數據源的報告中?這個數據源叫什麼?例如,我剛剛創建了一個名爲'AnnsDBDataSet_Customers'的表,並且是從'AnnsDB'創建的數據集的Customers表。 –

回答

0

在這裏,問題就出在聲明

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

這裏所說的「數據集」的名稱應該是相同的數據集名稱usedin .rdlc report.Click上的報表元素proprty,看看哪些數據集,它使用。並提供與查詢數據集'dataset.Tables [0]'完全相同的數據。有用。