Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Inserting and Updating Data
Programmer's Guide > OpenAccess ORM Classic (Old API) > Getting Started > QuickStart > Inserting and Updating Data

Glossary Item Box

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:

  1. First you need to bind some data to the RadGrid from which you will choose cars. 
    1. Open the BookACar.aspx page.
    2. Switch to the designer and select the 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.
  2. 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.