Tuesday, November 24, 2009

Getting data from ObjectDataSource by Code

You may want to get the data from an ObjectDataSource via the code behind. Here's a way to do so:

DataView dv = ObjectDataSource1.Select() as DataView;
DataViewRowCollection dvRows = dv.Table.Rows;
string myContent = dvRows[0][0];

This returns the value in the first column of the first row of the results.

The select() method also returns other data types as shown below.

Return type

Action

IEnumerable

The IEnumerable is returned by the Select method.

DataTable

A DataView is created by using the DataTable and returned by the Select method.

DataView

The DataView is returned by the Select method.

DataSet

The first DataTable of the DataSet is extracted and a DataView is created and returned by the Select method.

Object

The object is wrapped in a one-element IEnumerable collection and returned by the Select method.

No comments:

Post a Comment