Gridview and ObjectDataSource - SortExpression sent as empty string to DAL function.

Last post 06-30-2007 3:17 PM by ProblemChild. 0 replies.

Sort Posts:

  • Gridview and ObjectDataSource - SortExpression sent as empty string to DAL function.

    06-30-2007, 3:17 PM
    • Member
      point Member
    • ProblemChild
    • Member since 06-29-2007, 4:26 PM
    • Posts 2

    Good Day All,

    Well, it's my first post, and I always hate coming to a new board and asking for help right off the bat, but I'm stuck.

    I've done a lot of searching, and found similar issue but none quite the exact same as mine, so I'm posting in hopes that someone

    can tell me if this is a known bug, or something I am doing wrong.

    Problem description:

    I have a webform application I am writing that creates different screens and objects based on current session state, and command buttons clicked.

    For this reason, I am creating everything dynamically in code so that I can control when certain objects are loaded and placed into memory.

    In my ASPX page, I have some static HTML, and an asp.net PlaceHolder control.

    The code behind calls a function DrawScreen(CheckMode()) on Page_Load, the checkmode determines what to draw on the screen.

    One of those modes is a searchmode, in this mode, I created a table with form fields for Keywords, Product ID and Catalog, and a Find Now button.

    It also runs a Function called execSearch that looks to see if there were keywords, product id, or Catalog on postback and returns a gridview with the data.

    If the function didn't return "nothing" then it is a gridview and it is added to the table, and displayed to the user. In this way, as long as the page is in search mode, onpostback, the table is drawn, and the search is executed if there are items in the search fields. Which results in a gridview being drawn to the screen with the same data and objectID. Here is the function:

     

     

    1    Public Function execSearch(ByVal intPiAcct As Integer) As GridView
    2    
    3    'Every time the page loads in searchmode, a search is executed only If there are values submitted in the form.
    4    
    5    'Check if Search Data was submitted
    6    
    7    Dim search As srchKey
    8    
    9    search = GetSearchInfo()
    10   
    11   If search.srchType <> "" Then
    12   
    13   'Only if search data is present, does the routine actually execute.
    14   
    15   'Create a datasource which uses functions from catDAL to return it's data
    16   
    17   Dim objSearch As New ObjectDataSource
    18   
    19   Dim SearchGrid As GridView = New GridView
    20   
    21   objSearch.ID = "objSearch"
    22   
    23   objSearch.ConvertNullToDBNull = True
    24   
    25   objSearch.TypeName = "catDal"
    26   
    27   
    28   
    29   If search.srchType = "Keywords" Then
    30   
    31   'If the user entered Keywords then 'we'll setup the gridview for Keywords results telling it which method to call from catDal
    32   
    33   objSearch.EnablePaging = True
    34   
    35   objSearch.SortParameterName = "SortCriteria"
    36   
    37   objSearch.SelectMethod = "ByKeyword"
    38   
    39   objSearch.SelectCountMethod = "GetKeyCount"
    40   
    41   SearchGrid.AllowPaging = True
    42   
    43   SearchGrid.AllowSorting = True
    44   
    45   SearchGrid.PageSize = search.recsPerPage
    46   
    47   ElseIf search.srchType = "Product ID" Then
    48   
    49   'If product id, then only one record will come back So no paging info is needed.
    50   
    51   objSearch.SelectMethod = "ByProdID"
    52   
    53   ElseIf search.srchType = "Catalog" Then
    54   
    55   'Similar to keywords, this result set requires paging.
    56   
    57   objSearch.EnablePaging = True
    58   
    59   objSearch.SortParameterName = "SortCriteria"
    60   
    61   objSearch.SelectMethod = "ByCatalog"
    62   
    63   objSearch.SelectCountMethod = "GetCatCount"
    64   
    65   SearchGrid.AllowPaging = True
    66   
    67   SearchGrid.AllowSorting = True
    68   
    69   SearchGrid.PageSize = search.recsPerPage
    70   
    71   End If
    72   
    73   'When returning data, the query will always require these parameters.
    74   
    75   objSearch.SelectParameters.Add("piACCT", intPiAcct)
    76   
    77   objSearch.SelectParameters.Add("srchCriteria", search.srchCriteria)
    78   
    79   SearchGrid.ID = "srchGrid"
    80   
    81   SearchGrid.Width = Unit.Percentage(100)
    82   
    83   SearchGrid.GridLines = GridLines.Both
    84   
    85   Dim rowstyle As TableItemStyle = SearchGrid.RowStyle
    86   
    87   rowstyle.BorderStyle = BorderStyle.Solid
    88   
    89   rowstyle.BorderWidth = Unit.Pixel(10)
    90   
    91   rowstyle.BorderColor = Drawing.Color.Black
    92   
    93   rowstyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#F0E2D5")
    94   
    95   Dim altrowstyle As TableItemStyle = SearchGrid.AlternatingRowStyle
    96   
    97   altrowstyle.BorderStyle = BorderStyle.Solid
    98   
    99   altrowstyle.BorderWidth = Unit.Pixel(10)
    100  
    101  altrowstyle.BorderColor = Drawing.Color.Black
    102  
    103  altrowstyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#D1D3E0")
    104  
    105  Dim headerrowsrtyle As TableItemStyle = SearchGrid.HeaderStyle
    106  
    107  headerrowsrtyle.BorderStyle = BorderStyle.Solid
    108  
    109  headerrowsrtyle.BorderWidth = Unit.Pixel(10)
    110  
    111  headerrowsrtyle.BorderColor = Drawing.Color.Black
    112  
    113  headerrowsrtyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#A5A7C1")
    114  
    115  SearchGrid.AutoGenerateColumns = True
    116  
    117  'For paging, we must create our own routine to adjust the paged data here we tell vb what routine we created to handle paging.
    118  
    119  AddHandler SearchGrid.PageIndexChanging, AddressOf srchGrid_ChangePage
    120  
    121  AddHandler SearchGrid.Sorting, AddressOf srchGrid_Sorting
    122  
    123  'Tell the grid which data source to use
    124  
    125  SearchGrid.DataSource = objSearch
    126  
    127  Return SearchGrid
    128  
    129  SearchGrid = Nothing
    130  
    131  Else
    132  
    133  Return Nothing
    134  
    135  End If
    136  
    137  End Function
    138  
    139  
    
      

    At this point, I am concerned only with the Keywords search, you can see the objectdatasource is setup for a sortparametername of "SortCriteria"

    It uses a class called catDAL and it's selectmethod is ByKeyword. 2 Constant Select parameters are added: intPiAcct which is the catalog account number to search in, and searchCriteria which is the string of keywords enterd by the user. This draws the page on the screen with the search results. and enables sorting and paging. Handlers are added for the Sorting and pageIndexChanging events.

    The routines for handling sorting and paging events are here:

    1     'Responds to the Grid Paging event in order to setup the grid to
    2        'view the next page of data
    3    
    4        Private Sub srchGrid_ChangePage(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    5            Dim searchgrid As GridView = sender
    6            searchgrid.PageIndex = e.NewPageIndex
    7            searchgrid.DataBind()
    8        End Sub
    9    
    10   
    11   Private Sub srchGrid_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
    12           'CatalogHolder.Controls.Add(New LiteralControl(e.SortBLOCKED EXPRESSION
    13           Dim searchgrid As GridView = sender
    14           Dim objsearch As ObjectDataSource = searchgrid.DataSource
    15           Dim strSortCriteria As String
    16           strSortCriteria = "[" & e.SortExpression & "] "
    17           If e.SortDirection = SortDirection.Ascending Then
    18               strSortCriteria = strSortCriteria & " asc"
    19           Else
    20               strSortCriteria = strSortCriteria & " desc"
    21           End If
    22           objsearch.SelectParameters.Add(objsearch.SortParameterName, strSortCriteria)
    23           searchgrid.DataBind()
    24   
    25       End Sub

    The paging works fine, it's the sorting which will not work for me, as the subject line says, the SortCriteria is never placed into the variable passed

    to the ByKeyword Method.

     

    I have created a "non-generic" ByKeyword method in my catDAL which has a signature of:

    Public Function ByKeyword(ByVal piAcct As Integer, ByVal srchCriteria As String, ByVal SortCriteria As String, ByVal maximumRows As Integer, ByVal startRowIndex As Integer) As Data.DataTable

     

    The routine retuns a datatable to be bound to the gridview.

    With various breaks set in my code and running in debug here is what I see happening, and the various tests I have done:

     

    1 - When the user first hits the "SearchMode" the search fields are drawn and the execSearch returns nothing as no data was posted.

    2 - The user enters search data, and clicks find now.

    3 - The pagepostsback Still in SearchMode the Keywords field now has a string value, so execSearch fires, setting up the objectDatasource, and gridview and returns it.

    4 - SearchMode sees execSearch returned a gridview and runs the gridviews DataBind method.

    5 - The ByKeywords Method from the DAL fires. piAcct has a value, srchCriteria has a value (Both Dynamically add as parameters during execSearch()),

    SortCriteria is empty, maximumrows is an integer from the form indicating how many records per page, and startRowIndex is 0 as this has not been paged yet.

    6 - The query is built by the function and run against the Database. SortCriteria is checked for a value and SHOULD add an Order By clause if it has a value.

    7 - The datatable is returned to the objectdatasource, the objectdatasource is returned to the grid, and the grid is returned to the calling procedure.

    8 - As I mentioned the paging works, so I won't discuss it.

    9 - The user clicks a column header to sort.

    10 - The page posts back, The execSearch function runs again creating the initial gridview with search data and returning it.

    11 - Then the Sorting event fires, in the sorting event I grab the sortexpression and sortdirection and format it into a proper statement for sql.

    Then I get a reference to the objectdatasource by declaring the sender object as a gridviiew, then declaring a type variable as the sender.datasource

    Then I add the SortCriteria to the selectparameters.

    12 - I then call databind, which fires the ByKeywords method. A break at this point shows me that SortCriteria is still coming up empty.

     

    I have performed some testing at this phase. To see if what I was doing with the objectdatasource in the Sorting event was working, I added a made up

    select parameter "objsearch.selectParameters.Add("Test","Test")

     

    After doing that and calling databind, I got an Error that it could not find a "non-generic" method ByKeyword that had "Test" as a parameter.

    So I know what I do at the Sorting event to the objectdatasource is being received and used.

     

    So now, when I assign the selectparameter with the "SortCriteria" name, it doesn't give me an error. So to me the logic of this is that, yes

    my SortCriteria is being added to the SelectParameters, and yes, the objectdatasource recognizes it is the SortParameter. And yes it calls the ByKeywords

    method, but it passes an empty value in as the SortCriteria despite there being a parameter there.

     

    Why is the objectdatasource ignoring what I have explicitly placed into the parameters? How can I correct this?

    It is obvious to me from reading that the functionality is there and intended to do exactly what I am doing with it.

    I really don't want to have to create a DataView or anything in memory then sort that as it would cause me some

    design issues with how to get the Variables needed to execute the ByKeywords search.

     

    Some additional info if it matters: Visual Studio 2005, ASP.NET 2.0.50727

     

    Thanks,

     

    Steve

     

     

     

Page 1 of 1 (1 items)