Hi Guys, I am going to tell you how can we use object datasource with specifying different select method at runtime.
What does Object Datasource Do for Us ?
Object Datasource contains the record set of specified collection and later this can be bind to any data control available like gridview, list view etc.
Following are the few properties which we need to specify when using object datasource
a)Type Name : This is the name of class which method will be binded to the object datasource.
b)Select Method : Name of the method which will return the returns the collection.
Now lets take a look at the following :
its a simple object datasource with set the above properties.
which retrieves all the available biller as datatable from method getall billers.
and on page load I have bind objectdatasource to gridview as follows :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindBillerGrid("GetAllBillers", "DataAccess.DALBiller");
}
}
private void BindBillerGrid(string selectmethod,string typename)
{
//bind objDSBiller to GrdVwBiller
GrdVwBiller.DataSource = objDSBiller;
GrdVwBiller.DataBind();
}
Now I want to set a search method in Select Method of ObjDS with some parameters, but before doing this I need to look the sequence diagram of object ds which is as follows :
so before fetching data from database objectds's Selecting method is called which sets the required parameters for Defined method in property select method for now, and then invokes the specified method and after which objectds’s selected method is invoked.
so I did the following to specify two parameters here for Search Method of biller inside selecting method of objds.
protected void objDSBiller_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (objDSBiller.SelectMethod=="SearchBiller")
{
//add parameters to objDSBiller
objDSBiller.SelectParameters.Add(param1);
objDSBiller.SelectParameters.Add(param2);
//set values of parameters
e.InputParameters["columnname"] = ddlBillerColumn.SelectedValue;
e.InputParameters["value"] = txtSearch.Text.Trim();
}
}
param1,param2 are defined in variable region as follows
#region variables
Parameter param1 = new Parameter("columnname", TypeCode.String);
Parameter param2 = new Parameter("value", TypeCode.String);
#endregion
all the things will work fine except you can get an exception something like
“ObjectDataSource 'XXX' could not find a non-generic method 'XXX' that has parameters: XXX, XXX ”
to resolve this exception we need to look at seqeunce diagram again that after fetching records from db object datasource binds the data with data control like gridview so we can remove the added parameter which were for specified select method as follows :
//bind objDSBiller to GrdVwBiller
GrdVwBiller.DataSource = objDSBiller;
GrdVwBiller.DataBind();
//remove parameters after objectdatasource has retrieved data.
if (objDSBiller.SelectMethod == "SearchBiller")
{
objDSBiller.SelectParameters.Remove(param1);
objDSBiller.SelectParameters.Remove(param2);
}
so that object datasource’s property select method can have any other method which doesn’t have input parameters and if its having then we can specify and remove them as discussed above.
here is the complete code for the stuff discussed above
public partial class People_Biller_BillerGrid : System.Web.UI.Page
{
#region variables
BLLBiller objBllbiler = new BLLBiller();
Biller biller = new Biller();
Parameter param1 = new Parameter("columnname", TypeCode.String);
Parameter param2 = new Parameter("value", TypeCode.String);
#endregion
#region functions
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindBillerGrid("GetAllBillers", "DataAccess.DALBiller");
}
}
private void BindBillerGrid(string selectmethod,string typename)
{
//set selectmethod,typename of objDSBiller
objDSBiller.SelectMethod = selectmethod;
objDSBiller.TypeName = typename;
//bind objDSBiller to GrdVwBiller
GrdVwBiller.DataSource = objDSBiller;
GrdVwBiller.DataBind();
//remove parameters after objectdatasource has retrieved data.
if (objDSBiller.SelectMethod == "SearchBiller")
{
objDSBiller.SelectParameters.Remove(param1);
objDSBiller.SelectParameters.Remove(param2);
}
}
protected void objDSBiller_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (objDSBiller.SelectMethod=="SearchBiller")
{
//add parameters to objDSBiller
objDSBiller.SelectParameters.Add(param1);
objDSBiller.SelectParameters.Add(param2);
//set values of parameters
e.InputParameters["columnname"] = ddlBillerColumn.SelectedValue;
e.InputParameters["value"] = txtSearch.Text.Trim();
}
}
protected void GrdVwBiller_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//change pageindex and bind datasource
GrdVwBiller.PageIndex = e.NewPageIndex;
GrdVwBiller.DataSource = objDSBiller;
GrdVwBiller.DataBind();
}
protected void GrdVwBiller_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandArgument == "Edit")
{
//check if edit is clicked
int billerid;
//get the current row clicked.
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
//Get the current row biller id.
billerid = Convert.ToInt32(GrdVwBiller.DataKeys[row.RowIndex].Value);
//redirect user to Edit Biller Page.
Response.Redirect("~/People/Biller/AddModifyBiller.aspx?billerid=" + billerid);
}
else if (e.CommandArgument == "View")
{
//check if edit is clicked
int billerid;
//get the current row clicked.
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
//Get the current row biller id.
billerid = Convert.ToInt32(GrdVwBiller.DataKeys[row.RowIndex].Value);
//redirect user to View Biller Page.
Response.Redirect("~/People/Biller/ViewBiller.aspx?billerid=" + billerid);
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
BindBillerGrid("SearchBiller", "DataAccess.DALBiller");
}
protected void btnClear_Click(object sender, EventArgs e)
{
//clear the search data and redisplay gridbiller
BindBillerGrid("GetAllBillers", "DataAccess.DALBiller");
}
#endregion
}