473,714 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting a dataset

hello.

how can i sort data in a dataset? all the examples i have seen on msdn, etc
are sorting a dataview. this works fine, but i want to return the results
in xml and the dataview doesn't have a .getxml method (unlike the dataset).

any ideas?

thanks.
Nov 12 '05 #1
4 10034
Suzy:
"suzy" <su**@spam.co m> wrote in message
news:Ms******** *****@news-binary.blueyond er.co.uk...
hello.

how can i sort data in a dataset? all the examples i have seen on msdn, etc are sorting a dataview. this works fine, but i want to return the results
in xml and the dataview doesn't have a .getxml method (unlike the dataset).
any ideas?

thanks.

You can't sort a dataset which if you think about it, kind of makes sense.
A dataset can have 0 or more tables, but lets say you had 10 tables, three
of which had relations defined. How would you logically sort this set since
not everything relates to each other? Anyway, you could implement a sort
order of your own, perhaps using IComparable or similar logic, but then
you'll have to define what sorting is (for instance in my example you could
sort by Table Name ) and since a DataSet can involve many tables which may
or may not be related, it's probably not the way to go if you are looking to
sort an individual object.

There's tremendous support for XML in .NET and you can easily create a class
that inherits DataView or DataSet and you can them use IFormatter for
instance or XMLSerializer to serialize your dataview to XML. You can use
the SoapFormatter or any of the other XML Support but you have many options.

Also, instead of creating your own class or subclassing a DataView or using
the SoapFormatter on it, you could simply serialize the dataset to xml.
Whenever you need to sort it, deserialize it, create a DataView, do your
sorting and then use it however you need it. Once you are done, reserialize
it.

Suffice to say you have a bunch of good options and if you need any specific
help implementing any of these strategies, let me know a little more about
your DataSet and I'll do what I can to help you.

Cheers,

Bill

Nov 12 '05 #2
Hi William

Thanks for your reply. You are obviously far more of an expert on .NET than
me :) I am just a beginner so it's all still very new to me. I had never
really heard of the stuff you mentioned in your post (serialization, etc).

Let me tell you my scenario, then maybe you can give some specific advise?
:) Thanks.

Basically, I want to write a tool that allows me to read/write data to a SQL
db. And I want to reference this tool from asp.net.

So for example, I should be able to select a table to retrieve data from,
and also specify which table(s) to join to from the primary table, etc.

Ideally, I would like to return the data in structured hierarchical xml, eg:
if i retrieved a list of authors joined to books, id want something like
this:

<authors>
<author id="1" name="tom clancey" dob="03/04/1950">
<book id="23" title="midnight dream" year="1999" isbn="abc123456 7"/>
<book id="45" title="long road" year="2003" isbn="bff135665 7"/>
</author>
<author id="2" name="mary smith" dob="05/04/1967">
<book id="53" title="never say never again" year="1989"
isbn="cgr465767 "/>
</author>
</authors>
However, the only way I can think of getting my xml in this format is to use
FOR XML EXPLICIT in sql server (which is quite hard for me to generate for
custom queries), so i am returning the data in a flat level xml where every
field in my dataset is returned as a node (1 level xml hierachy). Is there
another way to get my xml in my ideal format without using for xml explicit?
Maybe via xsl transformation or something?

Anyhow, as my data is currently being returned as a single table in a
dataset, I wanted to perform sorting/filtering/paging on it so I could build
it into my tool. I know I can copy my dataset into a dataview and sort the
data that way, but then I am stuck as to how to get that data in xml format
back up my call stack. At the moment I am thinking the only way is to doing
a complete re-read of the db building the sort/filter into the query, what
do you think?
While I am here, can I just ask about my updates/inserts? I've read a bit
about data adapters for updates, but am I right in thinking this is only
best used for bulk updates/inserts? I would probably only be
adding/updating 1 record at a time so I was thinking I would just build up a
query and execute the command directly on the db (without using data
adapters). Do you think this is a good idea?

Many many thanks for your help :) It's good to hear other peoples advice
when you are learning..



"William Ryan eMVP" <do********@com cast.nospam.net > wrote in message
news:uJ******** *****@TK2MSFTNG P11.phx.gbl...
Suzy:
"suzy" <su**@spam.co m> wrote in message
news:Ms******** *****@news-binary.blueyond er.co.uk...
hello.

how can i sort data in a dataset? all the examples i have seen on msdn, etc
are sorting a dataview. this works fine, but i want to return the results in xml and the dataview doesn't have a .getxml method (unlike the

dataset).

any ideas?

thanks.


You can't sort a dataset which if you think about it, kind of makes sense.
A dataset can have 0 or more tables, but lets say you had 10 tables, three
of which had relations defined. How would you logically sort this set

since not everything relates to each other? Anyway, you could implement a sort
order of your own, perhaps using IComparable or similar logic, but then
you'll have to define what sorting is (for instance in my example you could sort by Table Name ) and since a DataSet can involve many tables which may
or may not be related, it's probably not the way to go if you are looking to sort an individual object.

