Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 14th, 2005, 03:38 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default DataReader problem

Hi,

   I have a problem with a data reader.In Page_Load event I'm populating a datareader with a SQL command and displaying the first record in 2 text boxes.

   When I press the Next button, next record should be loaded to those 2 text boxes.

   In Page_Load first record is loading properly and here's my code,

protected SqlDataReader drCustomer;

private void Page_Load(object sender, System.EventArgs e)
{

   SqlConnection myConnection = new SqlConnection("server=Nalaka;database=AMW;user id=sa;password=sa");
   myConnection.Open();

   SqlCommand cmdCustomers=myConnection.CreateCommand();
   cmdCustomers.CommandText="SELECT * FROM Customers";

   drCustomer =cmdCustomers.ExecuteReader();

   if (drCustomer.HasRows)

   {
    drCustomer.Read();

    txtCusName.Text=drCustomer.GetString(1).ToString() ;
    txtContactName.Text=drCustomer.GetString(2).ToStri ng();
   }

}


Then when I press the Next button for the first time it loads the secong record to text boxes. but after that when I'l pressing the next button again and again it doesn't move to next records it always shows the secong record. Data reader is not reading the next record after hte secong time.why is this

here's my code for Next button click event.
private void btnNext_Click(object sender, System.EventArgs e)
{
   drCustomer.Read();
   txtCusName.Text=drCustomer.GetString(1).ToString() ;
   txtContactName.Text=drCustomer.GetString(2).ToStri ng();

}

__________________
nalaka hewage
http://nalakahewage.blogspot.com
 
Old December 15th, 2005, 12:14 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Every single time your ASPX page loads (i.e. every HTTP request to the server) the page_load method runs. This happens before the button event handles. Here's what's happening:

Page Hit 1 (first plain page request)
-------------------------------------
Page Load
  Load data reader
  data reader.read()
  Populate text boxes

Page hit 2 (click next)
-----------------------
Page Load
  Load data reader
  data reader.read()
  Populate text boxes
Handle button click event
  data reader.read()
  populate text boxes

Page hit 3,4,5...n (click next)
-------------------------------
Page Load
  Load data reader
  data reader.read()
  Populate text boxes
Handle button click event
  data reader.read()
  populate text boxes

The same exact thing is happening every time you click the next button regardless of how many times you clicked it.

Here's what I recommend:
Instead of a data reader, use a data adapter/dataset. The data reader is a connected data resource which is good for speed, but doesn't permit direct record access (go to record X). The data table in a dataset will allow you to hit DataTable(x).Row(n). The page load method can populate the dataset in place of the datareader.

Then you'll need to store the "current record" index to a page state variable. This is best done with the ViewState collection. You should set this on the first page load to 0, then work from there. The "next" and "previous" buttons can increment/decrement the record index accordingly. Put the few lines to populate the textboxes in another function so you can call it from all the places it will need to be used (page load, next, prev, etc).

-Peter
 
Old December 19th, 2005, 11:15 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default



Hi Peter,

Thank you very much. I'll do what you suggested.

nalla



 
Old December 20th, 2005, 12:04 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default


Hi Peter,

Can U tell me how to declare a page state variable and where?
where do we set the current record index to the page state variable?

nalla



 
Old December 20th, 2005, 08:43 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Use viewstate:

Assign:
   ViewState[intCurrentPageIndex"] = x;

Retrieve:
   x = (int)ViewState[intCurrentPageIndex"];

-Peter
 
Old January 23rd, 2006, 04:11 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default


Hi peter,
sorry for getting late to tell this.
I followed what you mentioned and it's working fine.

Thank you very much

nalaka hewage
 
Old February 7th, 2006, 06:07 AM
Registered User
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi nalla,

      Can u please post the code?

Jayanta






Similar Threads
Thread Thread Starter Forum Replies Last Post
DataReader Problem gaurav_jain2403 ADO.NET 5 October 10th, 2007 03:48 PM
Datareader problem is about to cause my death joer80 .NET Framework 2.0 1 June 7th, 2006 07:20 AM
Datareader problem, killing me joer80 ADO.NET 1 March 23rd, 2006 06:44 PM
Problem with datareader ashokparchuri ADO.NET 2 May 13th, 2005 04:32 AM
Problem reading DataReader after Databind rathbird ADO.NET 1 March 10th, 2004 05:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.