F
Froefel
I have written a simple DAL that returns a typed list of objects as a
source for a gridview.
I also added the necessary paging and sorting plumbing needed when
binding a gridview to an ObjectDataSource. That part is working fine.
However, the next step is to provide a search button to only populate
the grid on demand (not on pageload) and provide a few web controls
that drive the search criteria. I'm stuck on both of these tasks.
Currently, in my aspx page, the ObjectDataSource is defined as
follows:
<asp:ObjectDataSource
ID="productsDataSource"
runat="server"
TypeName="ProductDAL"
SelectMethod="GetProducts"
SelectCountMethod="TotalNumberOfProducts"
SortParameterName="SortExpression"
StartRowIndexParameterName="startRowIndex"
MaximumRowsParameterName="maximumRows"
EnablePaging="true">
</asp:ObjectDataSource>
Note: The code-behind file doesn't contain any code.
The ProductDAL looks as follows:
public class ProductDAL
{
private readonly static string connString =
ConfigurationManager.ConnectionStrings["NWConnectionString"].ConnectionString;
public static List<Product> GetProducts()
{
return GetProducts(int.MaxValue, 0, string.Empty);
}
public static List<Product> GetProducts(int maximumRows, int
startRowIndex)
{
return GetProducts(maximumRows, startRowIndex, string.Empty);
}
public static List<Product> GetProducts(string SortExpression)
{
return GetProducts(int.MaxValue, 0, SortExpression);
}
public static List<Product> GetProducts(int maximumRows, int
startRowIndex, string SortExpression)
{
// returns a list of Product instances based on the
// data in the Northwind Products table
string sql = @"SELECT ProductID, ProductName, QuantityPerUnit,
UnitPrice, UnitsInStock FROM Products";
if (SortExpression != string.Empty)
sql += " ORDER BY " + SortExpression;
//here we're using Microsoft's DAAB objects.
SqlDataReader dr = SqlHelper.ExecuteReader(connString,
CommandType.Text, sql);
List<Product> results = new List<Product>();
int itemsRead = 0;
int currentIndex = 0;
while (dr.Read() && itemsRead < maximumRows)
{
if (currentIndex >= startRowIndex)
{
Product product = new Product();
product.ProductID = Convert.ToInt32(dr["ProductID"]);
product.ProductName = dr["ProductName"].ToString();
product.QuantityPerUnit = dr["QuantityPerUnit"].ToString();
if (dr["UnitPrice"].Equals(DBNull.Value))
product.UnitPrice = 0;
else
product.UnitPrice = Convert.ToDecimal(dr["UnitPrice"]);
if (dr["UnitsInStock"].Equals(DBNull.Value))
product.UnitsInStock = 0;
else
product.UnitsInStock = Convert.ToInt32(dr["UnitsInStock"]);
results.Add(product);
itemsRead++;
}
currentIndex++;
}
return results;
}
public static int TotalNumberOfProducts()
{
return GetProducts().Count;
}
}
Based on the above code, how do I provide the SelectedValue of say a
DropDownList to the GetProducts function so it can adjust the SQL
accordingly. Note that all of the criteria controls need to be
optional (i.e. selecting [All] in a control excludes the criteria).
Also I need to take into account that one of the criteria controls is
a multi-select ListBox which should result in an IN(a,b,c) clause in
the SQL statement.
Being very new at web development with ASP.NET, I've been puling my
hair out on how to accomplish this, so any help would be greatly
appreciated.
-- Hans
source for a gridview.
I also added the necessary paging and sorting plumbing needed when
binding a gridview to an ObjectDataSource. That part is working fine.
However, the next step is to provide a search button to only populate
the grid on demand (not on pageload) and provide a few web controls
that drive the search criteria. I'm stuck on both of these tasks.
Currently, in my aspx page, the ObjectDataSource is defined as
follows:
<asp:ObjectDataSource
ID="productsDataSource"
runat="server"
TypeName="ProductDAL"
SelectMethod="GetProducts"
SelectCountMethod="TotalNumberOfProducts"
SortParameterName="SortExpression"
StartRowIndexParameterName="startRowIndex"
MaximumRowsParameterName="maximumRows"
EnablePaging="true">
</asp:ObjectDataSource>
Note: The code-behind file doesn't contain any code.
The ProductDAL looks as follows:
public class ProductDAL
{
private readonly static string connString =
ConfigurationManager.ConnectionStrings["NWConnectionString"].ConnectionString;
public static List<Product> GetProducts()
{
return GetProducts(int.MaxValue, 0, string.Empty);
}
public static List<Product> GetProducts(int maximumRows, int
startRowIndex)
{
return GetProducts(maximumRows, startRowIndex, string.Empty);
}
public static List<Product> GetProducts(string SortExpression)
{
return GetProducts(int.MaxValue, 0, SortExpression);
}
public static List<Product> GetProducts(int maximumRows, int
startRowIndex, string SortExpression)
{
// returns a list of Product instances based on the
// data in the Northwind Products table
string sql = @"SELECT ProductID, ProductName, QuantityPerUnit,
UnitPrice, UnitsInStock FROM Products";
if (SortExpression != string.Empty)
sql += " ORDER BY " + SortExpression;
//here we're using Microsoft's DAAB objects.
SqlDataReader dr = SqlHelper.ExecuteReader(connString,
CommandType.Text, sql);
List<Product> results = new List<Product>();
int itemsRead = 0;
int currentIndex = 0;
while (dr.Read() && itemsRead < maximumRows)
{
if (currentIndex >= startRowIndex)
{
Product product = new Product();
product.ProductID = Convert.ToInt32(dr["ProductID"]);
product.ProductName = dr["ProductName"].ToString();
product.QuantityPerUnit = dr["QuantityPerUnit"].ToString();
if (dr["UnitPrice"].Equals(DBNull.Value))
product.UnitPrice = 0;
else
product.UnitPrice = Convert.ToDecimal(dr["UnitPrice"]);
if (dr["UnitsInStock"].Equals(DBNull.Value))
product.UnitsInStock = 0;
else
product.UnitsInStock = Convert.ToInt32(dr["UnitsInStock"]);
results.Add(product);
itemsRead++;
}
currentIndex++;
}
return results;
}
public static int TotalNumberOfProducts()
{
return GetProducts().Count;
}
}
Based on the above code, how do I provide the SelectedValue of say a
DropDownList to the GetProducts function so it can adjust the SQL
accordingly. Note that all of the criteria controls need to be
optional (i.e. selecting [All] in a control excludes the criteria).
Also I need to take into account that one of the criteria controls is
a multi-select ListBox which should result in an IN(a,b,c) clause in
the SQL statement.
Being very new at web development with ASP.NET, I've been puling my
hair out on how to accomplish this, so any help would be greatly
appreciated.
-- Hans