G
Guest
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.DataLayer.DataObjects
{
/// <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>Product.</returns>
//IList<Product> SelectProductsAll();
Product SelectProductsAll();
}
}
================ this is where I create my IList ==============
namespace WareHouse.DataLayer.DataObjects.SqlServer
{
class SqlServerProductDao : IProductDao
{
public IList<Product> SelectProductsAll()
{
StringBuilder sql = new StringBuilder();
sql.Append("usp_SelectProductsAll");
DataTable dt = Db.GetDataTable(sql.ToString());
IList<Product> list = new List<Product>();
foreach (DataRow row in dt.Rows)
{
string productname = row["P_ProductName"].ToString();
list.Add(new Product(productname));
}
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>Populated DataSet.</returns>
public static DataSet GetDataSet(string sql)
{
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = factory.CreateCommand())
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = sql;
using (DbDataAdapter adapter =
factory.CreateDataAdapter())
{
adapter.SelectCommand = 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>Populated DataTable.</returns>
public static DataTable GetDataTable(string sql)
{
return GetDataSet(sql).Tables[0];
}
/// <summary>
/// Populates a DataRow according to a Sql statement.
/// </summary>
/// <param name="sql">Sql statement.</param>
/// <returns>Populated DataRow.</returns>
public static DataRow GetDataRow(string sql)
{
DataRow row = null;
DataTable dt = GetDataTable(sql);
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?
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.DataLayer.DataObjects
{
/// <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>Product.</returns>
//IList<Product> SelectProductsAll();
Product SelectProductsAll();
}
}
================ this is where I create my IList ==============
namespace WareHouse.DataLayer.DataObjects.SqlServer
{
class SqlServerProductDao : IProductDao
{
public IList<Product> SelectProductsAll()
{
StringBuilder sql = new StringBuilder();
sql.Append("usp_SelectProductsAll");
DataTable dt = Db.GetDataTable(sql.ToString());
IList<Product> list = new List<Product>();
foreach (DataRow row in dt.Rows)
{
string productname = row["P_ProductName"].ToString();
list.Add(new Product(productname));
}
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>Populated DataSet.</returns>
public static DataSet GetDataSet(string sql)
{
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = factory.CreateCommand())
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = sql;
using (DbDataAdapter adapter =
factory.CreateDataAdapter())
{
adapter.SelectCommand = 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>Populated DataTable.</returns>
public static DataTable GetDataTable(string sql)
{
return GetDataSet(sql).Tables[0];
}
/// <summary>
/// Populates a DataRow according to a Sql statement.
/// </summary>
/// <param name="sql">Sql statement.</param>
/// <returns>Populated DataRow.</returns>
public static DataRow GetDataRow(string sql)
{
DataRow row = null;
DataTable dt = GetDataTable(sql);
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?