• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 933
  • Last Modified:

fill report from dataset c#

Hi all,

I have to make some reports and going to use the Microsoft Reports.
Now I want to fill the reports from a dataset that I create at runtime but how can I first make a report for data I don't have at the moment?
I'll be a mixed dataset with data from severall tables joined in one dataset.

How do I do this?

Basicly, want to run my code, create a dataset, add it to a report, then open the report OR save it as PDF without even opening the report.

Greetings



0
Pit76
Asked:
Pit76
  • 3
  • 2
1 Solution
 
phead_2Commented:
You need to create a dummy datasource(s) in the report to generate the field names. You can then add the dataset while running (and go straight to PDF if you like).

Open your .rdlc file by right clicking it in the solution explorer and pick open with. open with 'XML Editor'. In the top section add your datasets:
  <DataSources>
    <DataSource Name="DummyDataSource1">
      <rd:DataSourceID>c649d533-64c3-42b6-9805-19adbfccd468</rd:DataSourceID>
      <ConnectionProperties>
        <DataProvider>SQL</DataProvider>
        <ConnectString />
      </ConnectionProperties>
    </DataSource>
  </DataSources>

Open in new window

0
 
phead_2Commented:
Didn't finish my post... After adding the datasource you need to create the dataset(s)"


  <DataSets>
    <DataSet Name="DataSet1">
      <Fields>
        <Field Name="StringField1">
          <DataField>StringField1</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
        <Field Name="StringField2">
          <DataField>StringField2</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
      </Fields>
      <Query>
        <DataSourceName>DummyDataSource1</DataSourceName>
        <CommandText />
        <rd:UseGenericDesigner>true</rd:UseGenericDesigner>
      </Query>
      <rd:DataSetInfo>
        <rd:TableName>DataTable1</rd:TableName>
      </rd:DataSetInfo>
    </DataSet>
    <DataSet Name="DataSet2">
      <Fields>
        <Field Name="StringField1">
          <DataField>StringField1</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
        <Field Name="StringField2">
          <DataField>StringField2</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
      </Fields>
      <Query>
        <DataSourceName>DummyDataSource1</DataSourceName>
        <CommandText />
        <rd:UseGenericDesigner>true</rd:UseGenericDesigner>
      </Query>
      <rd:DataSetInfo>
        <rd:TableName>DataTable2</rd:TableName>
      </rd:DataSetInfo>
    </DataSet>


You can then use the fields defined in the datasets in the report.

To populate the datasets make data tables (I named them table1 and table2) that match the datasets (table names, column names and datatypes). Create report datasources, load the report and export to pdf:



                            ReportDataSource DS1 = new ReportDataSource("DataTable1", table1);
                            ReportDataSource DS2 = new ReportDataSource("DataTable2", table2);
                            LocalReport report = new LocalReport();
                            report.ReportPath = path to your .rdlc file...
                            report.DataSources.Add(DS1);
                            report.DataSources.Add(DS2);
 
            string deviceInfo =
              "<DeviceInfo>" +
              "  <OutputFormat>PDF</OutputFormat>" +
              "  <PageWidth>8.5in</PageWidth>" +
              "  <PageHeight>11in</PageHeight>" +
              "  <MarginTop>0.1in</MarginTop>" +
              "  <MarginLeft>0.1in</MarginLeft>" +
              "  <MarginRight>0.1in</MarginRight>" +
              "  <MarginBottom>0.1in</MarginBottom>" +
              "</DeviceInfo>";
 
            Stream CreateStream = new FileStream(pathtofileyouwanttocreate, FileMode.Create);
 
 
            report.Render("PDF", deviceInfo, CreateStream,
               out warnings);

Open in new window

0
 
Pit76Author Commented:
thx for that info!

Now I changed my mind and will generate teh report from an object ( a class). I have at that time a class with all the needed data, so it would be dumb of me to send a query to the db for data I allready have.

Below a codesnippet. How can I add my class as the datasource for the report? It is done in the report designer when I drag al the fields to my report, but when I fill my class at runtime the report doesn't take the new values I provide...

Thx for any help..


 ReportDocumentBLL _localReport = new ReportDocumentBLL();
      try
      { 
        // testdata
        _localReport.Type = "test";
        _localReport.StructureleMededeling="+++105/2009/11289+++";
        _localReport.Nummer = 1052009;
    
          // fill report with data from _localReport
          // ???

Open in new window

0
 
Pit76Author Commented:
K, found it!

Had to bind the datasource to the class:

ReportDocumentBLLBindingSource.DataSource = _localReport;
reportViewer1.RefreshReport();

Thx for the help with the datasets, will come in handy later... :)
0
 
Pit76Author Commented:
thx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now