The Visual Basic .NET programming language is seen by many as an entry-level language. But this, of course, is not the case. We’ve got used to knowing that Visual Basic.NET is most often used in Desktop applications. However, this language allows much more - for example, to create web applications on the ASP .NET framework.
ASP .NET supports the MVC (Model-View-Controller) programming pattern, which greatly simplifies application scaling and testing compared to regular ASP .NET. This template has various variations, but the main idea remains the same - to delimit the areas of responsibility between business logic and presentation. All modern web frameworks are built on this structure. Thus, using Visual Basic .NET, you can easily create frontend web applications in Angular or any other framework.
But let’s get back to the topic of this article - report generation. Report generator programs have strongly entered into our lives at the beginning of the “2000s”, and now only a few dare to create a report from “scratch”. This should be a truly unique report that is beyond the power of any generator. It sounds ironic, but such cases do happen. The specific logic of generating a report can really become an obstacle before using report generators. However, perhaps you just do not know all their possibilities. For example, few people know and use the ability to create a report template from the user application code in the FastReport .NET report generator. This way of creating a report gives you a unique opportunity to control the template structure and the generation process directly in the program code. It is where you can implement your "smart" logic.
Yes, this way of report generation requires good knowledge of the product, namely the structure of the report, objects, and their properties. Therefore, the developer should have adequate qualifications.
Let’s stop these arguments and start creating a demo program that generates a report from the code of an ASP .NET MVC web application in the VisualBasic language.
First of all, you need to create an appropriate project for this.
Visual Studio will carefully create a ready-made demo application with a ready-made structure for you. All you have to do is to connect the FastRport .NET libraries and add your code for creating a report.
Let's add the libraries to the references (References) of the project: FastReport.dll, FastReport.Web.dll. You will find these libraries at the root of the installed FastReport .NET program.
Let's use a ready-made controller for programming the main logic — HomeController:
From the comments to the code, it is clear that we are creating report objects, as well as building a clear hierarchy and dependency. For example, all bands must be added to the page, and text fields are placed on bands or inside other objects such as tables or matrices, for example. It is important not only to build a dependency, but also to correctly set the properties of objects - their size, position relative to the parent object, and so on. All these details form a report. Therefore, this method requires a good level of knowledge of the FastReport.NET product.
Each web method in our demo application already has a view. In the Views folder, find Index.vbhtml. Paste the following line of code in a convenient place:
Here we are executing the method of converting the web report to HTML. Of course, apart from html, it will contain styles, scripts, pictures - in general, everything that is needed to display the report on a web page.
In the Views folder there is a configuration file for the Web.config frontend application. Let's add new namespaces to it:
In the same folder, there is another file that needs to be edited - Views/Home/Shared/_Layout.vbhtml. Let's add to it the use of scripts and styles of the FastReport libraries:
There is another Web.config at the root of the project. Add in it after the </system.web> section:
Our application is ready to run. Let's make sure it works.
That's all: we have created a full-fledged report right in the program code. It looks like a report created by a special visual designer. But, of course, this is more difficult to do in code. Therefore, we recommend using this method only in special cases, when it is impossible to implement complex logic inside the report.