User Navigation in C# - (Next and Previous Buttons)

This time, I will teach you how to navigate user details using C# and MySQL Database. I create a simple navigation method that can be easily learned by a newbie in programming. It has a “Next” and “Previous” button that can navigate multiple records back and forth, all you have to do is follow the procedure below.

Creating Database

Create a database named "db_user". Write the following codes for creating and adding data in the table.
  1. CREATE TABLE `tbl_user` (
  2. `UserID` int(11) NOT NULL,
  3. `Fullname` varchar(30) NOT NULL,
  4. `Username` varchar(90) NOT NULL,
  5. `Pass` varchar(90) NOT NULL,
  6. `UserType` varchar(30) NOT NULL
  7.  
  8. --
  9. -- Dumping data for table `tbl_user`
  10. --
  11.  
  12. INSERT INTO `tbl_user` (`UserID`, `Fullname`, `Username`, `Pass`, `UserType`) VALUES
  13. (1, 'Janno Palacios', 'janobe', 'admin', 'Administrator'),
  14. (2, 'Jeanniebe Nillos', 'jean', 'janobe', 'Staff');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access MySQL libraries. Note : Add MySQL.Data.dll as your references. Visit this tutorial to know how to add MySQL.Data.dll
  1.  
  2. using MySql.Data.MySqlClient;

Step 4

Declare all the classes and variables that are needed.
  1.  
  2. MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=db_user;sslMode=none");
  3. MySqlDataAdapter da;
  4. MySqlCommand cmd;
  5. DataTable dt;
  6. string sql;
  7. int maxrow, inc;s

Step 5

Create a method to navigate the records.
  1.  
  2. private void navigate_records(string sql, int inc)
  3. {
  4. try
  5. {
  6. con.Open();
  7. cmd = new MySqlCommand();
  8. da = new MySqlDataAdapter();
  9. dt = new DataTable();
  10.  
  11. cmd.Connection = con;
  12. cmd.CommandText = sql;
  13.  
  14. da.SelectCommand = cmd;
  15. da.Fill(dt);
  16.  
  17. maxrow = dt.Rows.Count - 1;
  18.  
  19. txtUserID.Text = dt.Rows[inc].Field<int>(0).ToString();
  20. txtName.Text = dt.Rows[inc].Field<string>(1);
  21. txtUsername.Text = dt.Rows[inc].Field<string>(2);
  22. txtPass.Text = dt.Rows[inc].Field<string>(3);
  23. cboRole.Text = dt.Rows[inc].Field<string>(4);
  24. }
  25. catch (Exception ex)
  26. {
  27. MessageBox.Show(ex.Message, "Exception Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  28. }
  29. finally
  30. {
  31. con.Close();
  32. da.Dispose();
  33. }
  34. }

Step 6

Write the following code to retrieve the first record in a specific field in the first load of the form.
  1.  
  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4. inc = 0;
  5. sql = "SELECT * FROM `tbl_user`";
  6. navigate_records(sql, inc);
  7. }

Step 7

Write this code for the next record.
  1.  
  2. private void btnNex_Click(object sender, EventArgs e)
  3. {
  4. if(inc != maxrow)
  5. {
  6. inc = inc + 1;
  7. sql = "SELECT * FROM `tbl_user`";
  8. navigate_records(sql, inc);
  9. }
  10. else
  11. {
  12. MessageBox.Show("No more rows.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
  13. }
  14. }

Step 8

Write this code for the previous record.
  1.  
  2. private void btnPrev_Click(object sender, EventArgs e)
  3. {
  4. if (inc > 0)
  5. {
  6. inc = inc - 1;
  7. sql = "SELECT * FROM `tbl_user`";
  8. navigate_records(sql, inc);
  9. }
  10. else if(inc == 0)
  11. {
  12. MessageBox.Show("First Records", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
  13. }
  14. }
The complete source code is included. You can download it and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment