Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [245]

By Root 5582 0
ObjectDataSource provides an infrastructure for paging, but actual paging must be implemented in the class bound to ObjectDataSource. In the following code snippet, the Customers class has a method, LoadByCountry, that takes two additional parameters to indicate the page size and the index of the first record in the page. The names of these two parameters must be assigned to MaximumRowsParameterName and StartRowIndexParameterName, respectively.

TypeName="DAL.CustomerRepository"

StartRowIndexParameterName="firstRow"

MaximumRowsParameterName="totalRows"

SelectMethod="LoadByCountry">

PropertyName="SelectedValue" />

PropertyName="Text" />

PropertyName="Text" />

The implementation of paging is up to the method and must be coded manually. LoadByCountry provides two overloads, one of which supports paging. Internally, paging is actually delegated to FillCustomerList.

public static CustomerCollection LoadByCountry(string country)

{

return LoadByCountry(country, -1, 0);

}

public static CustomerCollection LoadByCountry(string country,

int totalRows, int firstRow)

{

CustomerCollection coll = new CustomerCollection();

using (SqlConnection conn = new SqlConnection(ConnectionString))

{

SqlCommand cmd;

cmd = new SqlCommand(CustomerCommands.cmdLoadByCountry, conn);

cmd.Parameters.AddWithValue("@country", country);

conn.Open();

SqlDataReader reader = cmd.ExecuteReader();

HelperMethods.FillCustomerList(coll, reader, totalRows, firstRow);

reader.Close();

conn.Close();

}

return coll;

}

As you can see in the companion source code, FillCustomerList simply scrolls the whole result set using a reader and discards all the records that don’t belong in the requested range. You could perhaps improve upon this approach to make paging smarter. What’s important here is that paging is built into your business object and exposed by data source controls to the pageable controls through a well-known interface.

Updating and Deleting Data


To update underlying data using ObjectDataSource, you need to define an update/insert/delete method. All the actual methods you use must have semantics that are well suited to implement such operations. Here are some good prototypes for the update operations:

public static void Save(Employee emp)

public static void Insert(Employee emp)

public static void Delete(Employee emp)

public static void Delete(int id)

More so than with select operations, update operations require parameters. To update a record, you need to pass new values and one or more old values to make sure the right record to update is located and to take into account the possibility of data conflicts. To delete a record, you need to identify it by matching a supplied primary key parameter. To specify input parameters, you can use command collections such as UpdateParameters, InsertParameters, or DeleteParameters. Let’s examine update/insert scenarios first.

To update an existing record or insert a new one, you need to pass new values. This can be done in either of two ways—listing parameters explicitly or aggregating all parameters in an all-encompassing data structure. The prototypes shown previously for Save and Insert follow the latter approach. An alternative might be the following:

void Save(int id, string firstName, string lastName, ...)

void Insert(string firstName, string lastName, ...)

You can use command parameter collections only if the types involved are simple types—numbers, strings, dates.

To make a custom class such as Employee acceptable to the ObjectDataSource control, you need to set the DataObjectTypeName property:

TypeName="DAL.EmployeeRepository"

SelectMethod="Load"

UpdateMethod="Save"

DataObjectTypeName="DAL.Employee">

Return Main Page Previous Page Next Page

®Online Book Reader