简体   繁体   中英

How to move next/previous record from SQL Server database table in asp.net c#

I am a beginner and trying to display next/previous records from a SQL Server database table in asp.net with c#. But it doesn't work...

When I press the next button, only one time it works, the next time I press it's not working. I tried to find where the problem is and I found that the variable rowIndex is not incrementing after one time I press the next button.

And don't know whether the previous button works or not since the next button works only one time, the previous button also works once.

I don't know how to solve this since I am a beginner, so please help me :)

This is the Home.aspx.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


public partial class Home : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataTable dt = new DataTable();
    int rowIndex;

    protected void Page_Load(object sender, EventArgs e)
    {       
        con = new SqlConnection("Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = DashingGoal; Integrated Security = True; Connect Timeout = 30; Encrypt = False; 
                   TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False");
        con.Open();

        cmd = new SqlCommand("select * from content", con);
        da = new SqlDataAdapter(cmd);
        da.Fill(dt);

        rowIndex = 0;
        Label2.Text = dt.Rows.Count.ToString();

        if (!Page.IsPostBack)
        {
            if (dt.Rows.Count > 0)
            {
                // Populate the TextBox with the first entry on page load
                Label1.Text = dt.Rows[0]["heading"].ToString();
                //Then we store the DataTable in Session so that we will NOT
                //query the DB on every postbacks
                Session["dt"] = dt;
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Session["dt"] != null)
        {
            dt = (DataTable)Session["dt"];

            if (rowIndex < dt.Rows.Count)
            {
                rowIndex++;
                Label3.Text = rowIndex.ToString();
                //get the next row entry on Button Click by setting the Row Index
                Label1.Text = dt.Rows[rowIndex]["heading"].ToString();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (rowIndex == ds.Tables[0].Rows.Count - 1 || rowIndex != 0)
        {
            rowIndex--;
            Label1.Text = ds.Tables[0].Rows[rowIndex]["heading"].ToString();
        }
    }
}

And this is the markup for the Home.aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #form1 {
            height: 809px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Font-Bold="False" Font-Size="XX-Large" OnClick="Button1_Click" style="z-index: 1; left: 51px; top: 261px; position: absolute; width: 57px; height: 41px" Text="&lt;" />
        <asp:Button ID="Button2" runat="server" Font-Size="XX-Large" OnClick="Button2_Click" style="z-index: 1; left: 860px; top: 262px; position: absolute; height: 41px; width: 57px" Text="&gt;" />
        <asp:Label ID="Label1" runat="server" style="z-index: 1; left: 111px; top: 444px; position: absolute; height: 61px; width: 744px"></asp:Label>            
        <asp:Label ID="Label2" runat="server" style="z-index: 1; left: 91px; top: 34px; position: absolute; height: 27px; width: 68px" Text="Label"></asp:Label>
        <asp:Label ID="Label3" runat="server" style="z-index: 1; left: 225px; top: 27px; position: absolute; height: 22px; width: 51px" Text="Label"></asp:Label>
    </form>
</body>
</html>

Yes and it's not supposed to work as well that way. check your code again (as below). if (rowIndex == ds.Tables[0].Rows.Count - 1 which rowindex you are going to get here? it's totally wrong; On button click event you don't get the rowindex. You should rather use a GridView control with Paging enabled and set the records in grid to be 1 or whichever number you want.

protected void Button1_Click(object sender, EventArgs e)
{
    if (rowIndex == ds.Tables[0].Rows.Count - 1 || rowIndex != 0)
    {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM