I have a code that returns data in IList. My webGrid doesn't allow me to
sort with IList returned, it say it only suports DataView, DataTable and
DataSet, not IEnumerable. I don't know how to return the DataSet type when
using the following code:
======== this is my interface =============== =======
namespace WareHouse.DataL ayer.DataObject s
{
/// <summary>
/// Defines methods to access categories and products.
/// This is a database-independent interface. The implementations will
/// be database specific.
/// </summary>
public interface IProductDao
{
/// <summary>
/// Gets a product.
/// </summary>
/// <param name="productId ">Unique product identifier.</param>
/// <returns>Produc t.</returns>
//IList<ProductSe lectProductsAll ();
Product SelectProductsA ll();
}
}
=============== = this is where I create my IList ==============
namespace WareHouse.DataL ayer.DataObject s.SqlServer
{
class SqlServerProduc tDao : IProductDao
{
public IList<ProductSe lectProductsAll ()
{
StringBuilder sql = new StringBuilder() ;
sql.Append("usp _SelectProducts All");
DataTable dt = Db.GetDataTable (sql.ToString() );
IList<Productli st = new List<Product>() ;
foreach (DataRow row in dt.Rows)
{
string productname = row["P_ProductN ame"].ToString();
list.Add(new Product(product name));
}
return list;
}
=============== = This is where I populate the dataset ========
/// <summary>
/// Populates a DataSet according to a Sql statement.
/// </summary>
/// <param name="sql">Sql statement.</param>
/// <returns>Popula ted DataSet.</returns>
public static DataSet GetDataSet(stri ng sql)
{
using (DbConnection connection = factory.CreateC onnection())
{
connection.Conn ectionString = connectionStrin g;
using (DbCommand command = factory.CreateC ommand())
{
command.Connect ion = connection;
command.Command Type = CommandType.Sto redProcedure;
command.Command Text = sql;
using (DbDataAdapter adapter =
factory.CreateD ataAdapter())
{
adapter.SelectC ommand = command;
DataSet ds = new DataSet();
adapter.Fill(ds );
return ds;
}
}
}
}
/// <summary>
/// Populates a DataTable according to a Sql statement.
/// </summary>
/// <param name="sql">Sql statement.</param>
/// <returns>Popula ted DataTable.</returns>
public static DataTable GetDataTable(st ring sql)
{
return GetDataSet(sql) .Tables[0];
}
/// <summary>
/// Populates a DataRow according to a Sql statement.
/// </summary>
/// <param name="sql">Sql statement.</param>
/// <returns>Popula ted DataRow.</returns>
public static DataRow GetDataRow(stri ng sql)
{
DataRow row = null;
DataTable dt = GetDataTable(sq l);
if (dt.Rows.Count 0)
{
row = dt.Rows[0];
}
return row;
}
I tried to return a dataset instead of IList, but it doesnt' seem to work.
Any suggestions?