2012-01-10 11 views
8

मैंने कस्टम नियंत्रण (एक रिपोर्ट व्यूअर वाला एक विंडोज फॉर्म) बनाया है।स्थानीय रिपोर्ट के लिए डेटास्रोत सेट करना - .NET और रिपोर्ट व्यूअर

CustomReportViewer कक्षा में शामिल होते हैं

//Load local report 
this.reportViewer1.ProcessingMode = ProcessingMode.Local;   
//enable loading of external images   
this.reportViewer1.LocalReport.EnableExternalImages = true; 
//pass the report to the viewer 
using (FileStream stream = new FileStream(filename, FileMode.Open)) 
{ 
    this.reportViewer1.LocalReport.LoadReportDefinition(stream); 
} 

मैं इस का उपयोग कर फोन:

CustomReportViewer reportViewer = new CustomReportViewer(); 

यह ठीक काम करता है और एक खिड़कियों प्रपत्र युक्त प्रतीत होता है कि मैं एक स्थानीय रिपोर्ट लोड करने के लिए निम्न कोड है रिपोर्ट दर्शक नियंत्रण लेकिन मुझे निम्न संदेश मिलता है:

A data source instance has not been supplied for the data source "ReportData" 

मुझे पूरी तरह से यकीन नहीं है कि डेटा स्रोत कैसे सेट अप करें? मुझे आवश्यक डेटा दूरस्थ डेटाबेस में संग्रहीत किया गया है ... मुझे इस कनेक्शन को सेट करने के लिए क्या करना है?

उत्तर

16

आपको ReportDataSource बनाने की आवश्यकता है, और इसकी Value संपत्ति सेट करें - उदा। DataTable और IEnumerable रों supported sources

हैं उदाहरण के लिए, और यह सोचते हैं कि एक विधि एक DataSet वापस जाने के लिए, एक भी DataTable कॉलम अपनी रिपोर्ट के लिए आवश्यक मिलान के साथ मौजूद है:

DataSet ds = SomeMethodToRetrieveDataSet(); // e.g. via DataAdapter 
// If your report needs parameters, they need to be set ... 
ReportParameter[] parameters = new ReportParameter[...]; 

ReportDataSource reportDataSource = new ReportDataSource(); 
// Must match the DataSource in the RDLC 
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = ds.Tables[0]; 

// Add any parameters to the collection 
reportViewer1.LocalReport.SetParameters(parameters); 
reportViewer1.LocalReport.DataSources.Add(reportDataSource); 
reportViewer1.DataBind(); 

ध्यान दें कि यह अक्सर आसान होता है आरडीएलसी को अलग-अलग आरडीएलसी फाइलों को बनाए रखने के बजाय, अपनी असेंबली में बस एम्बेड करने के लिए। Embedded Resource रूप RDLC पर Build Action चुनकर ऐसा करें, और फिर आप ReportEmbeddedResource गुण सेट कर सकते हैं:

reportViewer1.LocalReport.ReportEmbeddedResource = 
         "MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc"; 

ध्यान दें कि संसाधन स्ट्रिंग (विधानसभा सहित) संसाधन का पूरी तरह से योग्य नाम शामिल होना चाहिए।

+0

यह भी ध्यान दें कि आपके संसाधन फ़ोल्डर में हैं, तो फ़ोल्डर का नाम पूरी तरह से योग्य नाम में भी मिलता है। – StuartLC

7

मेरे लिए कुंजी जैसा कि स्टुअर्टएलसी द्वारा उपर्युक्त उत्तर दिया गया था ... उसमें और स्पष्टीकरण के साथ जब उन्होंने कहा कि "आरडीएलसी में डेटासोर्स से मेल खाना चाहिए" .. यह वास्तव में "डेटासेट नाम" तत्व मान बन गया पुन: <DataSetName>DataSet1</DataSetName>

मैं गोल और गोल गया क्योंकि इसे "डेटासोर्स" कहा जाता है, इसलिए मैंने डेटासोर्स तत्व नाम का उपयोग जारी रखा लेकिन स्पष्ट रूप से rdl और rdlc फ़ाइल में यह वास्तव में डेटासेट नाम को दर्शाता है। तो यह ध्यान में रखते हुए कोड है जो स्टुअर्ट से ऊपर से उधार लिया गया है। डेटासेट नाम तत्व मान पर ध्यान दें:

 using (SqlConnection sqlConn = new SqlConnection(rvConnection)) 
     using (SqlDataAdapter da = new SqlDataAdapter(rvSQL, rvConnection)) 
     { 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      DataTable dt = ds.Tables[0]; 

      this.reportViewer1.Reset(); 
      this.reportViewer1.ProcessingMode = ProcessingMode.Local; 
      this.reportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "ssrsExport.rdlc"; 
      ReportDataSource reportDataSource = new ReportDataSource(); 
      // Must match the DataSet in the RDLC 
      reportDataSource.Name = "DataSet1"; 
      reportDataSource.Value = ds.Tables[0]; 
      this.reportViewer1.LocalReport.DataSources.Add(reportDataSource); 
      this.reportViewer1.RefreshReport(); 
     } 
संबंधित मुद्दे