There's tremendous support for XML in .NET and you can easily create a class that inherits DataView or DataSet and you can them use IFormatter for
instance or XMLSerializer to serialize your dataview to XML. You can use
the SoapFormatter or any of the other XML Support but you have many options.
Also, instead of creating your own class or subclassing a DataView or using the SoapFormatter on it, you could simply serialize the dataset to xml.
Whenever you need to sort it, deserialize it, create a DataView, do your
sorting and then use it however you need it. Once you are done, reserialize it.

Suffice to say you have a bunch of good options and if you need any specific help implementing any of these strategies, let me know a little more about
your DataSet and I'll do what I can to help you.

Cheers,

Bill


Nov 12 '05 #3
Sorting can be done only on the views. One of the possible solutions for
your problem is to create a table from the view's data so that you can
persist the table to XML. To do this, you need to first clone the table's
schema from the view's underlying DataTable and import the rows from the
DataView into the cloned DataTable.
---
DataTable dtClone = dv1.Table.Clone ();
foreach (DataRowView drv in dv1) {
dtClone.ImportR ow(drv.Row);
}
DataSet ds = new DataSet();
ds.Tables.Add(d tClone);
string xmlData = ds.GetXml();
---

HTH,
Ravi

"suzy" <su**@spam.co m> wrote in message
news:Ms******** *****@news-binary.blueyond er.co.uk...
hello.

how can i sort data in a dataset? all the examples i have seen on msdn, etc are sorting a dataview. this works fine, but i want to return the results
in xml and the dataview doesn't have a .getxml method (unlike the dataset).
any ideas?

thanks.

Nov 12 '05 #4
Ravi,

Thanks, that method works perfectly.

Much appreciated!

"Ravi[MSFT]" <rv******@onlin e.microsoft.com > wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
Sorting can be done only on the views. One of the possible solutions for
your problem is to create a table from the view's data so that you can
persist the table to XML. To do this, you need to first clone the table's
schema from the view's underlying DataTable and import the rows from the
DataView into the cloned DataTable.
---
DataTable dtClone = dv1.Table.Clone ();
foreach (DataRowView drv in dv1) {
dtClone.ImportR ow(drv.Row);
}
DataSet ds = new DataSet();
ds.Tables.Add(d tClone);
string xmlData = ds.GetXml();
---

HTH,
Ravi

"suzy" <su**@spam.co m> wrote in message
news:Ms******** *****@news-binary.blueyond er.co.uk...
hello.

how can i sort data in a dataset? all the examples i have seen on msdn,

etc
are sorting a dataview. this works fine, but i want to return the results in xml and the dataview doesn't have a .getxml method (unlike the

dataset).

any ideas?

thanks.


Nov 12 '05 #5

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

Similar topics

1
1779
by: Dave S | last post by:
Hi, I'm using a datagrid to display contents of a dataset and I've set the datagrid to allow paging and sorting. Rather than re-retrieve from the DB, I store the original dataset in a Session variable and then rebind to it for each page (which works). I'd also like to be able to sort any of the columns of the datagrid, so I do this using a dataview as shown below triggered from the normal Grid_SortCommand event :
11
16377
by: Nikki | last post by:
Is it possible to sort a dataset rather than a dataview? I have a web service that returns a dataset which I would like to sort before returning it (this is so the sorting is standardised and so applications that see the results as xml don't have to fiddle around and sort it themselves later). I have tried sorting a dataview and adding that dataview's table to the dataset but the results don't remain sorted. The only way I can see to do...
4
2353
by: Mark Travis | last post by:
Hi all, I have written a simple Web Application that displays a query result onto a page using the ASP DataGrid. To Digress ======= Development information about the page is as follows 1. The database used is SqlServer 2000. 2. A Stored Procedure is dragged onto the page from the Server Explorer
3
2757
by: Freeon | last post by:
Hi, I am looking for a way to sort a strong typed dataset. It would seem the most straightforward way is to use a dataview. The only problem is when I use the dataview I seem to loose the strong typed properties from my original dataset Anyone that can point me to an example of how to sort my dataset and maintain the use of my typed properties would be greatly appreciated Thank, Freeon
4
1758
by: Jon Paal | last post by:
I am passing an xml data file to a server control and need to sort the data. The data has three levels(tables). What is the recommended approach to do the sorting ? (using vb.net and asp.net 2.0)
4
8244
by: samb | last post by:
When I use manual databinding to a GridView control, as bellow. 'Retrive a DataSet from database Dim ds As DataSet = uda.GetUsers(conectionString) 'gvUsers - The GridView gvUsers.DataSource = ds gvUsers.DataBind()
4
11161
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is returned from a function. The definitions in the function are: Dim ReportDS As DataSet = New DataSet Dim ReportTable As System.Data.DataTable = New System.Data.DataTable("SendTo") The ReportTable is populated row by row by data gotten back from the
0
2087
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any help........pls urgent ========================================================= <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="TestDatagrids.WebForm3"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">...
6
4075
by: =?Utf-8?B?RGFu?= | last post by:
I am reposting a question from about 3 weeks ago ("sorting capability"). I have an aspx page in which I get the data from a database dynamically, through C# code, by creating a dynamic table using TableHeaderCell and TableHeaderRow. The data is binded to the table by using a DataSet and Data Reader. The responses on the forum made me think that it would be easier to sort the data in the table (when clicking on the header column), by...
0
8715
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
9189
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...
0
9037
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7963
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5964
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
4470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.