Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView Sorting Problem (Repost) 2

Status
Not open for further replies.

DotNetDunce

Programmer
Aug 23, 2001
47
0
0
US
jbenson001, please accept my apology. I had a major brain fart and I guess my last post in my previous thread caused it to where I could not repost in it. I do appreciate your help and if you will overlook my stupidity. I'm placing the correct code here in this thread.

Thanks again for your help,

Melinda

Code:
<%@ Page Language="VB" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="Search.aspx.vb" Inherits="Search2" title="Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <hr style="width: 782px" />
    &nbsp;<asp:TextBox ID="txtSearch" runat="server" Font-Names="Arial"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" Font-Names="Arial" />
    <asp:Label ID="lblUserType" runat="server" Visible="False" Font-Names="Arial"></asp:Label><br />
    <br />
    <asp:Label ID="lblNoRecs" runat="server" Font-Bold="True" ForeColor="Red" Text="No Records Found"
        Visible="False" Font-Names="Arial"></asp:Label><br />
        
        
    <asp:GridView ID="GridView1" 
    runat="server" 
    AutoGenerateColumns="false" 
    AllowSorting="true" 
    AllowPaging="true" 
    OnSorting="GridView1_Sorting" 
    EnableViewState="true" Font-Names="Arial">
        <HeaderStyle BackColor="Blue" ForeColor="White" />
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="ticketno" HeaderText="Ticket Number" InsertVisible="False"
                SortExpression="ticketno" DataNavigateUrlFormatString="detail.aspx?ticketno={0}" DataTextField="ticketno">
                <ItemStyle HorizontalAlign="Center" />
            </asp:HyperLinkField>
            <asp:BoundField DataField="lname" HeaderText="Last Name" SortExpression="lname">
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="company" HeaderText="Company" SortExpression="company" >
                <HeaderStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="product" HeaderText="Product" SortExpression="product" >
                <HeaderStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="date_entered" HeaderText="Date Entered" SortExpression="date_entered" DataFormatString="{0:d}" HtmlEncode="False" >
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="assigned" HeaderText="Assigned To" SortExpression="assigned" >
                <HeaderStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" >
                <HeaderStyle HorizontalAlign="Center" />
            </asp:BoundField>
        </Columns>
    </asp:GridView>
</asp:Content>
 
When you say the sort is firing twice, do you mean the sub GridView1_Sorting() is being called twice? I can't see in your code why that would happen. Have you debugged and traced through your code?
 
I have debugged and traced through the code. It executes the sorting event twice. I can't find the reason for it either. :( I'm still green when it comes to ASP.NET so I was hoping someone could spot something obvious or help me figure out a work-around. Thanks again for taking a look.
 
At what point in your code does the sorting code get called for the second time?
 
It executes the sorting code when I click on the column header and immediately executes again when it gets to the end of the sub.
 
Code behind for search.aspx.vb.

Thanks,

Melinda

