locked
how can i make next and previous row buttons work ? RRS feed

  • Question

  • In i have connected SQL 2008DB with asp.net 2010 web application using c# and  I can not make next and previous buttons work, 
    and i want to show selected row number in label1 , 
    only last and first buttons are working - Help me please
    and i write these codes in defualt.aspx.cs 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data;
    using System.IO;

    namespace WebApplication1
    {

        public partial class _Default : System.Web.UI.Page
        {
            int i = 0;
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            
            protected void Page_Load(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection(@"Data Source=HARB\SQLEXPRESS;Initial Catalog=daily_log_golf;Integrated Security=True");
                conn.Open();
                SqlDataAdapter da =new SqlDataAdapter  ("select * from school", conn);
                da.Fill(ds, "school");
                dt = ds.Tables["school"];         
             }
            public void Display()
        {
            DataRow dr = dt.Rows[i];
            TextBox1.Text = dr["id"].ToString();
            TextBox2.Text = dr["name"].ToString();
            TextBox3.Text = dr["email"].ToString();
            TextBox4.Text = dr["town"].ToString();
         }
          protected void firstbtn_Click(object sender, EventArgs e)
                          {
                            i = 0;
                            Display();
                           }

           protected void nextbtn_Click(object sender, EventArgs e)
                    {
                  if (i < ds.Tables[0].Rows.Count - 1)
                        {
                            i++;
                            Display();
                        }
                        else
                        {
                        }
                        }

                    protected void previousbtn_Click(object sender, EventArgs e)
                    {
                        i++;
                        if (i >= 0)
                        {
                           Display();
                        }
                    }

                    protected void lastbtn_Click(object sender , EventArgs e)
                    {
                     i = dt.Rows.Count - 1;
                     Display();
                    }

                    protected void showrowbtn_Click(object sender, EventArgs e)
                    {
      //to show selected row number in label 1
               DataRow row = dt.NewRow();
                dt.Rows.Add(row);
               // get the index now
               Label1.Text = (dt.Rows.IndexOf(row)).ToString();
                    }

                       }
    }
    • Moved by CoolDadTx Thursday, September 17, 2015 5:28 PM Wrong forums
    Thursday, September 17, 2015 12:46 PM

Answers

All replies

  • Hi,

    Since a web application is running in a disconnected environment, the value of the variable 'i' will be initialized every time and always set to '0' during postbacks i.e, when you submit the page  by clicking the buttons. You need to store the variable  in ViewState or Session to maintain the value between postbacks. Please change your code as below :

          //int i = 0;
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            private int i
            {
                get
                {
                    object recNum = ViewState["i"];
                    if (recNum != null)
                        return (int)recNum;
                    else
                        return 0;
                }
                set { ViewState["i"] = value; }
            }

    Also you need to decrease the value of 'i' in 'previousbtn_Click' event. Change it as below:

    protected void previousbtn_Click(object sender, EventArgs e)
            {
                i--;
                if (i >= 0)
                {
                    Display();
                }
            }

    Please post asp.net web forms related questions in http://forums.asp.net/18.aspx


    Microsoft MVP - ASP/ASP.NET

    Thursday, September 17, 2015 1:34 PM
  • Please post questions related to ASP.NET in the ASP.NET forums (http://forums.asp.net ).
    Thursday, September 17, 2015 5:28 PM