Hi folks,
I have an ASP page.
I have a label and button on that page.
label shows data from database.
lets say i have 100 rows in database

Now when I run my page i want to see first row data on my page label...
and when user click next button I want to see next record (next row) in the database on that label

So in short next button should bring next row from database...

how can i achieve this????

>how can i achieve this????

Think about HttpSession.

Hi,

The following is one method of do the above. but there are much more better methods of achieving the same

Place one Label, Button and a HiddenField in the ASPX page

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;  

namespace Test
{
    public partial class DataDisplay : System.Web.UI.Page
    {
        SqlConnection cnn = new SqlConnection();
        DataTable dt = new DataTable(); 
        int rowIndex = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            cnn.ConnectionString = "data source=.;initial catalog=northwind;user id=sa;password=sa";
            cnn.Open();
            PopulateDataTable();
            if (!IsPostBack)
            {
                Label1.Text = dt.Rows[0]["CompanyName"].ToString();
                HiddenField1.Value = "1";
            }

            if (Request.Form["HiddenField1"] != null)
                rowIndex = Convert.ToInt16(Request.Form["HiddenField1"].ToString());  

            if (rowIndex == dt.Rows.Count && rowIndex != 0)
            {
                rowIndex = 0;
                HiddenField1.Value = 0;
            }

        }

        private void PopulateDataTable()
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cnn;
            cmd.CommandText = "SELECT * FROM CUSTOMERS";
            cmd.CommandType = CommandType.Text;

            SqlDataAdapter sDA = new SqlDataAdapter();
            sDA.SelectCommand = cmd;
            sDA.Fill(ds, "Customer");
            cnn.Close();
            dt = ds.Tables[0]; 

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (dt.Rows.Count > 0)
            {
                Label1.Text = dt.Rows[rowIndex]["CompanyName"].ToString();
                rowIndex++;
                HiddenField1.Value = rowIndex.ToString(); 
            }
        }
    }
}
using System.data.sqlclient;

// add here other necessary name spaces

follow the steps
declare following items globally

dataset ds=new dataset();
     static int count=0;

1. write the following code in your page_load/form_load(in case of window application)

sqlDataAdapter adp=new SqlDataAdapter("select * from    tablename", yourconnectionstring);
         
         adp.fill(ds);
         label1.text=ds.tables[0].rows[count][0].tostring();//this code will give you first row's first columns's data. count=row number and [0]=column no.

2. Now write the following code to your button click event

count++;
      label.text=     ds.tables[0].rows[count][0].tostring();
commented: Encase your code in: [code] and [/code] tags -1

You can loop the Rows of the DataTable to move to the Next data row... But since you need to get the next row entry on button click then you can declare an int variable that would increment the value by 1 on every click..

Here's an example:

private DataTable GetData()
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
        try
        {
            connection.Open();
            SqlCommand sqlCmd = new SqlCommand("Select * From Table", connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            connection.Close();
        }
        return dt;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = GetData();
            if (dt.Rows.Count > 0)
            {
                // Populate the TextBox with the first entry on page load
                TextBox1.Text = dt.Rows[0]["Name"].ToString(); 
                //Then we store the DataTable in Session so that we will NOT
                //query the DB on every postbacks
                Session["dt"] = dt;
            }

        }
     }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int rowIndex = 0;
        rowIndex++;
        if (Session["dt"] != null)
        {
            DataTable dt = (DataTable)Session["dt"];
            if (rowIndex <= dt.Rows.Count)
            {
                //get the next row entry on Button Click by setting the Row Index
                TextBox1.Text = dt.Rows[rowIndex]["Name"].ToString(); 
            }
        }
     }
commented: Please use [code] tags. -1
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

namespace database_connectivity
{
    public partial class _Default : System.Web.UI.Page
    {
        OleDbConnection con;
        DataTable dt = new DataTable();
        
        
        OleDbDataReader thisreader;
        int rowindex = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/try/stud.accdb");
            con.Open();
            PopulateDataTable();

            if (!IsPostBack)
            {
                TextBox1.Text = dt.Rows[0]["Rollno"].ToString();
                TextBox2.Text = dt.Rows[0]["Name1"].ToString();
                HiddenField1.Value = "1";
            }

