how to move next and previous record on label in web application

Bhavesh23
 
on Jul 02, 2021 06:56 AM
Sample_130153.zip
420 Views

here i have one more query.

how to move next and previous record which is displayed on label 

i have 6 label with named Name, Address, City, Contactno.

i want to show next record by pressing next button.

all records display on label from database.

my html code...

                 <div class="form-group mx-auto">
                     <table border="0" align="center">
                         <tr>
                             <td><asp:Label ID="Label1" Width="250px" runat="server" Text="Label" Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
                         </tr>
                         <tr>
                             <td><asp:Label ID="Label2" Width="250px" runat="server" Text="Label" Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
                         </tr>
                         <tr>
                             <td><asp:Label ID="Label3" Width="250px" runat="server" Text="Label" Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
                         </tr>
                         <tr>
                             <td><asp:Label ID="Label4" Width="250px" runat="server" Text="Label" Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
                         </tr>
                         <tr>
                             <td><asp:Label ID="Label5" Width="250px" runat="server" Text="Label" Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
                         </tr>
                         <tr>
                             <td><asp:Label ID="Label6" Width="250px" runat="server" Text="Label" Font-Names="Cambria" Font-Size="15pt" ForeColor="#0066FF"></asp:Label></td>
                         </tr>
                         <tr>
                             <td>&nbsp;</td>
                         </tr>
                         <tr>
                             <td class="text-center">
                                 <asp:Button ID="btnNext" CssClass="btn btn-danger" runat="server" Text="Next" OnClick="btnNext_Click" /> &nbsp;
                                 <asp:Button ID="btnPrevious" CssClass="btn btn-danger" runat="server" Text="Previous" />
                             </td>
                         </tr>
                     </table>
                 </div>

my c# code...

            SqlConnection con = new SqlConnection(this.con);
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter ("Select NAME,ADDRESS,CITYORVILLAGE,TALUKA,DISTRICT,CONTACTNO FROM [RESTICTIED_PERSONS]", con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            if (j <= ds.Tables[0].Rows.Count - 1)
            {
                txtName.Text = ds.Tables[0].Rows[j]["NAME"].ToString();
                txtAddress.Text = ds.Tables[0].Rows[j]["ADDRESS"].ToString(); 
                txtCityorvillage.Text = ds.Tables[0].Rows[j]["CITYORVILLAGE"].ToString();
                txtTaluka.Text = ds.Tables[0].Rows[j]["TALUKA"].ToString();
                txtDistrict.Text = ds.Tables[0].Rows[j]["DISTRICT"].ToString();
                txtContactno.Text = ds.Tables[0].Rows[j]["CONTACTNO"].ToString();
            }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 02, 2021 07:00 AM
Bhavesh23
 
on Jul 02, 2021 11:26 PM

Both example not suitable for my project sir.

Actually when i press next button than next name should be display on label.

If i press previous button, previous name should be display on same label using code behind.

Not page should be navigate.

 

dharmendr
 
on Jul 03, 2021 04:48 AM

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>&nbsp;</td>
        </tr>
        <tr>
            <td class="text-center">
                <asp:Button ID="btnNext" CssClass="btn btn-danger" runat="server"
                    Text="Next" OnClick="btnNext_Click" />&nbsp;
                <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