Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting numbers in Access and .NET

I have an Access table which has a Number field and a Text field.

I can run a query like this:

SELECT * FROM Table ORDER BY intID ASC
//outputs 1,2,3,10

But when I try to run the same query through the .NET OleDB client, like this:

Private Sub GetData()    
   Using cnDB As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path)
        cnDB.Open()
        Dim SQL As String = "SELECT * FROM Table ORDER BY intID ASC"
        Dim cmd As New OleDbCommand(SQL, cnDB)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        While dr.Read()
            lst.Items.Add(dr.Item("intID") & " - " & dr.Item("strName"))
        End While
        cnDB.Close()
    End Using
End Sub

I get items in the order 1,10,2,3.

What's going on here, and how can I have the data sort "naturally" (1,2,3,10) in both places?

like image 233
Riddari Avatar asked Dec 12 '25 05:12

Riddari


1 Answers

try

SELECT * FROM Table ORDER BY CInt(intID) ASC

to explicitly tell Access to treat this as an integer and not a string. Obviously, something in the OleDbClient is seeing this field as a string (text field) and sorting accordingly.

like image 67
David Avatar answered Dec 14 '25 20:12

David