In this task you will learn how to insert data into the database through your application.
 |
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.
|
Inserting Data
To insert object from the form into the database:
- First you need to bind some data to the RadGrid from which you will choose cars.
- Open the BookACar.aspx page.
- Switch to the designer and select the 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.
Replace the existing code with the following:
| C# |
Copy Code |
|
public partial class BookACar : 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 RadGrid1_SelectedIndexChanged(object sender, EventArgs e) { RadTextBox1.Visible = true; RadTextBox2.Visible = true; int carId = Int32.Parse(RadGrid1.SelectedValue.ToString()); var result = from c in cachedContext.Cars where c.CarID == carId select c; RadTextBox1.Text = result.First().Model.ToString(); RadTextBox2.Text = result.First().Make.ToString(); Names.Visible = true; Address.Visible = true; ZIP.Visible = true; Country.Visible = true; City.Visible = true; State.Visible = true; DrivingLicense.Visible = true; Button1.Visible = true; lable3.Visible = true; StartDate.Visible = true; EndDate.Visible = true; Lable1.Visible = true; lable2.Visible = true; } protected void Button1_Click(object sender, EventArgs e) { int carID = Int32.Parse(RadGrid1.SelectedValue.ToString()); DateTime dateToCompare = DateTime.Parse(StartDate.Text); var rentalResult = (from c in cachedContext.Rentalorders where c.Car.CarID == carID select c).ToList(); foreach (RentalOrder rt in rentalResult) { if (((dateToCompare.CompareTo(rt.RentEndDate) < 0) && (dateToCompare.CompareTo(rt.RentStartDate) > 0))) { Response.Redirect("/ErrorPage.aspx"); } } Customer cst = new Customer(); cst.Address = Address.Text; cst.City = City.Text; cst.Country = Country.Text; cst.DrvLicNumber = DrivingLicense.Text; cst.FullName = Names.Text; cst.State = State.Text; cst.ZIPCode = ZIP.Text; Car myCar = new Car(); myCar = (from c in cachedContext.Cars where c.CarID == carID select c).First(); RentalOrder rtl = new RentalOrder(); rtl.DateProcessed = DateTime.Today; rtl.Car = myCar; rtl.Customer = cst; rtl.Employee = (from c in cachedContext.Employees where c.FirstName.Contains("Internet") select c).First(); if (rtl.Employee == null) { rtl.Employee = (from c in cachedContext.Employees select c).First(); } rtl.TankLevel = "full"; rtl.RentStartDate = DateTime.Parse(StartDate.Text); rtl.RentEndDate = DateTime.Parse(EndDate.Text); TimeSpan date = DateTime.Parse(EndDate.Text).Subtract(DateTime.Parse(StartDate.Text)); double days = date.TotalDays; decimal? result; if (days == 1) { result = (from c in cachedContext.Rentalrates where myCar.Category == c.Category select c.Daily).First(); } else if (days > 7) { result = (from c in cachedContext.Rentalrates where myCar.Category == c.Category select c.Monthly).First(); } else { result = (from c in cachedContext.Rentalrates where myCar.Category == c.Category select c.Weekly).First(); } rtl.RateApplied = result; Response.Redirect("/OrderCompleted.aspx"); cachedContext.Add(cst); cachedContext.Add(rtl); cachedContext.SaveChanges(); } } |
You have successfully created the ASP.NET Web application that is built on top of Telerik OpenAccess ORM.