?
Solved

How to Sort a Dataset in VB.net?

Posted on 2009-02-19
3
Medium Priority
?
906 Views
Last Modified: 2012-05-06
I have a dataset that I read a CSV file into and I use a reader to get access to the fields. My Question is how can I sort the data so that the reader comes out in a specific order? Thanks, Bart
Public Shared Function ReadCSVintoDataSet(ByVal strFileName As String) As DataSet
 
        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
             Path.GetDirectoryName(strFileName) & _
             "\;Extended Properties=""text;HDR=Yes;FMT=Delimited"""
        Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)
        Dim da As OleDb.OleDbDataAdapter
        Dim ds As DataSet
 
        Try
            'Open Data Adapter to Read from Text file
            da = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & Path.GetFileName(strFileName) & "]", objConn)
            ds = New DataSet("CSVFile")
 
            'Fill dataset using dataadapter
            da.Fill(ds, strFileName)
 
            Return ds
 
        Catch ex As Exception
            Return Nothing
            ' !!! oopsy MsgBox(ex.Message)
        End Try
 
    End Function

Open in new window

0
Comment
Question by:bartteems
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 23684778
Doesn't a simple ORDER BY work for you? This would only need to be added to the select statement, like so

da = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & Path.GetFileName(strFileName) & "] ORDER BY myColumnName", objConn)
0
 

Author Closing Comment

by:bartteems
ID: 31548907
I will give that a try, and if it works I will hit myself in the head for being silly. ;-) Thanks!

it works, thanks
0
 
LVL 39

Expert Comment

by:abel
ID: 23685438
Interesting, because I tried the same and couldn't get it to work, although examples (incomplete) on the internet said that it should work... I must've made some silly mistake somewhere. Glad it works for you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question