This topic is locked

Change Data Access Method?

5/25/2009 9:57:55 PM
ASPRunner.NET General questions
A
adamherman@hotmail.com author

Hi,
I am in the process of evaluating ASPRunner.NET for use as a rapid development tool in our company. I like a lot of what it has to offer and have a question
The default Select Method for the object data source of the page seems to always default to "FetchAllPaged". What is the best/easiest way to change this method to a SQL statement that I can embed in the code so I can change it dynamically? I am trying to automatically filter the result set in the grid based on a variety of parameters/conditions.
Is this the best way to do this?
Thanks!

Sergey Kornilov admin 5/27/2009

The easiest way is to extend controller class. Suppose you have a

table "Customers" then there is a matching controller class

CustomersController in the project. This class is partial so you can

add your own methods to it in a separate file or use existing

CustomersController.cs. Below is a sample method that does exactly

that - gets a collection of customer objects by SQL query.
[System.ComponentModel.DataObject]

public partial class CustomersController

{

[DataObjectMethod(DataObjectMethodType.Select, false)]

public CustomersCollection FetchBySQL(string sql)

{

QueryCommand cmd = new QueryCommand(sql);

CustomersCollection coll = new CustomersCollection();

coll.LoadAndCloseReader(DataService.GetReader(cmd));

return coll;

}

}
Then replace FetchAllPaged call with this method call. Don't forget

that FetchAllPaged has more parameters than FetchBySQL. ASPRunner.NET

uses SubSonic open source data access library to generate data access

layer and you can find more info how to use it on

http://subsonicproject.com/

A
adamherman@hotmail.com author 5/28/2009

Thanks, I appreciate the example. My project is a VB project and I am a VB guy. Do you have any examples in VB so I can test it in my project?
Thanks!

The easiest way is to extend controller class. Suppose you have a

table "Customers" then there is a matching controller class

CustomersController in the project. This class is partial so you can

add your own methods to it in a separate file or use existing

CustomersController.cs. Below is a sample method that does exactly

that - gets a collection of customer objects by SQL query.
[System.ComponentModel.DataObject]

public partial class CustomersController

{

[DataObjectMethod(DataObjectMethodType.Select, false)]

public CustomersCollection FetchBySQL(string sql)

{

QueryCommand cmd = new QueryCommand(sql);

CustomersCollection coll = new CustomersCollection();

coll.LoadAndCloseReader(DataService.GetReader(cmd));

return coll;

}

}
Then replace FetchAllPaged call with this method call. Don't forget

that FetchAllPaged has more parameters than FetchBySQL. ASPRunner.NET

uses SubSonic open source data access library to generate data access

layer and you can find more info how to use it on

http://subsonicproject.com/

Sergey Kornilov admin 5/29/2009

Try this:

Public Partial Class CustomersController

<DataObjectMethod(DataObjectMethodType.[Select], False)> _

Public Function FetchBySQL(ByVal sql As String) As CustomersCollection

Dim cmd As New QueryCommand(sql)

Dim coll As New CustomersCollection()

coll.LoadAndCloseReader(DataService.GetReader(cmd))

Return coll

End Function

End Class