Free C# Tutorials

home
Stay at Home and Learn

Move Backwards through the Database

 
Computer Tutorials List

 

 

 

 

<< Part of an ongoing lesson - first part is here >>

We can use similar code to move backwards through the records in the database.
Add another button to your form. Change the Text property to Previous Record. Change the Name property to btnPrevious.

Double click your new button to get at the coding window. Now add the following:

C# code to move to the previous record in the database

The If statement is now only checking the inc variable. We need to check if it's greater than zero. If it is, we can deduct 1 from inc, and then call our NavigateRecords methods. When the form loads, remember, inc will be 0. So if we tried to move back one record after the form first loads the programme would crash. It would crash because we'd be trying to access Rows[-1].

Run your programme and test it out. Click you Previous Record button and you should see this:

First record in the database

Click both of your buttons and make sure you can move back and forward through the records. You programme shouldn't crash!

Next, we'll see how to jump to the end, and to the start of the database.

 

<-- Navigation Buttons | Move to the First and Last Record -->

<--Back to the C# .NET Contents Page

View all our Home Study Computer Courses