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
- Open the Default.aspx page.
- Go to the designer and click on your RadGrid.
- Click the "arrow" like button.
- Open the Choose Data Source drop-down and select "<New data source>".

- In the Data Source Configuration Wizard, choose OpenAccessDataSource and click OK.
- When asked for "which data context should your application use to access the database?" choose OpenAccess.Car and click Finish.
- 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:
- Open the AvailableCars page.
- Do steps 1 to 6 from the above example
- Click on OpenAccessDataSource and write "Available==true" in the where property.

Retrieving Data from the Database Using Code-Behind
To retrieve data from the database using code-behind:
- Open the SeeRates.aspx.cs file.
- 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 |
- 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:
- Open the AdvancedSearch.aspx.cs file.
- 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 |
- 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.