Can't Figure Out How To Sort On Bind

Discussion in 'ASP .Net' started by pbd22, Apr 28, 2008.

  1. pbd22

    pbd22 Guest

    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
    pbd22, Apr 28, 2008
    #1
    1. Advertising

  2. 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,



    "pbd22" <> wrote in message
    news:...
    >
    > 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, Apr 30, 2008
    #2
    1. Advertising

  3. pbd22

    Andrew Guest

    Why don't u try to sort the arraylist before binding ?
    using something like this :

    public class MySearchResults
    {
    public MySearchResults(string aa, string bb, string cc) { }

    public class Comparer : System.Collections.IComparer
    {
    public int Compare(object x, object y)
    {
    MySearchResultsx1 = x as MySearchResults;
    MySearchResultsy1 = y as MySearchResults;

    if (x1 == null || y1 == null)
    throw new ArgumentException("Wrong type");
    else
    return string.Compare(x1.aa, y1.bb);
    }
    }

    cheers
    Andrew

    "Fernando Rodriguez, MCP" wrote:

    > 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,
    >
    >
    >
    > "pbd22" <> wrote in message
    > news:...
    > >
    > > 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

    >
    >
    >
    Andrew, Jun 5, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. logia
    Replies:
    6
    Views:
    631
  2. Chad A. Beckner

    Please Help! Can't figure this out...

    Chad A. Beckner, Jun 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    386
    bloomfield
    Jun 21, 2004
  3. myleslawrence

    Simple but I can't figure it out

    myleslawrence, Jan 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    311
    IPGrunt
    Jan 19, 2005
  4. Albert Hopkins
    Replies:
    3
    Views:
    456
    Steven D'Aprano
    Dec 10, 2008
  5. Navin
    Replies:
    1
    Views:
    661
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page