How to add print report functionality using report viewer?

MiKhel Navarra 20 Reputation points
2024-01-10T01:30:31.6966667+00:00

How to create print report functionality (Report Viewer) in ASP.NET Core MVC 7.0 and also i'm using razor pages in visual studio 2022. When i click the print button it will create the summary of reports of the index of inventory web application and the approach to print the report is through PDF in web and able to print.

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-01-11T10:05:11.9566667+00:00

    Hi @MiKhel Navarra,

    Currently asp.net core does not natively support RDLC reports.

    However, this feature can be implemented in asp.net Core through third-party packages, please note that third-party packages are not officially supported by Microsoft.

    Here is the test steps, hope it can help you. (Linux is not support)

    1.You can check the steps in this answer first.

    2.And I also try to add a DataTable in the report. Here is the sample code

            [HttpGet("print2")]
            public IActionResult Print2()
            {
                string mimetype = "";
                int extension = 1;
                var path = $"{this._webHostEnvironment.WebRootPath}\\Reports\\Report2.rdlc";
    
                Dictionary<String, string> parmeter = new Dictionary<String, string>();
                parmeter.Add("ReportParameter1", "RDLC in Blazor Web Application.");
    
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("Id", typeof(int));
                dataTable.Columns.Add("StartAirport", typeof(string));
                dataTable.Columns.Add("EndAirport", typeof(string));
                dataTable.Columns.Add("DepartureDate", typeof(DateTime));
                dataTable.Columns.Add("ArrivalDate", typeof(DateTime));
                dataTable.Columns.Add("ClassSeat", typeof(string));
    
    
                DataRow row1 = dataTable.NewRow();
                row1["Id"] = 1;
                row1["StartAirport"] = "JFK International Airport";
                row1["EndAirport"] = "LAX International Airport";
                row1["DepartureDate"] = new DateTime(2022, 6, 1);
                row1["ArrivalDate"] = new DateTime(2022, 6, 2);
                row1["ClassSeat"] = "Economy Class";
                dataTable.Rows.Add(row1);
    
                DataRow row2 = dataTable.NewRow();
                row2["Id"] = 2;
                row2["StartAirport"] = "LAX International Airport";
                row2["EndAirport"] = "JFK International Airport";
                row2["DepartureDate"] = new DateTime(2022, 6, 3);
                row2["ArrivalDate"] = new DateTime(2022, 6, 4);
                row2["ClassSeat"] = "Business Class";
                dataTable.Rows.Add(row2);
    
                DataRow row3 = dataTable.NewRow();
                row3["Id"] = 3;
                row3["StartAirport"] = "JFK International Airport";
                row3["EndAirport"] = "SFO International Airport";
                row3["DepartureDate"] = new DateTime(2022, 6, 5);
                row3["ArrivalDate"] = new DateTime(2022, 6, 6);
                row3["ClassSeat"] = "First Class";
                dataTable.Rows.Add(row3);
    
              
    
                LocalReport localreport = new LocalReport(path);
                localreport.AddDataSource("dstickets", dataTable);
                var restul = localreport.Execute(RenderType.Pdf, extension, parmeter, mimetype);
                return File(restul.MainStream, "application/pdf");
            }
    

    3.Note that the data source options in the data table in the .rdlc file must not be added manually. Otherwise, an exception will occur when the data is rendered.
    User's image

    1. Test Result, and you can download it or print it.

    User's image

    1. My RDLC file
    <?xml version="1.0" encoding="utf-8"?>
    <Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
      <AutoRefresh>0</AutoRefresh>
      <DataSources>
        <DataSource Name="AviaTicketsDataSet">
          <ConnectionProperties>
            <DataProvider>System.Data.DataSet</DataProvider>
            <ConnectString>/* Local Connection */</ConnectString>
          </ConnectionProperties>
          <rd:DataSourceID>83eaa81a-c134-452b-b7a5-67e35bca97ef</rd:DataSourceID>
        </DataSource>
      </DataSources>
      <DataSets>
        <DataSet Name="dstickets">
          <Query>
            <DataSourceName>AviaTicketsDataSet</DataSourceName>
            <CommandText>/* Local Query */</CommandText>
          </Query>
          <Fields>
            <Field Name="Id">
              <DataField>Id</DataField>
              <rd:TypeName>System.Int32</rd:TypeName>
            </Field>
            <Field Name="StartAirport">
              <DataField>StartAirport</DataField>
              <rd:TypeName>System.String</rd:TypeName>
            </Field>
            <Field Name="EndAirport">
              <DataField>EndAirport</DataField>
              <rd:TypeName>System.String</rd:TypeName>
            </Field>
            <Field Name="DepartureDate">
              <DataField>DepartureDate</DataField>
              <rd:TypeName>System.DateTime</rd:TypeName>
            </Field>
            <Field Name="ArrivalDate">
              <DataField>ArrivalDate</DataField>
              <rd:TypeName>System.DateTime</rd:TypeName>
            </Field>
            <Field Name="ClassSeat">
              <DataField>ClassSeat</DataField>
              <rd:TypeName>System.String</rd:TypeName>
            </Field>
          </Fields>
          <rd:DataSetInfo>
            <rd:DataSetName>AviaTicketsDataSet</rd:DataSetName>
            <rd:SchemaPath>F:\repos\RDLC\RDLC_Template\AviaTicketsDataSet.xsd</rd:SchemaPath>
            <rd:TableName>tickets</rd:TableName>
            <rd:TableAdapterFillMethod>Fill</rd:TableAdapterFillMethod>
            <rd:TableAdapterGetDataMethod>GetData</rd:TableAdapterGetDataMethod>
            <rd:TableAdapterName>ticketsTableAdapter</rd:TableAdapterName>
          </rd:DataSetInfo>
        </DataSet>
      </DataSets>
      <ReportSections>
        <ReportSection>
          <Body>
            <ReportItems>
              <Tablix Name="Tablix2">
                <TablixBody>
                  <TablixColumns>
                    <TablixColumn>
                      <Width>0.43354in</Width>
                    </TablixColumn>
                    <TablixColumn>
                      <Width>0.90625in</Width>
                    </TablixColumn>
                    <TablixColumn>
                      <Width>0.97569in</Width>
                    </TablixColumn>
                    <TablixColumn>
                      <Width>1.05902in</Width>
                    </TablixColumn>
                    <TablixColumn>
                      <Width>0.95486in</Width>
                    </TablixColumn>
                    <TablixColumn>
                      <Width>0.90625in</Width>
                    </TablixColumn>
                  </TablixColumns>
                  <TablixRows>
                    <TablixRow>
                      <Height>0.25in</Height>
                      <TablixCells>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="Textbox8">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>Id</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>Textbox8</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="Textbox10">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>StartAirport</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>Textbox10</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="Textbox2">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>EndAirport</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>Textbox2</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="Textbox4">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>DepartureDate</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>Textbox4</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="Textbox6">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>ArrivalDate</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>Textbox6</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="Textbox12">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>ClassSeat</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>Textbox12</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                      </TablixCells>
                    </TablixRow>
                    <TablixRow>
                      <Height>0.25in</Height>
                      <TablixCells>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="Id">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>=Fields!Id.Value</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>Id</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="StartAirport">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>=Fields!StartAirport.Value</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>StartAirport</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="EndAirport">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>=Fields!EndAirport.Value</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>EndAirport</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="DepartureDate">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>=Fields!DepartureDate.Value</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>DepartureDate</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="ArrivalDate">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>=Fields!ArrivalDate.Value</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>ArrivalDate</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                          </CellContents>
                        </TablixCell>
                        <TablixCell>
                          <CellContents>
                            <Textbox Name="ClassSeat">
                              <CanGrow>true</CanGrow>
                              <KeepTogether>true</KeepTogether>
                              <Paragraphs>
                                <Paragraph>
                                  <TextRuns>
                                    <TextRun>
                                      <Value>=Fields!ClassSeat.Value</Value>
                                      <Style />
                                    </TextRun>
                                  </TextRuns>
                                  <Style />
                                </Paragraph>
                              </Paragraphs>
                              <rd:DefaultName>ClassSeat</rd:DefaultName>
                              <Style>
                                <Border>
                                  <Color>LightGrey</Color>
                                  <Style>Solid</Style>
                                </Border>
                                <PaddingLeft>2pt</PaddingLeft>
                                <PaddingRight>2pt</PaddingRight>
                                <PaddingTop>2pt</PaddingTop>
                                <PaddingBottom>2pt</PaddingBottom>
                              </Style>
                            </Textbox>
                            <rd:Selected>true</rd:Selected>
                          </CellContents>
                        </TablixCell>
                      </TablixCells>
                    </TablixRow>
                  </TablixRows>
                </TablixBody>
                <TablixColumnHierarchy>
                  <TablixMembers>
                    <TablixMember />
                    <TablixMember />
                    <TablixMember />
                    <TablixMember />
                    <TablixMember />
                    <TablixMember />
                  </TablixMembers>
                </TablixColumnHierarchy>
                <TablixRowHierarchy>
                  <TablixMembers>
                    <TablixMember>
                      <KeepWithGroup>After</KeepWithGroup>
                    </TablixMember>
                    <TablixMember>
                      <Group Name="Details" />
                    </TablixMember>
                  </TablixMembers>
                </TablixRowHierarchy>
                <DataSetName>dstickets</DataSetName>
                <Top>1.3425in</Top>
                <Left>1.18625in</Left>
                <Height>0.5in</Height>
                <Width>5.23563in</Width>
                <Style>
                  <Border>
                    <Style>None</Style>
                  </Border>
                </Style>
              </Tablix>
              <Textbox Name="Textbox1">
                <CanGrow>true</CanGrow>
                <KeepTogether>true</KeepTogether>
                <Paragraphs>
                  <Paragraph>
                    <TextRuns>
                      <TextRun>
                        <Value>=Parameters!ReportParameter1.Value</Value>
                        <Style />
                      </TextRun>
                    </TextRuns>
                    <Style />
                  </Paragraph>
                </Paragraphs>
                <rd:DefaultName>Textbox1</rd:DefaultName>
                <Top>0.875in</Top>
                <Left>1.77604in</Left>
                <Height>0.25in</Height>
                <Width>2.94792in</Width>
                <ZIndex>1</ZIndex>
                <Style>
                  <Border>
                    <Style>None</Style>
                  </Border>
                  <PaddingLeft>2pt</PaddingLeft>
                  <PaddingRight>2pt</PaddingRight>
                  <PaddingTop>2pt</PaddingTop>
                  <PaddingBottom>2pt</PaddingBottom>
                </Style>
              </Textbox>
            </ReportItems>
            <Height>5.08333in</Height>
            <Style />
          </Body>
          <Width>10.5821in</Width>
          <Page>
            <LeftMargin>1in</LeftMargin>
            <RightMargin>1in</RightMargin>
            <TopMargin>1in</TopMargin>
            <BottomMargin>1in</BottomMargin>
            <Style />
          </Page>
        </ReportSection>
      </ReportSections>
      <ReportParameters>
        <ReportParameter Name="ReportParameter1">
          <DataType>String</DataType>
          <Prompt>ReportParameter1</Prompt>
        </ReportParameter>
      </ReportParameters>
      <ReportParametersLayout>
        <GridLayoutDefinition>
          <NumberOfColumns>4</NumberOfColumns>
          <NumberOfRows>2</NumberOfRows>
          <CellDefinitions>
            <CellDefinition>
              <ColumnIndex>0</ColumnIndex>
              <RowIndex>0</RowIndex>
              <ParameterName>ReportParameter1</ParameterName>
            </CellDefinition>
          </CellDefinitions>
        </GridLayoutDefinition>
      </ReportParametersLayout>
      <rd:ReportUnitType>Inch</rd:ReportUnitType>
      <rd:ReportID>57f42c9e-2157-4b25-b021-79305d102b51</rd:ReportID>
    </Report>
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. Best regards,
    Jason

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.