Working with databases

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
Ok I've moved onto my next lesson in my "Programming with VB .NET" book and I'm stuck on a few things.

Here are my chapter "hands on learning" instructions

Using the Data Source Configuration Wizard, establish a connection to the database named Payroll.mdb. The connection you create should select all the record from the table named tblEmployees. The table has the following fields and data types:

* fldID - Integer
* fldFirsdtName - String
* fldLastName - String
* fldDepartment - String
* fldEmployeeType - String
* fldDeductions - Integer
* fldWage - Double
* fldPension - Boolean

Create your bound control instances on the main form using the Data Sources window. Create a second form that will display the payroll records corresponding to the selected employee. In the second form, create an unbound DatraGridView control instance. Using the DataTable, search each record, and add only those records to the DataGridView that have the corresponding ID number. For each row, calculate the gross pay by multiplying the employee's wage by the number of hours worked by the employee for the pay period. Display in a TextBox the total gross pay for all the payroll records.


This is my first time even attempting something like this, so at first it blew me away but I kept at it (its been a few hours now) and I created form1 with all my labels and textbox's and bound all my textbox's to the corresponding fields of the database I created.

Now I'm stuck on what to do with form2. The first part of the instructions say to only select everything form "tblEmployees" If that's the case then how do I access the "tblPayroll" portion of the database for form2? But, I went ahead and edited the database connection and checked the "tblPayroll" as well (not sure if I should have but can't see what else to do.) Now I don't know how to make form2's textbox's corresponding with the selected employee in form1 of the tblEmployees.

The contents of tblPayroll are :

* fldID - Integer
* fldDate
* fldHoursWorked - Double
* fldGrossPay - Double
* fldFIT - Double
* fldFICA - Double
* fldNetPay - Double
 
Here are my chapter "hands on learning" instructions

Create your bound control instances on the main form using the Data Sources window.


This is my first time even attempting something like this, so at first it blew me away but I kept at it (its been a few hours now) and I created form1 with all my labels and textbox's and bound all my textbox's to the corresponding fields of the database I created.
I think you missed some details (pardon the pun) in previous reading if this took you hours. It is a one-second operation where you select "details" mode on your table in Data Sources window and drag it to form. This will create all labels and bound textboxes etc and all binding objects automatically.
 
Which is something I didn't know. And now do, thank you. But now I'm stuck at displaying the details of the payroll table based on the selected user of the employee table.
 
You could do this similarly, dragging the details table to child form to have "everything" generated. In parent form button that calls child form you could set the bindingsource filter to current master record id, like this:
VB.NET:
Dim row As somedbDataSet.Table1Row = DirectCast(Me.Table1BindingSource.Current, DataRowView).Row
childForm.Table2BindingSource.Filter = "t1id=" & row.id.ToString
childForm.ShowDialog()
Other options could be adding a FillByFK query to the child table and having this called to save data loading.

Or the other way around, load both master and child tables also in child form and just set same bindingsource position.

Or you could ignore the task and show details data also in parent form :))) Do this like described in Walkthrough: Displaying Related Data on a Form in a Windows Application by dragging the child element of master table onto form, this will link the bindingsource for child to master bindingsource and set Datamember of child bindingsource to be the FK relation.
 
I ahve a major issue with a book that is teaching you to this:

Using the DataTable, search each record, and add only those records to the DataGridView that have the corresponding ID number.

If by that the author means "use a for loop to iterate over the data table looking for a record and manually add them into a datagridview" then sorry, but that book is crap and you should return it for a refund, maybe also write to the publisher and tell them to tell the author to drag his ass out of the VB6 dark ages and learn how to do MVC properly
 
Back
Top