Code:
Imports System.Data
Imports System.Data.OleDb
Partial Class Search2
    Inherits System.Web.UI.Page
    Private connection As OleDbConnection
    Private connectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("bugsConnectionString").ToString
    Private DBView As DataView

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim DBdataset As DataSet
        Dim DBAdapter As OleDbDataAdapter

        connection = New OleDbConnection(connectionString)

        ' Create a OleDbDataAdapter for the table.
        DBAdapter = New OleDbDataAdapter()


        ' A table mapping names the DataTable.
        DBAdapter.TableMappings.Add("Table", "Tickets")

        ' Open the connection.
        connection.Open()

        ' Create a OleDbCommand to retrieve Tickets data.
        Dim DBSelectCommand As OleDbCommand
        Dim sStat As String
        sStat = "New"

        If IsNumeric(txtSearch.Text) Then
            If Session("UserType") = "Admin" Then
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [ticketno] = " & txtSearch.Text, connection)
            Else
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND [ticketno] = " & txtSearch.Text, connection)
            End If
        ElseIf Not IsNumeric(txtSearch.Text) And txtSearch.Text <> "" Then
            If Session("UserType") = "Admin" Then
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%'", connection)
            Else
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND ([lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%')", connection)
            End If
        Else
            If Session("UserType") = "Admin" Then
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets", connection)
            Else
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets WHERE status <> '" & sStat & "'", connection)
            End If
        End If

        DBSelectCommand.CommandType = CommandType.Text

        ' Set the OleDbDataAdapter's SelectCommand.
        DBAdapter.SelectCommand = DBSelectCommand

        ' Fill the DataSet.
        DBdataset = New DataSet("Tickets")

        DBAdapter.Fill(DBdataset)

        connection.Close()

        Dim Dbdatatable As DataTable
        Dbdatatable = DBdataset.Tables(0)

        DBView = Dbdatatable.DefaultView

        Me.GridView1.DataSource = DBdataset
        Me.GridView1.DataBind()


        If GridView1.Rows.Count <> 0 Then
            Me.lblNoRecs.Visible = "False"

        Else
            Me.lblNoRecs.Visible = "True"
        End If


    End Sub

    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        Dim DBdataset As DataSet
        Dim DBAdapter As OleDbDataAdapter
        connection = New OleDbConnection(connectionString)

        ' Create a OleDbDataAdapter for the table.
        DBAdapter = New OleDbDataAdapter()


        ' A table mapping names the DataTable.
        DBAdapter.TableMappings.Add("Table", "Tickets")

        ' Open the connection.
        connection.Open()

        ' Create a OleDbCommand to retrieve Tickets data.
        Dim sStat As String
        sStat = "New"
        Dim DBSelectCommand As OleDbCommand
        If IsNumeric(txtSearch.Text) Then
            If Session("UserType") = "Admin" Then
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [ticketno] = " & txtSearch.Text, connection)
            Else
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND [ticketno] = " & txtSearch.Text, connection)
            End If
        ElseIf Not IsNumeric(txtSearch.Text) And txtSearch.Text <> "" Then
            If Session("UserType") = "Admin" Then
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%'", connection)
            Else
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND ([lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%')", connection)
            End If
        Else
            If Session("UserType") = "Admin" Then
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets", connection)
            Else
                DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets WHERE status <> '" & sStat & "'", connection)
            End If
        End If

        DBSelectCommand.CommandType = CommandType.Text

        ' Set the OleDbDataAdapter's SelectCommand.
        DBAdapter.SelectCommand = DBSelectCommand

        ' Fill the DataSet.
        DBdataset = New DataSet("Tickets")

        DBAdapter.Fill(DBdataset)

        connection.Close()

        Dim Dbdatatable As DataTable
        Dbdatatable = DBdataset.Tables(0)

        DBView = Dbdatatable.DefaultView

        Dim sDirection As String
        If ViewState("sortDirection") Is Nothing Then
            ViewState("sortDirection") = SortDirection.Ascending
            sDirection = " ASC"
        ElseIf ViewState("sortDirection") = SortDirection.Ascending Then
            ViewState("sortDirection") = SortDirection.Descending
            sDirection = " DESC"
        ElseIf ViewState("sortDirection") = SortDirection.Descending Then
            ViewState("sortDirection") = SortDirection.Ascending
            sDirection = " ASC"
        Else
            ViewState("sortDirection") = SortDirection.Ascending
            sDirection = " ASC"
        End If

        DBView.Sort = e.SortExpression & sDirection
        Me.GridView1.DataSource = DBView
        Me.GridView1.DataBind()
    End Sub

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If Session("LoggedIn") = False Then
            Session("addy") = "search.aspx"
            Response.Redirect("login.aspx")
        End If
    End Sub
End Class
 
Code:
    <asp:GridView ID="GridView1"
    runat="server"
    AutoGenerateColumns="false"
    AllowSorting="true"
    AllowPaging="true"
    [!]OnSorting="GridView1_Sorting"[/!]
Code:
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) [!]Handles GridView1.Sorting[/!]
You have told your page to call the event twice.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
I've looked at this thing for so long I just didn't see it. Sometimes it takes a fresh pair of eyes to catch things. Thank you so much! I've never really posted anything to the forum because I'm too dumb, but I don't know what I'd without you guys! Works perfectly! Thanks a bunch!!

Melinda
 
You're not dumb, you're trying and learning; there is a difference between the two :)
 
This is a very small project and I am using Access for now. Eventually it will go over to SQL Server.
 
Good catch Mark!!.. I guess I breezed over that too quickly

Keep posting .. you will learn.. it's not being dumb.. it just a learning curve..
 
Thanks, jbenson001. I've been a member of this forum since 2001 and it seems like I've always been the one learning from scratch. I started out in ColdFusion, then went to ASP and some .NET, then to C++ and now I'm back to .NET and I've never got to stick with one long enough to become good at it. I appreciate all yours and everyones help and hopefully one day I'll be able to help others too.

Have a good one!
 
OK, the reason I asked is that whilst you are still learning it may be better if you jump straight to SQL Server (there's a free edition here). Access can cause problems in a multi-user environment whereas you simply won't get these problems with SQL Server. You will also be able to take advantage of stored procedures and you could get away with using the one stored procedure instead of having to check which SQL statement to run like in your above example (it will also protect you against SQL Injection at the same time).


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top