Tutorial: Adding code to the console application, SelfServicing
Preface
In the
previous tutorial, we set up VS.NET 2005 to have our generated sourcecode in one solution, have the right references and we added a console application project, NorthwindConsole. That last project is our target for this tutorial: we'll add some code to that project so you can see the generated code in action! This tutorial is SelfServicing specific. If you've generated Adapter code instead, please use the
Adapter variant of this tutorial.
The main goal of this tutorial is to show you how to get a complete solution setup and where to add your own code to utilize the LLBLGen Pro generated code so you can for example connect to the database, read some entities, manipulate them and save their changes. The LLBLGen Pro manual contains a lot of small different code snippets which illustrate the many different features of LLBLGen Pro's runtime library and generated code. With this tutorial you'll learn a starting point where to test these snippets out.
Using the generated code
Setting up the using / Imports statements
Before we will continue, we'll setup the using / Imports statements at the top of the Program.cs/ Module1.vb file.
- If you haven't loaded the solution created in the previous tutorial, do so by starting VS.NET 2005 and loading the solution
- If you're using C#: Open the Program.cs file of the NorthwindConsole project. If you're using VB.NET, open Module1.vb. You might want to rename that one, but for now, it's ok.
- At the top of the file, you should add the following code: (assuming you used the Northwind.Tutorial root namespace).
// C#
using Northwind.Tutorial.CollectionClasses;
using Northwind.Tutorial.DaoClasses;
using Northwind.Tutorial.EntityClasses;
using Northwind.Tutorial.FactoryClasses;
using Northwind.Tutorial.HelperClasses;
using Northwind.Tutorial.RelationClasses;
using Northwind.Tutorial.TypedViewClasses;
using Northwind.Tutorial.StoredProcedureCallerClasses;
using Northwind.Tutorial;
using SD.LLBLGen.Pro.ORMSupportClasses;
' VB.NET
Imports Northwind.Tutorial.CollectionClasses
Imports Northwind.Tutorial.DaoClasses
Imports Northwind.Tutorial.EntityClasses
Imports Northwind.Tutorial.FactoryClasses
Imports Northwind.Tutorial.HelperClasses
Imports Northwind.Tutorial.RelationClasses
Imports Northwind.Tutorial.TypedViewClasses
Imports Northwind.Tutorial.StoredProcedureCallerClasses
Imports Northwind.Tutorial
Imports SD.LLBLGen.Pro.ORMSupportClasses
We didn't add a using/Imports for the TypedListClasses as we don't have defined a Typed List so the namespace isn't available.
- You're now setup for adding some code.
Using Entities
In the
first tutorial you've mapped a couple of different elements to database elements: entities, a typed view and a retrieval stored procedure. In this part of the tutorial we'll utilize the entities. First we'll fetch a subset of the entities and display them in the console. After that we'll manipulate them and save the changes.
Using Typed Views
We'll leave the land of Entities for now and will instead look at how to use the Typed View we've mapped, Invoices. This Typed View is mapped onto the database view 'Invoices' and initially it will return over 2000 rows. We'll create a Windows Forms form and bind the 2nd page of 25 rows of the Typed View Invoices to a grid on the form.
- To avoid having to run the code for entities over and over again, comment out the code you've added to the Main method first, or you can simply remove that code as it's not needed for this part of the tutorial.
- We'll add a generic Windows Forms form which will have a DataGridView on it. To do so, in Solution Explorer Right-click the NorthwindConsole project -> Add -> Windows form. Specify as name Viewer.cs (or if you're using VB.NET: Viewer.vb)
- From the VS.NET Toolbox (if it's not visible, press Cntrl-W-X), drag a DataGridView onto the form. We won't use any datasources, so you can press ESC or click inside the form. Resize the DataGridView so that it covers the form equally with just a small border left around the DataGridView.
- Be sure the DataGridView is selected and open the Properties window in VS.NET or press F4. Scroll down to (Name) and specify _theGrid as the name. Scroll down to Anchor and anchor the grid to all 4 borders. You should now be able to resize the dialog and have the datagridview be resized with it.
- Right-click the white background of VS.NET and select View Code. Add the following method to the class
// C#
public void BindDataTable(DataTable toBind)
{
_theGrid.DataSource = toBind;
}
' VB.NET
Public Sub BindDataTable(ByVal toBind As DataTable)
_theGrid.DataSource = toBind
End Sub
- Our viewer is now ready to be used. A Typed View is a derived class of the .NET DataTable class so we can pass it to this viewer and have its contents show up in the viewer's DataGridView. We'll also use this same method in the tutorial section with the retrieval stored procedure call. We'll now return to our Main method in Program.cs / Module1.vb.
- We'll fetch the 2nd page of 25 rows of the Invoices Typed View and we'll show that in the viewer. To do that, put the following code in the Main method. You can comment out any code you've left there or remove the code you previously added to the Main method.
// C#
InvoicesTypedView invoices = new InvoicesTypedView();
// simply fetch the 2nd page of 25 rows. We specify no filter,
// no group by collection and we specify 0 for the max number of elements to return which means
// all of them. We also specify that we don't care about duplicate rows.
// We do specify a sort expression, as paging without a sorter isn't really reliable.
SortExpression sorter = new SortExpression();
sorter.Add(InvoicesFields.OrderId | SortOperator.Ascending);
invoices.Fill(0, sorter, true, null, null, null, 2, 25);
// Now that invoices is filled with data, we'll show it in the viewer.
Viewer v = new Viewer();
v.BindDataTable(invoices);
v.ShowDialog();
' VB.NET
Dim invoices As New InvoicesTypedView()
' simply fetch the 2nd page of 25 rows. We specify no filter,
' no group by collection and we specify 0 for the max number of elements to return which means
' all of them. We also specify that we don't care about duplicate rows.
' We do specify a sort expression, as paging without a sorter isn't really reliable.
Dim sorter As New SortExpression()
sorter.Add(InvoicesFields.OrderId Or SortOperator.Ascending)
invoices.Fill(0, sorter, True, Nothing, Nothing, Nothing, 2, 25)
' Now that invoices is filled with data, we'll show it in the viewer.
Dim v As New Viewer()
v.BindDataTable(invoices)
v.ShowDialog()
- Compile and run the project.
Using Retrieval Stored Procecure Calls
We also mapped a retrieval stored procedure call,
SalesByYear. This stored procedure accepts two dates and will return the list of orders which have their ShippedDate between the two specified dates. In the next few steps we'll call this SalesByYear procedure and will show its results in the Viewer we've created in the Using Typed Views section above. If you haven't followed that section yet, at least create the viewer class as illustrated there.
- Clean the Main method in Program.cs / Module1.vb or comment out the code you have there.
- Add to the using / Imports section at the top a using / Imports statement for System.Data
- Add to the Main method the following code. It will first call the stored procedure with two dates, whcih will result in a filled DataTable. This DataTable is then passed to the Viewer class to display the data on screen.
// C#
// we'll grab the results from January 1st 1997 till July 1st 1997.
// Call the procedure with these two dates as parameters. The procedure method will
// return a DataTable with the results
DataTable results = RetrievalProcedures.SalesByYear(new DateTime(1997, 1, 1), new DateTime(1997, 7, 1));
// We've just fetched the data from the database, so we can view it in the viewer
Viewer v = new Viewer();
v.BindDataTable(results);
v.ShowDialog();
' VB.NET
' we'll grab the results from January 1st 1997 till July 1st 1997.
' Call the procedure with these two dates as parameters. The procedure method will
' return a DataTable with the results
Dim results As DataTable = RetrievalProcedures.SalesByYear(New DateTime(1997, 1, 1), New DateTime(1997, 7, 1))
' Now that invoices is filled with data, we'll show it in the viewer.
Dim v As New Viewer()
v.BindDataTable(results)
v.ShowDialog()
- Compile and run the project