?
Solved

Why does sorting in my GridView fail?

Posted on 2008-06-18
3
Medium Priority
?
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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