Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Querying Persistent Objects and References
Programmer's Guide > OpenAccess ORM Classic (Old API) > Getting Started > QuickStart > Querying Persistent Objects and References

Glossary Item Box

In this task you will learn how to retrieve data from the database. You will also learn how to write simple and more complicated queries.

Telerik OpenAccess ORM Q1 2010 Release comes with several new or renamed assemblies:

  • Telerik.OpenAccess.Query.dll is renamed to Telerik.OpenAccess.35.Extensions.dll
  • Telerik.OpenAccess.40.dll is renamed to Telerik.OpenAcces.40.Extensions.dll
  • There is separation of the Win and Web UI into separate assemblies out of Telerik.OpenAccess.dll. These are Telerik.OpenAccess.Web.dll and Telerik.OpenAccess.Windows.dll assemblies.

Retrieving Data from the Database by Using OpenAccessDataSource

  1. Open the Default.aspx page.
  2. Go to the designer and click on your RadGrid
  3. Click the "arrow" like button.
  4. Open the Choose Data Source drop-down and select "<New data source>".

    ChoosingDataSource
  5. In the Data Source Configuration Wizard, choose OpenAccessDataSource and click OK.
  6. When asked for "which data context should your application use to access the database?" choose OpenAccess.Car and click Finish.
  7. Run your application. Navigate to the Home page. All cars should loaded and displayed in the grid. 

Retrieving and Filtering data from the Database using the OpenAccessDataSource

To retrieve and Filter data from the Database using the OpenAccessDataSource:

  1. Open the AvailableCars page.
  2. Do steps 1 to 6 from the above example
  3. Click on OpenAccessDataSource and write "Available==true" in the where property.

    whereInDataSource

Retrieving Data from the Database Using Code-Behind

To retrieve data from the database using code-behind:

  1.  Open the SeeRates.aspx.cs file.
  2. Add the following using statements. 
    C# Copy Code
    using Telerik.OpenAccess;
    using Telerik.OpenAccess.Query;
    using OpenAccessData;
    VB.NET Copy Code
    Imports Telerik.OpenAccess
    Imports Telerik.OpenAccess.Query
    Imports OpenAccessData
  3. Replace the existing code with the following:
    C# Copy Code
    public partial class SeeRates : System.Web.UI.Page
    {
        private SofiaCarRentalEntityDiagrams cachedContext;

        private CarRentMaster contextHolder;
        protected void Page_Init(object sender, EventArgs e)
        {
            contextHolder = (CarRentMaster)this.Master;
            cachedContext = contextHolder.CurrentContext;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var query = cachedContext.Rentalrates;
            RadGrid1.DataSource = query.ToList();
            RadGrid1.DataBind();
        }
    }
    VB.NET Copy Code
    Private cachedScope As IObjectScope
     Private scopeHolder As CarRentMaster
     Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
      scopeHolder = CType(Me.Master, CarRentMaster)
      cachedScope = scopeHolder.Scope
     End Sub

Retrieving Data from the Database Using More Complicated Search

To retrieve data from the database using more complicated search:

  1.  Open the AdvancedSearch.aspx.cs file.
  2. Add the following using statements: 
    C# Copy Code
    using Telerik.OpenAccess;
    using Telerik.OpenAccess.Query;
    using OpenAccessData;
    VB.NET Copy Code
    Imports Telerik.OpenAccess
    Imports Telerik.OpenAccess.Query
    Imports OpenAccessData
  3. Replace the existing code with the following:
    C# Copy Code
    public partial class AdvanceSearch : System.Web.UI.Page
    {
       
    private SofiaCarRentalEntityDiagrams cachedContext;
       
    private CarRentMaster contextHolder;
       
    protected void Page_Init(object sender, EventArgs e)
       {
           contextHolder = (CarRentMaster)
    this.Master;
           cachedContext = contextHolder.CurrentContext;
       }
       
    protected void Page_Load(object sender, EventArgs e)
       {
       }
       
    protected void Button1_Click(object sender, EventArgs e)
       {
           
    string carInfo = RadTextBox1.Text;
           var query =
    this.cachedContext.Cars;
           
    if(RadioButton1.Checked == true)
           {
               query = query.Where(x=> x.Make.Equals(carInfo));
           }
           
    else
           {
               
    query = query.Where(x=>x.Model.Equals(carInfo));
           }
           
    if (CheckBox1.Checked == true)
           {
               query = query.Where(x => x.ABS == true);
           }
           
    if (CheckBox2.Checked == true)
           {
               query = query.Where(x => x.AirConditioner == true);
           }
           
    if (CheckBox3.Checked == true)
           {
               query = query.Where(x => x.Mp3layer == true);
           }
           
    if (CheckBox4.Checked == true)
           {
               query = query.Where(x => x.ASR == true);
           }
           
    if (CheckBox5.Checked == true)
           {
               query = query.Where(x => x.Available == true);
           }
           
    if (CheckBox6.Checked == true)
           {
               query = query.Where(x => x.DVDPlayer == true);
           }
           
    if (CheckBox7.Checked == true)
           {
               query = query.Where(x => x.Navigation == true);
           }
           
           RadGrid1.DataSource = query.ToList();
           RadGrid1.DataBind();
       }
    }

Next Step

You have completed all the pages that are performing different data retrieval from the database. Now its time to learn how to perform updates in the database. See Inserting and Updating Data.