Revamping Web Applications: Adding DAL (Data Access Layer) Part 2

Following the previous post – Revamping Web Applications part 5: Adding DAL (Data Access Layer)

After the Data Access Layer (DAL) has been implemented by moving the ORM into a new project, there would be further errors experienced in our web application. This happens because of two reasons:

  1. Initially, LINQ data source was used which is now no longer used.
  2. The ORM which we have implemented here is Open Access, and syntax to implement CRUD operations are different from that of LINQ.

Hence, we need to change all the LINQ CRUD operations, and the following snippets explains the same. The commented line was the LINQ operation and the non-commented part is the new Open Access syntax. For further knowledge on implementing Open Access CRUD operation click here. \

imageimage

After replacing the LINQ commands we still might see few more errors such as:

Error 1image

Then it has occurred because we have converted the Linq to SQL to Open Access ORM. Hence, the control LinqDataSource will not help us map it to the new ORM.image

Since we have removed the Linq Data Source so we have to also remove the existing LinqDataSource and create a new DataSource.

Solution #1

We can use an OpenAccess DataSource control replacing the LINQ DataSource and keep the same ID. Hence, no more effort is needed.

Solution #2

More preferred practice in the industry is to remove the DataSource control and do the following:

We added the following code under the Page_Load event. We created a DataTable and then linked the DataSource property of the DropDownList with the newly created DataTables.
This helps in insulating the data. We also need to remove the DataSource property from the controls where we had linked to the Linq DataSource.

if (User.Identity.IsAuthenticated)
{
if (!IsPostBack)
{
int userid = (from uid in ndx.UserProfiles
where uid.Username == User.Identity.Name
select uid.UserId).First();
// Storing the value of the userid in a hidden variable
loggedUserId.Value = userid.ToString();
}

MasterData.getMasterData();
ds = Context.Cache["masterdata"] as DataSet;
if (ds != null)
{
DataTable dtcity = ds.Tables["CityMaster"];
DataTable dtcountry = ds.Tables["CountryMaster"];

if (!IsPostBack)
{
citySearchDropDown.DataSource = dtcity;
citySearchDropDown.DataTextField = dtcity.Columns["CityName"].ColumnName;
citySearchDropDown.DataValueField = dtcity.Columns["CityID"].ColumnName;
citySearchDropDown.DataBind();

countryDropDown.DataSource = dtcountry;
countryDropDown.DataTextField = dtcountry.Columns["CountryName"].ColumnName;
countryDropDown.DataValueField = dtcountry.Columns["CountryID"].ColumnName;
countryDropDown.DataBind();
}
}

How to Add telerik:OpenAccessDataSource

    1. Drag the OpenAccessDataSource tool under the OpenAcces group.image
    2. You will have something like this in the design view.image
    3. Click on “Configure Data Source…”
      Here we are asked to save changes to our items. Click Yes.
    4. Choose the data context for your application and click next.image
    5. Choose the table you want to retrieve from the database and click finish.image
    6. We also rename the ID as same as the asp:LinqDataSource so that there is no name-conflict anywhere else in our application. Now your code will look like this.
<telerik:OpenAccessDataSource ID="badgeLDS" runat="server"
ObjectContextProvider="EventNetworking.Data.NetworkingDataContext, EventNetworking.Data"
TypeName="EventNetworking.Data.Badge">
</telerik:OpenAccessDataSource>

Where “Badge” is our table-name.

Note: We have not used OpenAccessDataSource anywhere because it takes direct dependency from the OpenAccess.

Error 2

Another Runtime error you may encounter is over the DataBind property.

This occurs because LINQ sends the data as a list but now since we are using OpenAccess the data returned is in the form of iEnumerable and hence we need to convert it to a List explicitly.

Solution

It can be solved by simply adding the ToList() property to the DataSource.

directoryListView.DataSource = users.ToList();

Read-Revamping Web Applications Part-8 : Upgrading to RadListView

Advertisement

2 thoughts on “Revamping Web Applications: Adding DAL (Data Access Layer) Part 2

  1. Pingback: Revamping Web Applications: Upgrading to RadListView | Telerik Helper

  2. Pingback: Revamping Web Applications: Adding DAL (Data Access Layer) | Telerik Helper

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.