Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / April 2008

Tip: Looking for answers? Try searching our database.

Can't Figure Out How To Sort On Bind

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pbd22 - 28 Apr 2008 07:01 GMT
Hi.

I am returning to an old bit of code in our program and need to figure
out how to sort
my columns on bind. I am sorting on Date (mostly) and some other
values.
Problem is, the code is an ArrayList that seems to do some
tricky stuff with a chache object and I am unable to get any sort of
sorting happening.

I have provided the method below (and some supporting methods). The
interesting bit
of code (to me) is:

               'Assign ColumnOrder to ViewState
               ViewState("SortOrder") = ColumnOrder
               'Set up Cache Object and determine if it exists
               searchCache = CType(Cache.Get("searchCache" &
ColumnOrder), ArrayList)

It looks like the ArrayList is stored in Cache and then when a new
search parameter is
requested, the cache is called and the new parameter is passed as an
argument?

Any help in explaining how to attack this would be very helpful. I am
not too familiar with
doing the sorting on bind and can't do this in the stored procedure.

Thanks!

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub GetDataReader(ByVal ColumnOrder As String)

           Try
               'Assign ColumnOrder to ViewState
               ViewState("SortOrder") = ColumnOrder
               'Set up Cache Object and determine if it exists
               searchCache = CType(Cache.Get("searchCache" &
ColumnOrder), ArrayList)

               'TEMP
               searchCache = Nothing

               If (searchCache Is Nothing) Then

                   'Dim sqlStr As String = "call sp_myContent;"

                   Dim sqlStr As String = "call sp_SearchContent;"

                   Dim strConn As String =
"SERVER=localhost;DATABASE=MCS;" & _
                           "UID=vbuser;PASSWORD=vbuser06492;"
                   Dim MyConnection As New MySqlConnection(strConn)
                   MyConnection.Open()
                   Dim MyCommand As New MySqlCommand(sqlStr,
MyConnection)
                   Dim objDataReader As MySqlDataReader =
MyCommand.ExecuteReader() 'command.close removed
                   'Create instances of the class,
                   Dim bkResults As New ArrayList()
                   'Loop through DataReader
                   While objDataReader.Read()
                       With bkResults
                           If Not objDataReader.IsDBNull(0) Then
                               .Add(New
MySearchResults(objDataReader.GetString(0).Replace("<u>/",
"<u>").Remove(objDataReader.GetString(0).LastIndexOf("__"),
objDataReader.GetString(0).Length).ToString(), _

objDataReader.GetString(1).ToString(),
objDataReader.GetString(2).ToString(),
objDataReader.GetString(3).ToString(),
objDataReader.GetString(4).ToString(),
objDataReader.GetString(4).ToString()))
                           End If
                       End With
                   End While

                   'Insert ArrayList into Cache Object with unique
identifier
                   Cache.Insert("searchCache" & ColumnOrder,
bkResults)
                   'Close DataReader Connection
                   objDataReader.Close()
                   'Bind DataGrid from ArrayList
                   MyDataGrid.DataSource = bkResults

               Else

                   'Bind DataGrid from Cached ArrayList
                   MyDataGrid.DataSource = searchCache

               End If

               MyDataGrid.DataBind()

               'Clear ArrayList
               bkResults = Nothing

           Catch ex As Exception
               myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
           End Try

       End Sub

Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
           Try
               MyDataGrid.CurrentPageIndex = e.NewPageIndex
               GetDataReader(ViewState("SortOrder").ToString())
           Catch ex As Exception
               myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
           End Try
       End Sub

       Sub MyDataGrid_Sort(ByVal sender As Object, ByVal e As
DataGridSortCommandEventArgs)
           Try
               MyDataGrid.CurrentPageIndex = 0

               GetDataReader(SortOrder(e.SortExpression.ToString()))
           Catch ex As Exception
               myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
           End Try

       End Sub

       Function SortOrder(ByVal Field As String) As String

           Try
               If Field = ViewState("SortOrder").ToString() Then
                   SortOrder = Replace(Field, "asc", "desc")
               Else
                   SortOrder = Replace(Field, "desc", "asc")
               End If

           Catch ex As Exception
               myLog.WriteEntry(ex.Message,
EventLogEntryType.Warning)
           End Try

       End Function
Fernando Rodriguez, MCP - 30 Apr 2008 23:02 GMT
You need to get a book about coding basics.

The sort order is not being passed as an argument, it is being concatenated
with the string "searchCache" to create a key for it, so the cache should
countain a copy of the ArrayList with the requested sorting, you're missing
the piece of code where the ArrayList is actually sorted and stored in the
cache.

By the way, storing different copies of the same data but with different
sorting is a very bad practice. I would just store one copy and then sort it
before binding by using the ArrayList's Sort method.

HTH,

> Hi.
>
[quoted text clipped - 143 lines]
>
>        End Function

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.