[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why does sorting in my GridView fail?

Posted on 2008-06-18
3
Medium Priority
?
427 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 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