Add navigational buttons to a database application without using a BindingNavigator

On my previous post, we were able to add a navigational button to our application by using a BindingNavigator, today, we will do everything manually. To add a navigational button by hand, all you need to do is to bind your controls using this method and increment or decrement the Row index number to navigate around your records. This method is slightly similar to the methods used by most developers in adding navigational buttons to VB.net database applications.

For the sake of example, follow these steps(I assume here that you have already created a database file named “dbEmployee” and a table named “tblEmployee”):

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. open System.Data.OleDb  
  9. //creates a font  
  10. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  11. //creates a connection object  
  12. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  13.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  14.  //creates an OleDbDataAdapter  
  15. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)  
  16. let deletecommand=new System.Data.OleDb.OleDbCommand()  
  17.   
  18. //generates a dataset  
  19. let dataset11 = new DataSet()  
  20. //fills the dataset with recod values  
  21. dataadpter.Fill(dataset11,"tblEmployee")|>ignore  
  22. //creates a form  
  23. let dataform = new Form(Text="Add Navigational Buttons Manually",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  24. //creates our controls  
  25. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 170))    
  26. let label1=new Label(Text="Employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  27. let label2=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  28. let label3=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,100),AutoSize=true)  
  29. let emplabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  30. let fnamelabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  31. let lnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)  
  32. let topbutton=new Button(Text="Top", Location=new System.Drawing.Point(40, 170))  
  33. let bottombutton=new Button(Text="Bottom", Location=new System.Drawing.Point(120, 170))  
  34. //assings the font to our form  
  35. dataform.Font<-ffont  
  36. //adds the controls to our form  
  37. dataform.Controls.Add(exitbutton)  
  38. dataform.Controls.Add(label1)  
  39. dataform.Controls.Add(label2)  
  40. dataform.Controls.Add(label3)  
  41. dataform.Controls.Add(emplabel)  
  42. dataform.Controls.Add(fnamelabel)  
  43. dataform.Controls.Add(lnamelabel)  
  44. dataform.Controls.Add(topbutton)  
  45. dataform.Controls.Add(bottombutton)  
  46.   
  47. //binds the fieldnames to our label  
  48. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
  49. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))  
  50. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2))  
  51.   
  52. //when the topbutton is clicked  
  53. //assigns 0 rowindexnumber value causing the   
  54. //first record to be displayed  
  55. topbutton.Click.Add(fun top->  
  56. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
  57. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))  
  58. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2)))  
  59.   
  60. //when the bottombutton is clicked  
  61. //assign the index number of the last row as a rowindexnumer  
  62. //since we only have two records on our table  
  63. //so the index number of the last row is understood to be 1  
  64. //when manipulating large number of records, i suggest using  
  65. //dataset11.Tables.["tblEmployee"].Rows.Count() to  
  66. //automatically count the number of records  
  67.   
  68. bottombutton.Click.Add(fun bottom->  
  69. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(0))  
  70. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(1))  
  71. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(2)))  
  72.   
  73. //when the exit button is clicked  
  74. exitbutton.Click.Add(fun exit->  
  75. //close the form and dataconnection  
  76.                     dataform.Close()  
  77.                     oleconn.Close())  
  78.   
  79. //executes our application  
  80. dataform.Show()  
  81. Application.Run(dataform)  

5. This will display the following output:



6. Simple...right?