Hi,
I am trying to populate some text box controls from my database where in i have 4 rows with six coulmns.
Please see the code below. I am able to see two of rows data when I press the button but it doesn't go to 3rd and 4th row. Please suggest me how i could make it possible
Let me know should you require more info.
Thanks,
Bhushan. ( Member ).
SqlDataReader dr;
SqlCommand dbCommand;
SqlConnection con;
private void Page_Load(object sender, System.EventArgs e)
{
string command;
con=new SqlConnection("server=HOME;database=myDatabase;Trusted_Connection=Yes");
command="Select * from Orders";
dbCommand=new SqlCommand(command,con);
con.Open();
dr=dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if(!dr.HasRows)
Response.Write("no rows");
else
{
recall();
}
}
public void Button1_Click(object sender, System.EventArgs e)
{
if(!dr.HasRows)
Response.Write("no rows");
else
{
recall();
}
}
public void recall()
{
dr.Read();
ordid.Text=dr["orderid"].ToString();
cname.Text=dr["customername"].ToString();
pnum.Text=dr["productnumber"].ToString();
pname.Text=dr["productname"].ToString();
qty.Text=dr["quantity"].ToString();
orddt.Text=dr["orderdate"].ToString();
}
Ashish SinghalPosted Feb 18, 2006, 7:03 PM
you need to call while(reader.Read()){} in button click and load this reader in page load and save into viewstate after use that viewstate in button click using while. you will see all rows which you want, Use if(!IsPostBack) in page load and write whole code inside it.