Hi Bhavesh23,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<div class="form-group mx-auto">
<table border="0" align="center" class="table">
<tr>
<td>Id:<asp:Label ID="lblId" Width="250px" runat="server" Font-Names="Cambria"
Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
</tr>
<tr>
<td>First Name:<asp:Label ID="lblFirst" Width="250px" runat="server"
Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
</tr>
<tr>
<td>Last Name:<asp:Label ID="lblLast" Width="250px" runat="server"
Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
</tr>
<tr>
<td>City:<asp:Label ID="lblCity" Width="250px" runat="server"
Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
</tr>
<tr>
<td>Country:<asp:Label ID="lblCountry" Width="250px" runat="server"
Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td class="text-center">
<asp:Button ID="btnNext" CssClass="btn btn-danger" runat="server"
Text="Next" OnClick="btnNext_Click" />
<asp:Button ID="btnPrevious" CssClass="btn btn-danger" runat="server"
Text="Previous" OnClick="btnPrevious_Click" />
</td>
</tr>
</table>
</div>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
public int RowNumber
{
get { return Session["Row"] == null ? 1 : Convert.ToInt32(Session["Row"]); }
set { Session["Row"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData(this.RowNumber);
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
this.RowNumber++;
GetData(this.RowNumber);
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
this.RowNumber--;
GetData(this.RowNumber);
}
private void GetData(int rowNo)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "";
query += "SELECT * FROM (";
query += " SELECT ROW_NUMBER() OVER(ORDER BY EmployeeID) AS 'RowNo',";
query += " EmployeeID,FirstName,LastName,City,Country,(SELECT COUNT(*) FROM Employees) AS 'Total'";
query += " FROM Employees";
query += ") t ";
query += "WHERE t.RowNo = @Row";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Row", rowNo);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
lblId.Text = sdr["EmployeeId"].ToString();
lblFirst.Text = sdr["FirstName"].ToString();
lblLast.Text = sdr["LastName"].ToString();
lblCity.Text = sdr["City"].ToString();
lblCountry.Text = sdr["Country"].ToString();
btnPrevious.Enabled = rowNo <= 1 ? false : true;
btnNext.Enabled = Convert.ToInt32(sdr["Total"]) == rowNo ? false : true;
}
con.Close();
}
}
VB.Net
Public Property RowNumber As Integer
Get
Return If(Session("Row") Is Nothing, 1, Convert.ToInt32(Session("Row")))
End Get
Set(ByVal value As Integer)
Session("Row") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GetData(Me.RowNumber)
End If
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.RowNumber += 1
GetData(Me.RowNumber)
End Sub
Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.RowNumber -= 1
GetData(Me.RowNumber)
End Sub
Private Sub GetData(ByVal rowNo As Integer)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = ""
query += "SELECT * FROM ("
query += " SELECT ROW_NUMBER() OVER(ORDER BY EmployeeID) AS 'RowNo',"
query += " EmployeeID,FirstName,LastName,City,Country,(SELECT COUNT(*) FROM Employees) AS 'Total'"
query += " FROM Employees"
query += ") t "
query += "WHERE t.RowNo = @Row"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Row", rowNo)
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
lblId.Text = sdr("EmployeeId").ToString()
lblFirst.Text = sdr("FirstName").ToString()
lblLast.Text = sdr("LastName").ToString()
lblCity.Text = sdr("City").ToString()
lblCountry.Text = sdr("Country").ToString()
btnPrevious.Enabled = If(rowNo <= 1, False, True)
btnNext.Enabled = If(Convert.ToInt32(sdr("Total")) = rowNo, False, True)
End If
con.Close()
End Using
End Sub
Screenshot