Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Querying the OpenAccessContext and Loading Initial Data
Programmer's Guide > Quick-Start Scenarios > WinForms > Querying the OpenAccessContext and Loading Initial Data

Glossary Item Box

In this task, you will query the OpenAccessContext and will bind the returned data to controls in the application, which you've defined in the previous task. This context class has been generated by using the Telerik OpenAccess Create Model Wizard in the Creating Domain Model task. 

The topics in this section form a walkthrough, and each topic builds on the previous one. At the end of each topic, you will have a functional application.

Querying Cars by Specific Make

To query for Cars which are specific make:

  1. At the top of the partial class definition for the main form, add the following code that creates an OpenAccessContext instance.
    C# Copy Code
    using System.Windows.Forms;
    using SofiaCarRentalLibrary;
    using System.Linq;

    namespace WinFormsOpenAccessIntegration
    {
       
    public partial class Form1 : Form
       {
           
    private SofiaCarRentalDbContext dbContext = new SofiaCarRentalDbContext();
           
    private BindingSource dataSource = new BindingSource();

           
    public Form1()
           {
               InitializeComponent();
           }
       }
    }
    VB.NET Copy Code
    Imports SofiaCarRentalLibrary
    Public Class Form1
        Private dbContext As New SofiaCarRentalDbContext()
        Private dataSource As New BindingSource()
    End Class
  2. In the form designer, double-click the LoadCars button. This opens the code page for the form and creates the btnLoadCars_Click event handler.
  3. In the btnLoadCars_Click event handler method, copy and paste the following code that returns a collection of Car objects, which are filtered by the Make property. Those Car objects are bound to the dgvCars control.
    C# Copy Code
    private void btnLoadCars_Click( object sender, EventArgs e )
    {
       
    this.dataSource.DataSource = dbContext.Cars.Where( c => c.Make == tbMake.Text ).ToList();
       
    this.dgvCars.DataSource = this.dataSource;
    }
    VB.NET Copy Code
    Private Sub btnLoadCars_Click(ByVal sender As Object, ByVal e As EventArgs)
     Me.dataSource.DataSource = dbContext.Cars.Where(Function(c) c.Make = tbMake.Text).ToList()
     Me.dgvCars.DataSource = Me.dataSource
    End Sub

Disposing the Context

The OpenAccessContext implements the IDisposable interface. It is a good practice to dispose the created context when you no longer need it. Once a new instance of the OpenAccessContext is created, a new connection to the database is also created. The connection is closed when the OpenAccessContext is disposed.  The following example illustrates disposing of the context when the FormClosed event is raised.

C# Copy Code
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
   dbContext.Dispose();
}
VB.NET Copy Code
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
 dbContext.Dispose()
End Sub

Testing the Application

To test the application:

  1. Build and start the application.
  2. When the WinForms application is loaded, enter a value in the Make text field or use the default one.
  3. Click LoadCars button.
  4. All Cars for the related Make should be displayed.

In this task, you've successfully created a query that returns Car objects for a specific Make and you've bound those Car objects to a grid control. On the next step, you will update and delete existing Car objects and save these changes to the database.