Hi all
I am using sqldatareader to read data from sqldatabase.
The data is being shown on web forms with tabcontainer and tabs.

The scenario is
I have three rows of data for a client.I want to read row by row data on a button click event.I used while loop and a sqldatareader, but this reads all rows at once and will display result only for the last row on my web form
----
------

----
---

SqlDataReader reader=null;
reader=cmd.ExecuteReader();
while(reader.Read())
{
txtboxName.text=reader["name"].ToString();
---
---
}
protected void button_click(Object sender,EventsArgs e)
{
//I want to read row data in the click event of a button
}

Use DataAdapter instead of DataReader class.

..
 SqlDataAdapter adp;
        DataTable dt;
        int ind;
        private void Form1_Load(object sender, EventArgs e)
        {
            adp = new SqlDataAdapter("select * from tablename", "put_connection_string_here");
            dt = new DataTable();
            adp.Fill(dt);
            ind = 0;
            showRec();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ind++;
            showRec();
        }
        void showRec()
        {
            if(dt.Rows.Count==0)
                return;
            if (ind >= dt.Rows.Count)
                ind = 0;
            TextBox1.Text = dt.Rows[ind][0].ToString(); //1st column value
        }
..

Thank you very much for your reply.Its working but partially.

Since the page is posted back, my 'ind' value is reset to zero on
button click.So i can fetch data for two rows only.'ind'=0 and 'ind'=1
Where do I set my 'ind' value?
Please help..

>Where do I set my 'ind' value?

Session.

Session["ind"]=ind;

Thank you again fro the help.I athought that but still I am not able to set it.Please check the code below
'ind' value is still either '0' or '1' only.

partial class frmPersonalShared1 : System.Web.UI.Page 
{
int ind; 
DataTable dt; 
protected void Page_Load(object sender, EventArgs e) 
{
if(IsPostBack) 
{
Session["ind"] = ++ind; 
}
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["connString"]); 
SqlCommand cmd = new SqlCommand("sp_sd", conn); 
cmd.CommandType = CommandType.StoredProcedure; 
SqlDataAdapter adp = new SqlDataAdapter(cmd); 
SqlParameter para = new SqlParameter("@PersonalID",SqlDbType.NVarChar); 
adp.SelectCommand.Parameters.Add(para);
para.Value = Session["Personal_ID"].ToString(); 
SqlParameter para_out = new SqlParameter("@output", SqlDbType.Int, 4); 
para_out.Direction = 
ParameterDirection.Output; 
//cmd.Parameters.Add(para_out); 
adp.SelectCommand.Parameters.Add(para_out);
//count = (int)para_out.Value; 
dt= new DataTable(); 
adp.Fill(dt);
showRes();
//int indx; 
//indx=Int32.Parse((Session["ind"].ToString())); 
}
protected void button_click(object sender, EventArgs e) 
{
Response.Write(ind);
showRes();
}
protected void showRes() 
{
ind=
Convert.ToInt32(Session["ind"]); 
if (dt.Rows.Count == 0) 
return; 
if (ind > dt.Rows.Count) 
ind = 0;
TextBox1.Text = dt.Rows[ind])][0].ToString(); 
TextBox2.Text = dt.Rows[ind][1].ToString(); 
}
}

I was able to solve the increment problem.I declared the variable as static in my class and was able to get ind=0,1,2 and so on.

static int ind;

Thank you very much for the help

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.