Solved

Why does sorting in my GridView fail?

Posted on 2008-06-18
3
409 Views
Last Modified: 2011-10-19
I wrote this simple little thing to search a database of purchased items by department and return some columns.  No sweat.  The user wants it to be sortable by the different columns, so I specified AllowSorting="true".  Then I set a SortExpression for each column.  Cool.  When I click on the column heading to perform the sort, however, it takes me to my "search returned no results" message.  It's as if it performs an entirely new search for something that it can't find.  Or maybe that's not it at all.  I'm lost.  Any ideas?  This is the last thing I have to get working on this before I turn it over to the user.

I attached a .zip file with code, a screen shot of a successful search, and a screen shot of a failed sort.  

Thanks!
<%@ Page Language="VB" %>
 
<script language="vb" runat="server">
    
    Sub searchHandler(ByVal sender As Object, ByVal e As EventArgs)
        If departmentList.SelectedItem.Value = "" Then
            lbl1.Text = "<br /><font color='red'>Please choose a department from the box above!</font>"
            GridView1.Visible = False
        Else
            'Need to build SQL statement with parameters.
            'In the meantime, I have made the database read-only.  This seems to work for security.
            AccessDataSource1.SelectCommand = "SELECT [ordered_date], [department], [item_purchased], [item#], [purchased_from], [amount], [taxed], [order#], [received_date] FROM [tblProcurement] WHERE [department] = '" + departmentList.SelectedItem.Value + "' ORDER BY [ordered_date]"
            If GridView1.RowHeaderColumn = "" Then
                lbl1.Text = "<br />Your search for " & departmentList.SelectedItem.Text & " found no results.<br /><a href='procurementSearch.aspx'>Perform a new search</a>"
                btnSearch.Enabled = False
            Else
                GridView1.Visible = True
                Dim instance As GridView = GridView1
                Dim sortExpression As String = "department"
                instance.Sort(sortExpression, WebControls.SortDirection.Ascending)
                'lbl1.Text = "<br />You searched for:<strong> " & departmentList.SelectedItem.Text & "</strong>.<br /><a href='procurementSearch.aspx'>Perform a new search</a>"
                lbl1.Text = "<br /><a href='procurementSearch.aspx'>Perform a new search.</a>"
                btnSearch.Enabled = False
            End If
        End If
    End Sub
 
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Frederick County Procurement Search</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <meta name="author" content="Jeremy Coulson" />
        <link rel="stylesheet" type="text/css" href="procurementStyles.css" />
</head>
 
<body>
    
    <form ID="frmProcurement" runat="server">
    <h2>Frederick County IT Procurement Search</h2>
    <div class="districtSearch">
        <ul>
            <li class="instructionLi">Enter your department to search for your purchases.</li>
            <li class="instructionLi">Click on the column heading to sort by that column.</li>
        </ul>
        <asp:Label ID="text1" runat="server">Department: </asp:Label>
        <asp:DropDownList ID="departmentList" runat="server" Width="14.5em" tabindex="1">
            <asp:ListItem Selected="True" Value="" Text="" />
            <asp:ListItem Value="Planning" Text="Planning" />
            <asp:ListItem Value="Inspections" Text="Inspections" />
            <asp:ListItem Value="Human Resources" Text="Human Resources" />
            <asp:ListItem Value="MIS" Text="MIS" />
        </asp:DropDownList><br />
        <%--<asp:TextBox ID="strSearch" runat="server" Width="14.5em" tabindex="1" /><br />--%>
        <asp:Button ID="btnSearch" runat="server" text="Search" onClick="searchHandler" tabindex="2" />&nbsp;&nbsp;&nbsp;
        <asp:Label ID="lbl1" runat="server" /><br />
        <br />
        
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" CellPadding="6" ForeColor="#333333" Visible="False" RowHeaderColumn="ordered_date" EmptyDataText="Your search returned no results." AllowSorting="true">
            <Columns>
                <asp:BoundField DataField="ordered_date" HeaderText="Date Ordered" SortExpression="ordered_date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" />
                <%--<asp:BoundField DataField="department" HeaderText="Department" SortExpression="department" />--%>
                <asp:BoundField DataField="item_purchased" HeaderText="Item Purchased" SortExpression="item_purchased" />
                <asp:BoundField DataField="item#" HeaderText="Item #" SortExpression="item#" />
                <asp:BoundField DataField="purchased_from" HeaderText="Purchased From" SortExpression="purchased_from" />
                <asp:BoundField DataField="amount" HeaderText="Amount" SortExpression="amount" />
                <asp:BoundField DataField="taxed" HeaderText="Taxed" SortExpression="taxed" />
                <asp:BoundField DataField="order#" HeaderText="Order #" SortExpression="order#" />
                <asp:BoundField DataField="received_date" HeaderText="Date Received" SortExpression="received_date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" />
            </Columns>
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#E3EAEB" />
            <EditRowStyle BackColor="#7C6F57" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataSourceMode="DataSet" DataFile="~/itDocs/procurement_search/data/procurement.mdb">
        </asp:AccessDataSource>     
    </div>
    
    </form>
    
    <br />
    <center><input type=button value="Close Window" onClick="javascript:window.close();" /></center>
 
    <hr id="districtsHr" />
    
    <p id="pageText">Call IT at 665-5614 or email Nina at <a href="mailto:ndunleav@co.frederick.va.us">ndunleav@co.frederick.va.us</a> if you have questions or find a discrepancy.</p><br />
 
