Question

Is there any way to add programmatically a datasource / dataset to a Microsoft.Reporting.WebForms.LocalReport when the report-XmlFile (*.rdlc) has no datasource / dataset definitions at design-time?

This works if I already have a datasource / dataset definition in my *.rdlc

C#

public byte[] RenderReport(string reportName, string reportFormat)
{
    LocalReport report = LoadReport(reportName);

    //Has same name like DataSet in *.rdlc
    ReportDataSource rds = new ReportDataSource("DataSet1", getData());

    report.DataSources.Clear();
    report.DataSources.Add(rds);

    return report.Render(reportName);
}

private DataTable getData()
{
    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("ID",typeof(System.String)));
    dt.Columns.Add(new DataColumn("NAME", typeof(System.String)));

    dt.Rows.Add(new string[] { "1", "Me" });
    return dt;
}

*.rdlc

  <DataSources>
    <DataSource Name="DataSource1">
      <ConnectionProperties>
        <DataProvider>System.Data.DataSet</DataProvider>
        <ConnectString>/* Local Connection */</ConnectString>
      </ConnectionProperties>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="DataSet1">
      <Query>
        <DataSourceName>DataSource1</DataSourceName>
        <CommandText>/* Local Query */</CommandText>
      </Query>
      <Fields>
        <Field Name="ID">
          <DataField>ID</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
        <Field Name="NAME">
          <DataField>NAME</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
      </Fields>
    </DataSet>
  </DataSets>

But if I remove the datasource / dataset definition I get

{Microsoft.Reporting.DefinitionInvalidException: The definition of the report '' is invalid. ---> Microsoft.ReportingServices.ReportProcessing.ReportPublishingException: The Value expression for the text box ‘Textbox1’ refers to the field ‘ID’. Report item expressions can only refer to fields within the current dataset scope or, if inside an aggregate, the specified dataset scope. Letters in the names of fields must use the correct case.}

Do I always have to create something like a "Dummy"-DataSource/DataSet or do I miss something in my code? I hope there is another solution as manipulating the XML before rendering-process, any ideas?

Thanks!

Was it helpful?

Solution

You can't leave RDLC witout DataSets, if you are using it and RDLC is embedded in your project. Either you leave DataSet fixed and change only it's items either try to load report definition from XML

// Valid XML with dynamic DataSources and DataSets
string s = @"<?xml version=""1.0"" encoding=""utf-8""?><Report ...>...</Report>";
report.LoadReportDefinition(new MemoryStream(Encoding.UTF8.GetBytes(s)));
return report.Render(reportName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top