473,651 Members | 2,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 objectdatasourc e:

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

"Nick" <Ni**@discussio ns.microsoft.co mwrote in message
news:E1******** *************** ***********@mic rosoft.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 ObjectDataSourc e control supports sorting when the
SelectMethod returns a DataSet, DataView, or DataTable object. Internally,
the ObjectDataSourc e relies on the DataView.Sort property to perform sorting
in this case. ObjectDataSourc e 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
SortParameterNa me property to the name of a method parameter that accepts
the SortExpression from the data source. When the SelectMethod is called,
ObjectDataSourc e 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
AuthorsComponen t class.

---------

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

Now for how to return a datatable:

public DataTable SelectProductsA ll()
{

StringBuilder sql = new StringBuilder() ;
sql.Append("usp _SelectProducts All");

DataTable dt = Db.GetDataTable (sql.ToString() );
dt.Columns.Item["P_ProductN ame"].ColumnName = "productnam e";

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_ProductN ame"].ColumnName = "productnam e";

thanks
Nick

"Mischa Kroon" wrote:
>
"Nick" <Ni**@discussio ns.microsoft.co mwrote in message
news:E1******** *************** ***********@mic rosoft.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 ObjectDataSourc e control supports sorting when the
SelectMethod returns a DataSet, DataView, or DataTable object. Internally,
the ObjectDataSourc e relies on the DataView.Sort property to perform sorting
in this case. ObjectDataSourc e 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
SortParameterNa me property to the name of a method parameter that accepts
the SortExpression from the data source. When the SelectMethod is called,
ObjectDataSourc e 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
AuthorsComponen t class.

---------

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

Now for how to return a datatable:

public DataTable SelectProductsA ll()
{

StringBuilder sql = new StringBuilder() ;
sql.Append("usp _SelectProducts All");

DataTable dt = Db.GetDataTable (sql.ToString() );
dt.Columns.Item["P_ProductN ame"].ColumnName = "productnam e";

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********@gma il.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
1381
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 there any links or documents which can help me understand. Thanx in advance. Prasad
1
4246
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
2681
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
4316
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 Dim dt As New DataTable() Dim cnString As String Dim adapter As New OdbcDataAdapter() Dim qs1 As String = "Select * from dl" 'first part of query .... .... ....
10
27845
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
8718
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 or all three at once. This dataview will display its results in a winform datagrid. Right now, I create the dataset from a datareader: while(dr.Read()) { drow = _filteredCriteriaDataSet.Tables.NewRow();
5
6840
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 employees"; DataSet myResults = new DataSet(); OdbcDataAdapter myAdapter = new OdbcAdapter(sqlString, conn); myAdapter.Fill(myResults); return myResults;
3
7396
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 grid just fine, but I can't get the automated column (column index 1) to be set to readonly. (Column 0 is actually an automated column of edit buttons) here's my code: DataSet ds = new DataSet(); DataTable dt = new DataTable();...
1
2583
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 update code for typical vb.net / asp.net data maintenance applications. For some reason they have two class files for each table, one is a collection class. Oddly both have the same properties for each table column duplicated.. a lot of duplicate...
4
19752
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
8357
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.