<ObjectDataSource runat=”server” EnablePaging=”true” ID=”ODS2″ />

February 16, 2007

Here i am with another post about object data source (ODS). This time i will discuss some small tidbits when using ODS with GridView,instead of paging and filtering data using ODS.


 First I am going to discuss the deletion of record in Gridview , when it is used with ODS. Typically we use RowCommandEvent and check the commandName property of ‘e’ parameter , to perform the functionality e.g

Protected Sub gvwDiaryEntries_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvwDiaryEntries.RowCommand

Select Case e.CommandName

case “DeleteCmd”

‘do some thing

end select
end sub

But the things change a little bit when GridView is used with ObjectDataSource. In that case, RowCommandEvent is not fired instead ODS Deleting Event is fired.

Protected Sub ODSContacts_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ODSContacts.Deleting

Dim AddressID As Long = e.InputParameters(0)


e.InputParameters.Add(“AddressID”, AddressID)

End Sub

The ODS adds the DataKeyValue of specified row in the InputParameters collection, passed to this event Handler in parameter named ‘e’. But it doesnt give any proper name to it.

So, we need to clear the parameters collection and re-add it. Before that, we have to save the value of the DataKeyValue in arbitrary variable.

The parameter name “AddressID” is the parameter of the Delete Method.The delete method nameis  specified in DeleteMethodName property of ODS. Dont forget to set the DataKeyNames property ot GridView.


Second thing I am going to discuss, is passing of parameters to data source when we are implementing paging in our class.

For example

class mydataclass

public function GetData(ID as integer,ParentID as integer,StartRowIndex as integer,MaxRows as integer)as dataset

‘returns the PAGED data

end function

public function GetDataCount(ID as integer) as dataset

‘returns the count of the TOTAL data

end function

end class

If we bind our class to ODS and enable the paging at ODS, we need not to pass paging parameters (StartRowIndex and MaxRows ) to GetDataCount() method.

To implement this we need to handle the Selecting event of ODS as follows:

Protected Sub ODSContacts_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles ODSContacts.Selecting


If e.ExecutingSelectCount = False Then

‘pass these paramters only when count is not being fetched. These parameters will be passed to GetData() method

e.InputParameters.Add(“ParentID”, 1)

End If

‘pass the other paramters . this parameter is common to both GetDAta() and GEtDataCount() methods

e.InputParameters.Add(“ID”, MyBase.UserID)

End Sub

ExecutingSelectCount property returns true when Count of the data  is being fetched. So, we have only passed paging parameters to ODS if data count is not being fetched. (Remeber, when we use paging at ODS, it take two trips to the server. Refer to my previous post for the details)

So , what happens when selecting event is raised that it checks if the data is being fetched, it passed Id,ParentId and paging parameters (paging parameters are passed automatically) to GetData method.

 Next time , when count is being fetched, it automatically igrnore those paging parameters and pass only those parameters which are explicitly defined e.g. ID in this case.

I hope this article to be very helpfull to all of you. Feel free to contact me if you have any questions.