0% found this document useful (0 votes)
23 views

Model Binding

The document discusses how to use model binding in ASP.NET to bind data from a database to data controls like GridView and DetailsView using the Entity Framework. It describes creating an Entity Data Model using the wizard to represent database tables as classes. An example CustomerDB class is created to manage customer data using methods like GetCustomers(), GetCustomer(), InsertCustomer(), UpdateCustomer(), and DeleteCustomer(). These methods retrieve, add, update and delete customer data from the database context. GridView and DetailsView controls are added to a web form to display and manage customer records using model binding.

Uploaded by

Abby P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Model Binding

The document discusses how to use model binding in ASP.NET to bind data from a database to data controls like GridView and DetailsView using the Entity Framework. It describes creating an Entity Data Model using the wizard to represent database tables as classes. An example CustomerDB class is created to manage customer data using methods like GetCustomers(), GetCustomer(), InsertCustomer(), UpdateCustomer(), and DeleteCustomer(). These methods retrieve, add, update and delete customer data from the database context. GridView and DetailsView controls are added to a web form to display and manage customer records using model binding.

Uploaded by

Abby P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

How to use Model Binding in ASP.

NET
Up until now, we’ve used data source controls to bind data from a database to a data control. Now we’ll learn how to
use the Entity Framework and model binding to bind data directly to a data control (GridView and DetailsView).

The Entity Framework, or just EF, is a component of ADO.NET that provides another way to work with the data in a
database.

(ADO.NET is a set of classes that expose data access services for .NET Framework programmers.)

The easiest way to create an Entity Data Model is to use the Entity Data Model Wizard. We are going to add into a
Models folder by right-clicking on our package name and adding it, then right-clicking the folder and adding the model:

Name it “Northwind”. After we create it we should see this wizard come up. We want to choose the first option.
After we have configured the wizard as shown above, we will finish and within a minute we
should see this diagram pop up.

The Entity Framework (EF) provides an interface between the database used by an application
and the objects used by an application. EF also provides for submitting changes made to those
objects to the database as recognized with the other files that are made with it.

Each entity in the Entity Data Model contains one or more scalar properties that represent the
columns in the associated table or view. A scalar property is a property that can hold a value,
such as an integer value or a string.

If any two tables that you add to an Entity Data Model are related by a foreign key, an association
is defined between the entity classes based on that relationship. We do not have to worry about
this as we are only concerned with the Customers table, but if we wanted to bind Orders and
Order Details, we would see both tables in the diagram and they would be connected like an ERD.

The main file we will be recognizing/using is the Customer class that is created. This is going to allow us to get and set
the information from the database for each column.
From here, we are going to add a CustomerDB class. This is where we will define the rest of our logic for our model
binding.

Our GridView control will display 3 pieces of data from our database (CustomerID, CompanyName, ContactName), along
with this, we will have a DetailsView control that displays the rest of the Customer’s name that’s selected in the
GridView control. The DetailsView control lets the user update or delete the selected Customer. It also lets the user
insert a new Customer, which is then displayed back in the GridView control.

I have provided the code for you in a text file, but here is a breakdown of it:

 The CustomerDB is a class for managing customer data.


 The constructor initializes the Entity Framework context (db) which is used to interact with the database.
 GetCustomers retrieves a list of customers ordered by their company name.
 GetCustomer takes a grdCustomers parameter (our GridView), which is used to retrieve a specific customer. If
grdCustomers is null, it returns the first customer by company name. Otherwise, it uses the GetCustomerById
method to retrieve the customer based on a unique identifier.
 InsertCustomer is used for adding a new customer. It creates a new Customer entity, attempts to update its
properties from the cxt (which is a model context), adds it to the database context, and saves the changes. It
also handles validation errors and other exceptions.
 UpdateCustomer is used for updating an existing customer. It retrieves the customer based on the CustomerID,
validates the input, and updates the database. It also handles concurrency errors, validation errors, and other
exceptions.
 DeleteCustomer is used for deleting a customer. It retrieves the customer based on the CustomerID, removes it
from the database, and handles concurrency errors and other exceptions.
 Lastly, there is code that includes private helper methods for handling various types of exceptions and errors
that might occur during database operations. These methods help manage validation errors, concurrency errors,
and general database errors.
Now that we’ve got our basics down, it is time to implement. We will create a new web form with Master page named
“Customers.aspx” that uses our Site2.Master page. We will add the following basics below:
From here we will configure our GridView:

And our DetailsView:


I know this is painful, but the functionality pays off!

You might also like