Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview in that textboxes and dropdown lists will generate dynamically.

If i have 10000 records, it is loading 10000 records. But i dont want that.
I need to get the records based on page index. If i was in page 1 it should get and load 1 to 50 records data and if i was page 3 it should get and load 100 to 150 like that.

How can i achieve this. Please suggest.

What I have tried:

This the grid view i defined and i set the property allo paging true.

<asp:GridView ID="gvDUSrqst" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound"
                GridLines="Vertical" BorderColor="White" AllowPaging = "true" PageSize="50">
                <HeaderStyle BackColor="#009530" CssClass="lblClsGridHdr" HorizontalAlign="Center" />
                <RowStyle Font-Names="Arial" Font-Size="8pt" ForeColor="#009530" />
            </asp:GridView>


This is my page index changed code.

Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles gvDUSrqst.PageIndexChanging
        gvDUSrqst.PageIndex = e.NewPageIndex
        btnSaveDUS_Click(btnSaveDUS, e)
        Me.BindGrid()
    End Sub


This is bindgrid method.

Private Sub BindGrid()
        Dim dt As New DataTable()
        Dim i As Integer = 0
        Dim fld As String = ""
        Sql = "SELECT FIELD,CASE WHEN FIELD='MATNR' THEN 'X' WHEN TASK_CODE='" & lblTsk.Text & "' THEN 'Y' ELSE 'Z' END FROM TABLE1 where ETCODE=" & "'" & lbletcode.Text & "'" & " AND TECODE=" & "'" & lbltecode.Text & "'" & "  ORDER BY 2,TASK_CODE,SEQ"
        Using OracleConnect As New OracleConnection(OraConString)
            OracleConnect.Open()
            dt = New DataTable()
            cmdOra = New OracleCommand(Sql, OracleConnect)
            rdrOra = cmdOra.ExecuteReader
            dt.Load(rdrOra)
            For i = 0 To dt.Rows.Count - 1
                If i = 0 Then
                    fld = dt.Rows(0).Item(0)
                Else
                    fld = fld & "," & dt.Rows(i).Item(0)
                End If
            Next
            dt.Dispose()
            Sql = "SELECT " & fld & " FROM TABLE2 WHERE WFCODE='" & lblWFCode.Text & "'"
            'Sql = "SELECT * FROM (SELECT " & fld & ", ROWNUM R FROM " & ViewState("TABLE_NAME") & " WHERE WFCODE='" & lblWFCode.Text & "') where r>" & ViewState("pageindex") & " and r<" & ViewState("pageindex") + 51 & ""
            dt = New DataTable
            cmdOra = New OracleCommand(Sql, OracleConnect)
            rdrOra = cmdOra.ExecuteReader
            dt.Load(rdrOra)
            If dt.Rows.Count > 0 Then
                gvDUSrqst.DataSource = dt
                gvDUSrqst.DataBind()
            End If
            OracleConnect.Close()
        End Using
    End Sub
Posted
Updated 11-May-18 0:28am

1 solution

VB
Sql = "SELECT FIELD,CASE WHEN FIELD='MATNR' THEN 'X' WHEN TASK_CODE='" & lblTsk.Text & "' THEN 'Y' ELSE 'Z' END FROM TABLE1 where ETCODE=" & "'" & lbletcode.Text & "'" & " AND TECODE=" & "'" & lbltecode.Text & "'" & "  ORDER BY 2,TASK_CODE,SEQ"

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
Sree_Ranga 11-May-18 11:11am    
Thanks for the information shared. I got the Solution achieve my solution.
I am using two buttons instead of paging. on button click event i am loading the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900