3
Reply

Retrieve Next Record from db in WebApplications

SAIDI Reddy Sirigiri

SAIDI Reddy Sirigiri

Nov 25 2010 6:47 AM
12.3k
my Query is i want to retrieve next record from db in web application using sessions or viewstate .when am click on next button always that shows first record.
 am sending code please check it once.

Default.aspx Page:-
<asp:Label ID="lblUserId" runat="server" ></asp:Label><br />
    <asp:Button ID="btnShowData" runat="server" Text="Show Data"
            onclick="btnShowData_Click" />&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
    <asp:Button ID="btnNext" runat="server" Text="Next" onclick="btnNext_Click1" />



My Connection string from Web.config File:-
string conn = ConfigurationManager.ConnectionStrings["SqlConn1"].ToString();
  

In PageLoad:-

  DataTable dt = getData();
        lblUserId.Text = dt.Rows[0]["UserId"].ToString();
        int count = 0;
        Session["dt"] = dt;
        Session["count"] = count;

Method:-

 public DataTable getData()
    {
        SqlConnection con = new SqlConnection(conn);
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from registration", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        return dt;
    }

Next ButtonClick:-
 protected void btnNext_Click1(object sender, EventArgs e)
    {
        if (Session["dt"] != null)
        {
            DataTable dt = (DataTable)Session["dt"];
            int count = (int)Session["count"];
            count = count + 1;
            lblUserId.Text = dt.Rows[count]["UserId"].ToString();

        }
    }

Answers (3)