            if(Request .Form ["HiddenField1"]!=null )
                rowindex=Convert.ToInt16 (Request .Form["HiddenField1"].ToString ());

            if (rowindex == dt.Rows.Count && rowindex != 0)
            {
                rowindex = 0;
                HiddenField1.Value = "0";
            }


        }
       
        protected void Button1_Click(object sender, EventArgs e)
        {

            if (dt.Rows.Count > 0)
            {
                TextBox1.Text = dt.Rows[rowindex]["Rollno"].ToString();
                TextBox2.Text = dt.Rows[rowindex]["Name1"].ToString();
                rowindex++;
                HiddenField1.Value = rowindex.ToString();
            }
        }

        private void PopulateDataTable()
        {
            DataSet ds = new DataSet();
            OleDbCommand cmd = con.CreateCommand();

            cmd.Connection = con;
            cmd.CommandText = "SELECT Rollno,Name1 from stud";
            cmd.CommandType = CommandType.Text;

            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = cmd;
            adapter.Fill(ds, "Stud");

            con.Close();

            dt = ds.Tables[0];
        }
    }
}

Viewing next record until end of record.....

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    int count, count2, count3;
    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\quiz.mdb");
        try
        {
            con1.Open();
            OleDbCommand cmd = new OleDbCommand("select * from quizpaper",con1);
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = cmd;
            adapter.Fill(dt);
         }
        catch (Exception ex)
        {
            string msg = "Fetch Error:";
        }
        
        return dt;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd_qno.Visible = false;
        cmd_count.Visible = false;
        cmd_answer.Visible = false;
        if (!Page.IsPostBack)
        {
            DataTable dt = GetData();
            if (dt.Rows.Count > 0)
            {
                lbl_question.Text = dt.Rows[0]["q_question"].ToString();
                rd_answers.Items.Add(dt.Rows[0]["q_answer1"].ToString());
                rd_answers.Items.Add(dt.Rows[0]["q_answer2"].ToString());
                rd_answers.Items.Add(dt.Rows[0]["q_answer3"].ToString());
                rd_answers.Items.Add(dt.Rows[0]["q_answer4"].ToString());
                Session["dt"] = dt;
            }
        }
    }

    protected void cmd_next_Click(object sender, EventArgs e)
    {
        rd_answers.Items.Clear();    
        try
        {
            cmd_qno.Text = (Int32.Parse(cmd_qno.Text) + 1).ToString();
            cmd_count.Text = (Int32.Parse(cmd_count.Text) + 1).ToString();
            lbl_qno.Text = cmd_qno.Text;
            
            if (Session["dt"] != null)
            {
                DataTable dt = (DataTable)Session["dt"];
                count = Int32.Parse(lbl_qno.Text);
                if (count < dt.Rows.Count)
                {
                    lbl_question.Text = dt.Rows[count]["q_question"].ToString();
                    rd_answers.Items.Add(dt.Rows[count]["q_answer1"].ToString());
                    rd_answers.Items.Add(dt.Rows[count]["q_answer2"].ToString());
                    rd_answers.Items.Add(dt.Rows[count]["q_answer3"].ToString());
                    rd_answers.Items.Add(dt.Rows[count]["q_answer4"].ToString());
                         
                 }
                else
                {
                    cmd_next.Visible = false;
                    lbl_answer.Visible = false;
                    lbl_qno.Visible = false;
                    lbl_question.Text = "You complete this Exam...................................";
                    Label1.Visible = false;
                 }
            }
     }
        catch (Exception ex)
        {
        }
     }
    protected void rd_answers_SelectedIndexChanged(object sender, EventArgs e)
    {
         DataTable dt = (DataTable)Session["dt"];
         count2 = Int16.Parse(cmd_count.Text);
         if ((rd_answers.SelectedValue).Equals((dt.Rows[count2]["q_correctanswer"].ToString())))
         {
            cmd_answer.Text = (Int32.Parse(cmd_answer.Text) + 1).ToString();
            count3 = Int16.Parse(cmd_answer.Text);
         }
         else
         {
            cmd_answer.Text = (Int32.Parse(cmd_answer.Text) - 1).ToString();
            count3 = Int16.Parse(cmd_answer.Text);
         }
         lbl_mark.Text = "You Got Mark :" + count3;
    }
}

what did you mean by cmd_qno is it textbox or what?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.