C# .NET - English

Moving Forward And Backward Through The Database

To move forward through the database table, add a button to your form. Give
it the Name btnNext. Add some text to the button.

To move forward through the database table, add a button to your form. Give
it the Name btnNext. Add some text to the button.

Double click your button to get at the coding window. For the code, we need
to check what is inside of the MaxRows variable and make sure we don’t
go past it. MaxRows, remember, is holding how many records are in the DataSet.
We also need to increment the inc variable. It is this variable that
will move us on to the next record.

Add the following if statement to your button:

C# code to move forward through the database

The first line of the If Statement says “If inc does not equal
MaxRows minus 1″. If it doesn’t then we increment the inc
variable and call NavigateRecords. But can you see why we need to say
MaxRows – 1
? It’s because of the Rows[inc] line in our NavigateRecords
method. The count for Rows starts at zero. So if we only have 4 records in the
database, the count will be for 0 to 3. MaxRows, however, will be 4. If we don’t
deduct 1, the programme will crash with an error: IndexOutOfRange.

If MaxRows is reached, then we can display a message for the user: “No
more rows”.

Run your programme and test it out. You should be able to move forward through
your database. Here’s what your form should look like when the last record is
reached:

C# form showing the last record in the database

 

Move Backwards through the Database

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:

if (inc > 0)
{

inc–;
NavigateRecords( );

}
else
{

MessageBox.Show(“First Record”);

}

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 button and you should
see this:

C# form showing the first record in the database

 

Jump to the Last Record in your Database

To move to the last record of your database, you only need to make sure that
the inc variable and MaxRows have the same value.

Add a new button to your form. Set the Text property as Last Record,
and the Name property as btnLast. Double click the button, and add the
following code:

if (inc != MaxRows – 1)
{

inc = MaxRows – 1;
NavigateRecords( );

}

The If Statement again checks that inc is not equal to MaxRows
minus 1. If it isn’t, we have this:

inc = MaxRows – 1;

MaxRows minus 1 would equal 3 in a four record database. Because Rows[inc]
goes from 0 to 3, this is enough to move to the last record after the call to
NavigateRecords.

 

Jump to the First Record in your Database

To move to the first record in the database, we only need to set inc to zero.

Add another button to your form. Change the Text property to First Record.
Change the Name property to btnFirst. Double click your new button and add the
following code:

if (inc != 0)
{

inc = 0;
NavigateRecords( );

}

This just checks to see if inc isn’t already zero. If it isn’t, we set the
inc variable to 0. Then we call the NavigateRecords method.

Run your programme and test it out. You should now be able to move through
the records in your database without the programme crashing. What we’ll do now
is to allow the user to add a new record to the database. This is more complex
than the navigation, so you may need to pay close attention!

|

Kaynak : https://www.homeandlearn.co.uk/csharp/csharp_s12p8.html ‘sitesinden alıntı

Yorum Yap