17 How to use Object Data sources Pages
![17. How to use Object Data sources Pages [565 -579] By Sravya Chava Presentation 17. How to use Object Data sources Pages [565 -579] By Sravya Chava Presentation](https://slidetodoc.com/presentation_image_h2/b0704f4d2fd6763db4a1e5b3daa8213a/image-1.jpg)
17. How to use Object Data sources Pages [565 -579] By Sravya Chava Presentation ID: 31

Outline A Product List application The aspx file The product. DB class How to create a data access class How to design data access class How to create a select method How to create update, delete, and insert methods. How to use attributes to mark a data access class A Category Maintenance application The design The aspx file The code-behind file

The Product List Application

The aspx file Object data source is implemented by object data controls which lets u use data binding with 3 layer architecture for database application. The code for Default. aspx page for product list application using object data source is same as code for dropdown list and datalist control using SQL data source.

Product. DB class A class file is added to App_Code folder using add new item. Two methods are created Get. All. Catergories and Get. Product. By. Category Another method Get. Connection. String is created which is declared as private Get. All. Catergories method starts by creating a connection to Halloween databases by calling Get. Connection. String method Now this methods creates a string which contains the select statement that will retrieve the data from Halloween database Finally the connection is opened Execute reader method of the command object is called and object that contains the requested data is returned to object data source

Description: • The Category drop-down list is bound to an Object. Data. Source control that retrieves a list of categories from the Categories table. • The Data. List control is bound to a second Object. Data. Source control that uses a parameterized query to retrieve the products for a selected category. The Categoryi. D for the parameter is taken from the Selected. Value property of the dropdown list • Both Object. Data. Source controls use a data access class named Product. DB that contains the static methods that return a list of categories and the products for a specific category.


How to create a data access class The most challenging aspect of object data source is developing data access class Data access class have four different types of methods that are used to select, insert, update and delete data. For example; Product. DB class uses select method Data access method can be static or instance methods The preferred is static method to select, insert, delete and update where ever possible.


How to create select method Select statement returns data retrieved from databases Data could be of any form dataset or datatable If the select method returns datatable, the objectdatasource control automatically extracts a dataview from the table and uses view for data binding If select method returns dataset, Object. Data. Source control extracts data view from first data tablein dataset and uses the view for data binding

How parameters are generated • When the insert or delete method is called, one parameter of the business class type is generated and passed to the method. The parameter that's declared in the method can have any name you choose. • One parameter of the business class type is also generated and passed to the update method when this method is called if optimistic concurrency isn't used. The parameter that's declared in the method can have any name you choose. • If optimistic concurrency is specified, two parameters are generated and passed to the update method. One contains the original values, and the other contains the new values.

How to create update, delete, and insert methods To properly design an update, delete, or insert method, you must be aware of how the Object. Data. Source control generates the parameters passed to these methods. The property for each bound field must include both get and set accessors. The object data source uses the set accessors to set the values of the properties based on the values passed to it from the bound control. And the insert, update, and delete methods in the data access class use the get accessors to assign the values of the properties to parameters of command objects. Once the Object. Data. Source control has determined what parameters need to be passed, it uses reflection to determine whether the data access class has a method that accepts the required parameters. Manually enter the name of each field you want to bind. That's because the fields are defined in the data access class and not directly in the data source.

How to use attributes to mark a data access class • You can use Data. Object and Data. Object. Method attributes to mark data access classes and methods. Visual Studio uses these attributes to determine which classes and methods to list in the drop-down lists of the Configure Data Source wizard. • The Data. Object and Data. Object. Method attributes are stored in the System. Component. Model namespace.

A Category Maintenance application Design : It presents the design for this version of the Category Maintenance application. It uses a Grid. View control to let the user update and delete category rows and a Details View control to insert new category rows. Both the Grid View and Details View controls are bound to a single Object. Data. Source control. But the Details View control is used only in Insert mode, so it isn't used to display, update, or delete existing category rows.

These four methods provide the select, update, delete, and insert functions. Since all of these methods are defined as static, an instance of the Category. DB class doesn't have to be created to access the database.

The aspx file In part 1, you can see the aspx code for the Grid View control that displays the category rows. Its Columns collection includes three Bound. Field columns, named Categoryi. D, Short. Name, and Long. Name. In part 2, you can see the aspx code for the Object. Data. Source control. It names Category. DB as the data access class and Category as the business class. It also provides the names of the select, insert, update, and delete methods that will be used to access the data.





Queries

Thank. You
- Slides: 22