</body>
</html>

Open in new window

procurementSearch.zip
0
Comment
Question by:mrcoulson
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21821687
Hmmm...what action starts a new search?  Does it happen when the page posts back?
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21822318
>>>>however, it takes me to my "search returned no results" message.
Do you still see the GridView?
0
 

Accepted Solution

by:
mrcoulson earned 0 total points
ID: 22205752
I got this working.  My friend gave me some code that fixed it.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            Me.EnableViewState = True
        End If
    End Sub
 
    Protected Sub searchHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        GridView1.Visible = True
        If departmentList.SelectedItem.Value = "" Then
            lbl1.Text = "<br /><font color='red'>Please choose a department from the box above!</font>"
            GridView1.Visible = False
        Else
            'Need to build SQL statement with parameters.
            'In the meantime, I have made the database read-only.  This seems to work for security.
            Dim strSelectCommand As String = "SELECT [ordered_date], [department], [item_purchased], [item#], [purchased_from], [amount], [purchase_order#], [order#], [received_date] FROM [tblProcurement] WHERE [department] = '" + departmentList.SelectedItem.Value + "'"
            ViewState("selectCommend") = strSelectCommand
            strSelectCommand = strSelectCommand + "  ORDER BY [ordered_date] DESC"
            AccessDataSource1.SelectCommand = strSelectCommand
            GridView1.DataBind()
            btnExport.Enabled = True
            
            If GridView1.RowHeaderColumn = "" Then
                lbl1.Text = "<br />Your search for " & departmentList.SelectedItem.Text & " found no results.<br /><a href='procurementSearch.aspx'>Perform a new search</a>"
                btnSearch.Enabled = False
                GridView1.Visible = True
                
                Dim sortExpression As String = "department"
                GridView1.Sort(sortExpression, WebControls.SortDirection.Ascending)
                'lbl1.Text = "<br />You searched for:<strong> " & departmentList.SelectedItem.Text & "</strong>.<br /><a href='procurementSearch.aspx'>Perform a new search</a>"
                lbl1.Text = "<br /><a href='procurementSearch.aspx'>Perform a new search.</a>"
                btnSearch.Enabled = False
            Else
 
            End If
        End If
    End Sub
 
    'Endless thanks to the South African .Net Master and my very great friend, Werner van der Vyver.
    Protected Sub Gridview_Sort(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
        Dim strSelectCommand As String = ViewState("selectCommend")
        strSelectCommand = strSelectCommand + " ORDER BY [" + e.SortExpression + "]"
        AccessDataSource1.SelectCommand = strSelectCommand
        GridView1.DataBind()
    End Sub
 
    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=procurement_" + Today + ".xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.xls"
 
        Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
        Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
 
        '// Dim gv As GridView = GetGridView() '// a function to generate Gridview
 
        GridView1.RenderControl(htmlWrite)
        Response.Write(stringWrite.ToString)
        Response.End()
 
    End Sub
 
    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Confirms that an HtmlForm control is rendered for the specified ASP.NET
        ' server control at run time.
    End Sub

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

791 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