Solved

Why does sorting in my GridView fail?

Posted on 2008-06-18
3
401 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now