i would like to implement paging in an object data source control that is based on List<CustomClass> in a code based DAL.
I have
ProspectInfo.cs // this is the dal it contains the following method. This is called from an ObjectDataSource on my aspx page.
I
public List GetProspects()
{
List Prospects = new List();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("Select_Prospects", conn);
cmd.CommandType = CommandType.StoredProcedure;
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
#region create userinfo object
UserInfo ui = new UserInfo();
ui.CustomerType = reader.GetString(2);
... code here ...
Prospects.Add(ui);
#endregion
}
conn.Close();
}
catch (Exception ex)
{
throw (ex);
}
cmd.Dispose();
}
return Prospects;
}
When i run it i get this error:
ObjectDataSource 'objSearchResults' could not find a non-generic method 'GetProspects' that has parameters: maximumRows, startRowIndex.
I am pretty sure i need to either add some new methods and pass it those parameters but i want to know of any other alternatives. Can i base by "ProspectInfo" class on something that will allow paging so i dont have to implement too many methods in my dal?
Basically, there're two ways to implement paging. One is to implement paging in memory and the other is in database. For the first scenario, we can use the function of GridView. You just need to enable its paging function and it will page the data in memory.
For the second scenario, you have to follow these steps:
1) Enable paging in ObjectDataSource and set the required properties. For instance:
2) The methods in the middle class should be like this:
[System.ComponentModel.DataObject(true)]
public class Paging
{
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
public List<test> GetSub(int startRows, int maxRows)
{
samDataContext ctx = new samDataContext();
return ctx.tests.Skip(startRows).Take(maxRows).ToList<test>();
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
public int GetAll()
{
// We may think about caching the result, otherwise it will hit the database twice
samDataContext ctx = new samDataContext();
return ctx.tests.Count();
}
}
Thanks.
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
mcmcomasp
Contributor
6834 Points
1436 Posts
Data Paging with a Custom List
Oct 03, 2008 05:51 PM|LINK
hi all,
i would like to implement paging in an object data source control that is based on List<CustomClass> in a code based DAL.
I have
ProspectInfo.cs // this is the dal it contains the following method. This is called from an ObjectDataSource on my aspx page.
I
public List GetProspects() { List Prospects = new List(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString)) { SqlCommand cmd = new SqlCommand("Select_Prospects", conn); cmd.CommandType = CommandType.StoredProcedure; try { conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { #region create userinfo object UserInfo ui = new UserInfo(); ui.CustomerType = reader.GetString(2); ... code here ... Prospects.Add(ui); #endregion } conn.Close(); } catch (Exception ex) { throw (ex); } cmd.Dispose(); } return Prospects; }When i run it i get this error:
ObjectDataSource 'objSearchResults' could not find a non-generic method 'GetProspects' that has parameters: maximumRows, startRowIndex.
I am pretty sure i need to either add some new methods and pass it those parameters but i want to know of any other alternatives. Can i base by "ProspectInfo" class on something that will allow paging so i dont have to implement too many methods in my dal?
thanks in advance,
mcm
Wencui Qian ...
All-Star
56784 Points
5796 Posts
Microsoft
Re: Data Paging with a Custom List
Oct 07, 2008 03:44 AM|LINK
Hi mcmcomasp,
Basically, there're two ways to implement paging. One is to implement paging in memory and the other is in database. For the first scenario, we can use the function of GridView. You just need to enable its paging function and it will page the data in memory. For the second scenario, you have to follow these steps:
1) Enable paging in ObjectDataSource and set the required properties. For instance:
2) The methods in the middle class should be like this:
Thanks.If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework