How to pass text from textbox to report, with VS 2015, in C#?

Asked

Viewed 695 times

3

I’m creating an app for where I work, which should print some information-based labels that I enter into a form I created.

For that, I’m creating a smaller version, to test if everything I want works. I am using Visual Studio 2015 Community with files in RDLC format.

I have a form with a textbox with the name "tb_info", and a report with a parameter named "param_info". The goal is, when pressing the form button, it passes the information of the textbox for the parameter of report, giving the order to print the report next.

Some idea to accomplish this?

  • Going to use Report Viewer? Your application is WPF or MVC?

  • Makes it easy to use Reportviewer!?

  • I need to know more things. If it is a web application, for example, I don’t think. If it is WPF it may even compensate depending on.

  • It is not web, I am creating as a normal executable application (it will be MVC I believe).

  • It is something much simpler, are labels with 11cm x 20cm, and contains only between 3 to 4 text fields, everything else is merely decorative (logo of the company and customer, plus the carrier). I don’t know if it helps but the . NET version used in the company is 4.5

  • The link provided to the two lines back is just to create the right Reports?! Because VS2015 lets you create a report in rdlc, which I used to create the label layout!!

  • Well, you want to use Report Viewer and RDLC, then. No problem. I don’t have all the specific knowledge to give you an answer. I will edit your question again.

  • Thanks for the help.

Show 4 more comments

1 answer

4

Following is a template of an action that would return the report in PDF format, this concept addresses the loading of the "RDLC", the bind with the "Datasource" and variable assignment, hopefully useful:

public FileResult GetRelatorio()
{
    ReportViewer report = new ReportViewer();
    report.LocalReport.DisplayName = "Relatório";

    //Carregue o rdlc do seu relatório
    using (FileStream stream = new FileStream("meu_relatorio.rdlc", FileMode.Open))
       report.LocalReport.LoadReportDefinition(stream);

    //Adicione o seu datasource
    report.LocalReport.DataSources.Add(new ReportDataSource
    {
        Name = "Nome no data source",
        Value = "Seu data source"
    });

    //Dê valor aos parâmetros do seu relatório
    ReportParameter[] parameters = new ReportParameter[]{
        new ReportParameter("Texto1", txt1.Text),
        new ReportParameter("Texto2", txt2.Text)
    };
    report.LocalReport.SetParameters(parameters);

    //Gere o seu relatório em PDF
    byte[] Arquivo = report.LocalReport.Render("PDF");
    Stream stream = new MemoryStream(Arquivo);
    return File(stream, "application/pdf");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.