logo
small logo
  • Products
  • Comprar
  • Suporte
  • About
  • Customer panel Suporte
    • en
    • de
    • JP
    • ZH
  • Home
  • /
  • Articles
  • /
  • How to use stored procedures with multiple sets of data as a result
  • How to create business cards from a WinForms application

    28 de maio de 2020

    As you can see from the name, Windows Forms type of applications is designed to

    read more
  • How to create a sales receipt from a WinForms application

    30 de maio de 2020

    Most likely every .NET developer started from Windows Forms applications. Countless number of such applications

    read more
  • MS Word format vs. Open Document Text. Which is better?

    19 de outubro de 2020

    Speaking of text editors, we immediately present coryphaeus as Microsoft Word, which for many years

    read more
  • Want to generate tables for a user - Excel or OpenOffice Calc?

    8 de outubro de 2020

    Tables. For centuries, they have been used to present similar data for record-keeping, counting amounts,

    read more
  • Como proteger seu PDF?

    30 de novembro de 2020

    No dia da proteção de dados, decidimos preparar um artigo sobre a proteção de documentos

    read more

How to use stored procedures with multiple sets of data as a result

8 de maio de 2020

Often, when creating reports, we have to deal with databases that are far from ideal. The various "features", sometimes get us stuck. For example, how to build a report based on multiple datasets, which will result in MS SQL stored procedures? When you create a data source in the report, we can use a stored procedure in SQL query, but only for a specific table. This means that it will take only a first set of data returned by the stored procedure. To generate multiple tables of the results of a stored procedure, we need to create a data source software in the code of a custom application.

You need to create a database connection, execute the stored procedure, and enter its results into the dataset. Then we transfer dataset to the report and record the data source in the report. In this way, in the designer we will receive the prepared data source when making the report. We can go further and programmatically connect to the report source, so it's already in the data tree. But the firstly, we consider the first option with simple adding a data source to the report.

Let's say you have some WinForms applications where you generate reports. Add a button to call the report editor:

1
2
3
4
5
6
7
8
private void button1_Click(object sender, EventArgs e)
 {
 Report FReport = new Report();
 SqlConnection conn = new SqlConnection("Data Source=localhost;AttachDbFilename=;Initial Catalog=salesdb;Integrated Security=True;Persist Security Info=False;User ID=;Password=");
 DataSet data = GetDataSet(conn, "Test");
 FReport.RegisterData(data, "NorthWind");
 FReport.Design();
 }

 Here, we created a report object, announced the MS SQL database connection line, created a report data source, and registered it in the report. Get the GetDataSet data source as follows:

1
2
3
4
5
6
7
8
9
10
11
public DataSet GetDataSet(SqlConnection connection, string storedProcName, params SqlParameter[] params)
 {
 var command = new SqlCommand(storedProcName, connection) { CommandType = CommandType.StoredProcedure };
 command.Parameters.AddRange(params);
 
 var result = new DataSet();
 var dataAdapter = new SqlDataAdapter(command);
 dataAdapter.Fill(result);
 
 return result;
 }

 This method accepts a connection string, the name of the stored procedure and the possible parameters for the stored procedure. In our case, we did not pass anything. After you create a connection to the database stored procedure is executed. The results of her work are placed in dataAdapter.

On this stage we could have finished the article. The above code is enough to create and register a data source in a report, which will be available to you in the designer, when you edit created at the beginning of the report object.

When the Report Designer is open, you will see a blank report template. Sources of data will need to choose the settings:

 

However, we can improve the code and avoid further design actions in selecting data sources. That is, the report will create an additional data. To do this, we will complete the push button handler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void button1_Click(object sender, EventArgs e)
 {
 Report FReport = new Report();
 Config.DesignerSettings.DesignerLoaded += DesignerSettings_DesignerLoaded;
 SqlConnection conn = new SqlConnection("Data Source=localhost;AttachDbFilename=;Initial Catalog=salesdb;Integrated Security=True;Persist Security Info=False;User ID=;Password=");
 DataSet data = GetDataSet(conn, "Test");
 
 FReport.RegisterData(data, "NorthWind");
 // activate all data sources by default
 foreach (DataSourceBase source in FReport.Dictionary.DataSources)
 {
 source.Enabled = true;
 }
 FReport.Design();
 }

 As you've noticed, we signed the handler to the designer's download event. Next, we include all the data sources in the report in the loop. We implement the handler for the designer's download event:

1
2
3
4
 private void DesignerSettings_DesignerLoaded(object sender, EventArgs e)
 {
 (sender as Designer).cmdNew.CustomAction += new EventHandler(cmdNew_CustomAction);
 }

 We are forwarding the pointer to an event report designer. the event handler:

1
2
3
4
5
6
7
8
9
10
 void cmdNew_CustomAction(object sender, EventArgs e)
 {
 Designer designer = sender as Designer;
 
 //StandardReportWizard wizard = new StandardReportWizard(); // you can use any wizard form package
 BlankReportWizard wizard = new BlankReportWizard();
 wizard.Run(designer);
 
 designer.SetModified(this, "EditData");
 }

 Create a wizard of creating an empty report.

As a result, when editing an empty report, we get the added data:

 

about product comprar
avatar
Dmitriy Fedyashov
Technical Writer
Fast Reports Team: Dmitriy Fedyashov - Technical Writer at Fast Reports
.NET FastReport Data Source WinForms

Add comment
logo
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314
  • Comprar
  • Download
  • Documentação
  • Depoimentos
  • Como desinstalar nossos produtos
  • FAQ
  • Tutorial Video
  • Forum
  • Support SLA
  • Articles
  • Our News
  • Informação sobre nós
  • Parceiros
  • Extended licensing
  • Contatos

© 1998-2023 by Fast Reports Inc.

  • Privacidade

Trustpilot