locked
Why Paging and Sorting did not functional on IIS? RRS feed

  • Question

  • User-647380262 posted

    Hai . 

    I'm doing mini project using Visual Studio (.aspx ), using VB language and SQL Server as Database. I'm using the gridview function to show the data by using SQL Data Source. I'm planning to do Paging and Sorting on the gridview, but when I view the webpage on Internet Explorer, the Sorting and Paging appear but do not functional. Why is this happens?

    Below is the sample code that I use:

    <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="False" DataSourceID="dsDetails" AllowPaging="True"
    Width="540px" CssClass="Gridview" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" CellSpacing="2" FreezeHeader="True" AllowSorting="True">

    Thursday, January 19, 2017 4:44 AM

All replies

  • User-1366948256 posted

    Hello,

    You have not wired up paging index changed event. See the following link to have the example. I think you should also post your code for VB.NET so that we have better idea.

    http://www.c-sharpcorner.com/UploadFile/rohatash/gridview-paging-sample-in-Asp-Net/

      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView1.PageIndex = e.NewPageIndex;
    BindData();// A function to bind grid. }

    Thursday, January 19, 2017 6:14 AM
  • User-647380262 posted

    Hi. 

    So, this is the code I put on (.aspx.vb) . I just put the paging function code only.

    Private Sub BindData()
            Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT ID as [ID], firstName as [First Name], lastName as [Last Name], email as [Email], NRIC as [NRIC],Nationality as [nationality], passportNo as [Passport No], street as [Street], city as [City], stateProvince as [State/Province], postalCode as [Postal Code], countryRegion as [Country /Region],bPhone as  [Business Phone], hPhone as [House Phone], mPhone as [Mobile Phone], faxNo as [Fax No], jobTitle as [Job Title],yearsInBusiness as [No of Years In Business],yearsInIndustry as [ No Years In Industry], HNI as [HNI], HNIRemarks as [HNI Remarks], webPage as [Web Page], notes as [Notes] FROM [employee] ")
                    Using sda As New SqlDataAdapter()
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            GV1.DataSource = dt
                            GV1.DataBind()
                        End Using
                    End Using
                End Using
            End Using
        End Sub
    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, e As GridViewPageEventArgs) GV1.PageIndex = e.NewPageIndex BindData() End Sub

    But still , the paging function cannot functional

    When I add this code 

        Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Me.BindData()
            End If
        End Sub
    

    It shows this error

    -- Server Error in '/' Application.

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. --

     

    Thursday, January 19, 2017 6:24 AM
  • User-2057865890 posted

    Hi Amymir,

    In order to implement Paging in GridView, AllowPaging property is set to true and OnPageIndexChanging event has been handled.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
        OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10">

    Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Object reference not set to an instance of an object.

    Do you have name "ConnectionString" in the Web.config file?

    The following example is for a SQL Server database using SQL Server security (log on to the server by using user credentials in the connection string).

    <add name="ConnectionString"
        providerName="System.Data.SqlClient"
        connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;
    UserId=userid;Password=password;MultipleActiveResultSets=True" />

    Best Regards,

    Chris

    Friday, January 20, 2017 4:56 AM
  • User-647380262 posted

    Hi Chris. 

    Thank you for the answer. Finally the error has been corrected. But when I try to view the page , it cannot be viewed due to another error

    Private Sub BindData()
            Dim constr As String = ConfigurationManager.ConnectionStrings("CRMConnectionString").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT ID as [ID], firstName as [First Name], lastName as [Last Name], email as [Email], NRIC as [NRIC],Nationality as [nationality], passportNo as [Passport No], street as [Street], city as [City], stateProvince as [State/Province], postalCode as [Postal Code], countryRegion as [Country /Region],bPhone as  [Business Phone], hPhone as [House Phone], mPhone as [Mobile Phone], faxNo as [Fax No], jobTitle as [Job Title],yearsInBusiness as [No of Years In Business],yearsInIndustry as [ No Years In Industry], HNI as [HNI], HNIRemarks as [HNI Remarks], webPage as [Web Page], notes as [Notes] FROM [employee] ")
                    Using sda As New SqlDataAdapter()
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            GV1.DataSource = dt
                            GV1.DataBind()   <------- This code turn into red 
                        End Using
                    End Using
                End Using
            End Using
        End Sub




    The Error : 

     Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'GV1'.  Remove one definition.

    So , I delete the 

     GV1.DataBind()   <------- This code turn into red 

    It gives me another error.

    Both DataSource and DataSourceID are defined on 'GV1'.  Remove one definition.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'GV1'.  Remove one definition.

    So , what should I do ?

    Friday, January 20, 2017 7:16 AM
  • User-2057865890 posted

    Hi Amymir,

    <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="False" DataSourceID="dsDetails" AllowPaging="True"
    Width="540px" CssClass="Gridview" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" CellSpacing="2" FreezeHeader="True" AllowSorting="True">

    You have set both the DataSourceID and DataSource properties on GridView. Remove one of them.

    Remove DataSourceID

    <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="False" AllowPaging="True" 
    Width="540px" CssClass="Gridview" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" 
    BorderWidth="3px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" CellSpacing="2"
    FreezeHeader="True" AllowSorting="True">

    Best Regards,

    Chris

    Monday, January 23, 2017 2:03 AM
  • User-647380262 posted

    Hi Chris,

    I already removed the DataSourceID="dsDetails".

    Below is the code on .aspx

    <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="False"  AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" OnPageIndexChanged="Page_Load"
     Width="540px" CssClass="Gridview" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" CellSpacing="2" FreezeHeader="True">

    While below is the code on .aspx.vb

     Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Me.BindGrid()
            End If
        End Sub
    
        Private Sub BindGrid()
            Dim constr As String = ConfigurationManager.ConnectionStrings("CRMConnectionString").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT ID as [ID], firstName as [First Name], lastName as [Last Name], email as [Email], NRIC as [NRIC],Nationality as [Nationality], passportNo as [Passport No], street as [Street], city as [City], stateProvince as [State/Province], postalCode as [Postal Code], countryRegion as [Country /Region],bPhone as  [Business Phone], hPhone as [House Phone], mPhone as [Mobile Phone], faxNo as [Fax No], jobTitle as [Job Title],yearsInBusiness as [No of Years In Business],yearsInIndustry as [ No Years In Industry], HNI as [HNI], HNIRemarks as [HNI Remarks], webPage as [Web Page], notes as [Notes] FROM [employee]")
                    Using sda As New SqlDataAdapter()
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            GV1.DataSource = dt
                            GV1.DataBind()
                        End Using
                    End Using
                End Using
            End Using
        End Sub
    
    
        Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
            GV1.PageIndex = e.NewPageIndex
            Me.BindGrid()
        End Sub
    

    When I try to view the webpage on Internet Explorer(browser), the paging function can be viewed , but the paging button still cannot function/not clickable. So, based on the code above , is there any code that I forget to put on ?

    Monday, January 23, 2017 2:09 AM
  • User-2057865890 posted

    Hi Amymir,

    <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="False" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" OnPageIndexChanged="Page_Load" Width="540px" CssClass="Gridview" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" CellSpacing="2" FreezeHeader="True">

    Remove 

    OnPageIndexChanged="Page_Load"

    You could refer http://www.aspsnippets.com/Articles/Paging-in-ASPNet-GridView-Example.aspx.

    Best Regards,

    Chris

    Monday, January 23, 2017 3:20 AM
  • User-647380262 posted

    Hi Chris.

    Before I asked on this forum , I already refer to the link that you gave . But the paging still not functional. I don't know why or where the error in my code. All the example that I found through google still can't functional. Is there any different way to make paging functional?

    Thank you . 

    Monday, January 23, 2017 7:11 AM
  • User-2057865890 posted

    Hi Amymir,

    Please refer the following sample.

    <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="False"
           AllowPaging="True" Width="540px" CssClass="Gridview"
           BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"
           BorderWidth="3px" CellPadding="4" DataKeyNames="ID"
           ForeColor="Black" CellSpacing="2" FreezeHeader="True"
           AllowSorting="True"
           OnPageIndexChanging="OnPageIndexChanging" PageSize="5">
           <Columns>
              <asp:BoundField ItemStyle-Width="150px" DataField="Id" HeaderText="Id" />
              <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
              <asp:BoundField ItemStyle-Width="150px" DataField="Country" HeaderText="Country" />
           </Columns>
    </asp:GridView>

    Code behind

        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Me.BindGrid()
            End If
        End Sub
    
        Private Sub BindGrid()
            Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)),
                                                   New DataColumn("Name", GetType(String)),
                                                   New DataColumn("Country", GetType(String))})
            dt.Rows.Add(1, "John Hammond", "United States")
            dt.Rows.Add(2, "Mudassar Khan", "India")
            dt.Rows.Add(3, "Suzanne Mathews", "France")
            dt.Rows.Add(4, "Robert Schidner", "Russia")
            dt.Rows.Add(5, "Maria Anders", "Germany")
            dt.Rows.Add(6, "Thomas Hardy", "UK")
            dt.Rows.Add(7, "Christina Berglund", "Sweden")
            dt.Rows.Add(8, "Laurence Lebihan", "France")
            dt.Rows.Add(9, "Elizabeth Lincoln", "Canada")
            GV1.DataSource = dt
            GV1.DataBind()
        End Sub
    
        Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
            GV1.PageIndex = e.NewPageIndex
            Me.BindGrid()
        End Sub
    
    

    Best Regards,

    Chris

    Tuesday, February 7, 2017 7:05 AM