Click here to Skip to main content
16,017,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i am trying to navigate to next record through textbox but have not yet succeeded, tried different approaches but with no result
C#
public partial class data_entry : System.Web.UI.Page
{
    SqlDataAdapter da1;
    DataTable dt1;
    int i = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        string strcon = ConfigurationManager.ConnectionStrings["gallup"].ConnectionString;
        string next = "SELECT * from city_breakup";
        da1 = new SqlDataAdapter(next, strcon);
        dt1 = new DataTable();
        da1.Fill(dt1);
    }
protected void btn_next_Click(object sender, EventArgs e)
    {
        if(i<dt1.Rows.Count-1){
            i++;
            txtlcity.Text=dt1.Rows[i]["city"].ToString();
            txtsample.Text = dt1.Rows[i]["sample"].ToString();
        }
    }
}
Posted
Comments
ZurdoDev 19-Jul-12 7:57am    
What's the question?
Sandeep Mewara 19-Jul-12 9:23am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
mehdilahori 19-Jul-12 10:55am    
2 textboxes on page
txtCity
txtSample
one button
btnNext
when user clicks on button next record should be shown in respective textboxes
i am able to move to the next record only once but when i click next button second time nothing happens
can not store value of row index
thanks you very much

1 solution

Instance variables in the page class (in this case, i) are not stored between requests. You have a (very common) fundamental misunderstanding of the nature of a web application, and HTTP in general: your page class instance exists for the lifetime of a single HTTP request (i.e. a single request and response containing a page), and when the user requests a new page (for example by pressing a postback button) you will get a new instance, and i will be 0 again.

This misunderstanding is encouraged by the fakery that old school ASP.net indulges in (which is why I hate it): postback controls store lots of state in a very opaque way and make it look like your instance stays active and its state information is preserved. That is not the case as you discover when you try to write code similar to a WinForms application.

In this case I recommend simply making the 'next' button or link submit a query parameter that is the page to request, and extracting it from Request.Form in your page code.
 
Share this answer
 
Comments
mehdilahori 19-Jul-12 11:04am    
thank you Bob
what did you mean by query parameter and how can i achieve this approach
thanks
BobJanova 19-Jul-12 12:46pm    
A query parameter is a ?key=value in the URL, or in a posted back form. I think there's also a Command interface in ASP.net that you can use to attach result values to buttons. I'm not an expert on old school ASP.net though (because it's awful).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900