0

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
}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by rekhad
0

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
        }
..
0

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..

0

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(); 
}
}
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.