Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net moving to next record

Posted on 2012-03-29
7
Medium Priority
?
400 Views
Last Modified: 2012-03-30
I have found some code on the net and altered it a bit, but it onl works one time.
I need to 'scroll' through my records. But I can only go one time to the next record, what is wrong ?
code.txt
0
Comment
Question by:troosters
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37785868
Do this:-

protected void btnNext_Click(object sender, EventArgs e)
        {
                int rowIndex = 0;

                if(Session["rowIndex"]!=null)
                {
                    rowIndex = Convert.ToInt32(Session["rowIndex"]);
                    rowIndex++;
                }

                Session["rowIndex"]=rowIndex;
                if (Session["dt"] !=
                null)
                {
                    DataTable dt = (DataTable)Session["dt"];
                    if (rowIndex <= dt.Rows.Count)
                    {
                        txtName.Text = dt.Rows[rowIndex]["Name"].ToString();
                    }

                }

            
        }

Open in new window


Basically need to store rowindex in session so that its state persists during postbacks...
0
 
LVL 6

Expert Comment

by:ingriT
ID: 37785874
You keep on going to row 1 in your code (because you set it to 0 in the first line).

I'm not sure if keeping your datatable in a session is the best solution, why do you do that?

But if you also put your rowindex in a session variable, in the page_load where you also put your datatable in the session and then everytime when the btnNext is clicked you do the rowindex++ (and not the int rowindex = 0), you will get the next line.
0
 

Author Comment

by:troosters
ID: 37785881
BuggyCoder, I use int rowIndex = 1; at the beginning otherwise it just shows the first one twice. Also it goes on after the last record and then errors, so I changed if (rowIndex < dt.Rows.Count) in stead of <=

ingriT, I just found this code on the internet.
Is there a better way of doing it ? Now I just show one textbox, but it is going to be like 15 textboxes when it's finished.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:ingriT
ID: 37785887
What are you trying to achieve with your page? Are you showing a recordset, with a button to go to the next record? I would use a ListView for that, with a DataPager and a SqlDataSource, or a GridView with AllowPaging="true" and a SqlDataSource.

See these pages for an example:
http://www.4guysfromrolla.com/articles/021308-1.aspx
http://www.codeproject.com/Articles/16238/GridView-Custom-Paging
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 1200 total points
ID: 37785899
protected void btnNext_Click(object sender, EventArgs e)
        {
                int rowIndex = -1;

                if(Session["rowIndex"]!=null)
                {
                    rowIndex = Convert.ToInt32(Session["rowIndex"]);
                    rowIndex++;
                }

                Session["rowIndex"]=rowIndex;
                if (Session["dt"] !=
                null)
                {
                    DataTable dt = (DataTable)Session["dt"];
                    if (rowIndex < dt.Rows.Count)
                    {
                        txtName.Text = dt.Rows[rowIndex]["Name"].ToString();
                    }
                    else
                    {
                        //No More Data To Display
                    }

                }

Open in new window

           
        }
0
 

Author Comment

by:troosters
ID: 37785903
Do you also use that if you just want to show one record at a time. The record will contain lot's of info like name, firstname, address, language, work , job, weekend, car, driving licence, studies, hobbies, info, work experience, age, born, married, children, gender, nationality, ...

I thought to have a good overview it would be better to use something to fill textboxes which I can place wherever I want ?
0
 
LVL 6

Assisted Solution

by:ingriT
ingriT earned 800 total points
ID: 37785912
With the ListView there are a lot of options, including a DetailsView, to show all the information about one record.
See this page for a basic example; http://leedumond.com/blog/master-detail-editing-inserting-deleting-with-a-listview-and-detailsview/

You don't have to do the deleting/inserting, but it is a nice solution. It also keeps your code clean and easy to maintain imho.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question