473,372 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Can I convert IList to a DataSet, DataTable or DataView?

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<ProductSelectProductsAll();

Product SelectProductsAll();

}
}
================ this is where I create my IList ==============
namespace WareHouse.DataLayer.DataObjects.SqlServer
{
class SqlServerProductDao : IProductDao
{
public IList<ProductSelectProductsAll()
{

StringBuilder sql = new StringBuilder();
sql.Append("usp_SelectProductsAll");

DataTable dt = Db.GetDataTable(sql.ToString());

IList<Productlist = 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?
Jul 17 '06 #1
6 16685
I tried to return a dataset instead of IList, but it doesnt' seem to work.
Any suggestions?
You can use an objectdatasource:

http://gridviewguy.com/ArticleDetail...?articleID=139
Jul 17 '06 #2
The problem is that if I use IList, I can't sort my grid when I click there
sort header hyperlink on the grid

thanks
Nick

"Mischa Kroon" wrote:
I tried to return a dataset instead of IList, but it doesnt' seem to work.
Any suggestions?

You can use an objectdatasource:

http://gridviewguy.com/ArticleDetail...?articleID=139
Jul 17 '06 #3

"Nick" <Ni**@discussions.microsoft.comwrote in message
news:E1**********************************@microsof t.com...
The problem is that if I use IList, I can't sort my grid when I click
there
sort header hyperlink on the grid

thanks
Nick
from:
http://www.asp.net/QUICKSTART/aspnet...atasource.aspx

Like SqlDataSource, the ObjectDataSource control supports sorting when the
SelectMethod returns a DataSet, DataView, or DataTable object. Internally,
the ObjectDataSource relies on the DataView.Sort property to perform sorting
in this case. ObjectDataSource also supports custom sorting in the
SelectMethod implementation, which is useful if the method doesn't return a
DataSet, DataView, or DataTable. Custom sorting is configured by setting
SortParameterName property to the name of a method parameter that accepts
the SortExpression from the data source. When the SelectMethod is called,
ObjectDataSource will pass this expression to your method and you can
implement your own sorting logic using this expression. The preceding
example demonstrates custom a custom sorting implementation in the
AuthorsComponent class.

---------

So it is possible, it will just require a bit of work :)

Now for how to return a datatable:

public DataTable SelectProductsAll()
{

StringBuilder sql = new StringBuilder();
sql.Append("usp_SelectProductsAll");

DataTable dt = Db.GetDataTable(sql.ToString());
dt.Columns.Item["P_ProductName"].ColumnName = "productname";

return dt;
}

Since the datatable has an underlying dataview the built in sort command
should work.
And you should be done :)
Jul 17 '06 #4
Excellent, although I am having problems with the following line because
there is no "Items" in the dt.Columns:

dt.Columns.Item["P_ProductName"].ColumnName = "productname";

thanks
Nick

"Mischa Kroon" wrote:
>
"Nick" <Ni**@discussions.microsoft.comwrote in message
news:E1**********************************@microsof t.com...
The problem is that if I use IList, I can't sort my grid when I click
there
sort header hyperlink on the grid

thanks
Nick

from:
http://www.asp.net/QUICKSTART/aspnet...atasource.aspx

Like SqlDataSource, the ObjectDataSource control supports sorting when the
SelectMethod returns a DataSet, DataView, or DataTable object. Internally,
the ObjectDataSource relies on the DataView.Sort property to perform sorting
in this case. ObjectDataSource also supports custom sorting in the
SelectMethod implementation, which is useful if the method doesn't return a
DataSet, DataView, or DataTable. Custom sorting is configured by setting
SortParameterName property to the name of a method parameter that accepts
the SortExpression from the data source. When the SelectMethod is called,
ObjectDataSource will pass this expression to your method and you can
implement your own sorting logic using this expression. The preceding
example demonstrates custom a custom sorting implementation in the
AuthorsComponent class.

---------

So it is possible, it will just require a bit of work :)

Now for how to return a datatable:

public DataTable SelectProductsAll()
{

StringBuilder sql = new StringBuilder();
sql.Append("usp_SelectProductsAll");

DataTable dt = Db.GetDataTable(sql.ToString());
dt.Columns.Item["P_ProductName"].ColumnName = "productname";

return dt;
}

Since the datatable has an underlying dataview the built in sort command
should work.
And you should be done :)
Jul 18 '06 #5
The most reusable solution is to actually inherit from the gridview and
write a little bit of code. I've recoded the gridview to take the
object data source and perform all the sorting internally through a
little bit of reflection. I'll probably post something soon once I have
it a bit more finalized. It covers using the update / delete / select /
paging / sorting for the gridview and has the same interface for the
most part. All I do is grab the datasource, pull in the data, clear the
datasourceID and store that internally and then through reflection sort
the data as well as call the update / delete methods with a little
reflection. I am just validating the performance of it at this point.
The beauty of this type of solution is you don't need to write code for
every single ODS like most people suggest.

Jul 19 '06 #6
Thanks, let me know how it goes.

np

"ap********@gmail.com" wrote:
The most reusable solution is to actually inherit from the gridview and
write a little bit of code. I've recoded the gridview to take the
object data source and perform all the sorting internally through a
little bit of reflection. I'll probably post something soon once I have
it a bit more finalized. It covers using the update / delete / select /
paging / sorting for the gridview and has the same interface for the
most part. All I do is grab the datasource, pull in the data, clear the
datasourceID and store that internally and then through reflection sort
the data as well as call the update / delete methods with a little
reflection. I am just validating the performance of it at this point.
The beauty of this type of solution is you don't need to write code for
every single ODS like most people suggest.

Jul 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Prasad Patil | last post by:
hi, I have a three tier application, I have a method in the datalayer which should either return a (dataset or dataview or datrow), i need to know which should be the best possible option, are...
1
by: Arjen | last post by:
Hi, What are the main differences between a dataset and a dataview? What should I cache dataset or dataview... or what is the best thing to do in what situation? Thanks! Arjen
2
by: Jason Huang | last post by:
Hi, In the ASP.Net, what's the difference between DataSet and DataView? I what situation will I choose to use the DataSet or DataView? Thanks for help. Jason
4
by: slaprade | last post by:
I am loading a weeks worth of web logs into a dataset using Imports Microsoft.Data.Odbc These are text - fixed length fields so I have written a schema for them. The adapter fill looks like this...
10
by: Bernie Yaeger | last post by:
I have a need to add a primary key to a dataset/datatable. How can this be done using a standard oledb data provider? Tx for any help.
7
by: Brett Romero | last post by:
I have a dataset with one table, which has four columns. All are of type INT. I need to convert this dataset into a dataview so I can sort on the last three columns. I may sort one of the three...
5
by: needin4mation | last post by:
Hi, I have a webservice that just returns a count: public DataSet HelloWorld() { OdbcConnection conn = new OdbcConnection("DSN=xxx"); String sqlString = "select count(*) as employee from...
3
by: ASPnewb1 | last post by:
I am currently filling a dataTable then adding this table to a dataset, setting the dataset to the Gridview's datasource. If I set the Gridview to generate columns automatically it will fill the...
1
by: jc | last post by:
RE: Why use a CollectionBase class here vs dataset or dataview? I'm looking at some vb.net 2005 code that was generated from a homegrown Codesmith Template that generate all of the retreival and...
4
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have a DataSet with one table filled. From this DataSet (DataTable), I need to create a SqlDataReader. Does anybody knows how to do this ? Is it possible ?? Thanks for any help.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.