BIM211 – Visual Programming

79
BIM211 – Visual Programming Database Operations II 1

description

BIM211 – Visual Programming. Database Operations II. Contents. Adding a new record into database Changing an existing record Deleting a record Displaying data from multiple tables. 3. Writing the Program. Adding database file into solution Displaying students Adding new student - PowerPoint PPT Presentation

Transcript of BIM211 – Visual Programming

Page 1: BIM211 – Visual Programming

BIM211 – Visual Programming

Database Operations II

1

Page 2: BIM211 – Visual Programming

Contents

• Adding a new record into database• Changing an existing record• Deleting a record• Displaying data from multiple tables

2

Page 3: BIM211 – Visual Programming

3. Writing the Program

a) Adding database file into solutionb) Displaying studentsc) Adding new studentd) Changing student infoe) Deleting a studentf) Displaying all courses a student take

3

Page 4: BIM211 – Visual Programming

c) Adding New Student

4

Page 5: BIM211 – Visual Programming

Create a New Form “frmNewStudent”

5

tbFirstName

tbLastName

calBirthDay

numAge

btnOK withDialogResult = OK

btnCancel withDialogResult = Cancel

Page 6: BIM211 – Visual Programming

Drag & Drop a StudentsTableAdapter object onto the form

6

Page 7: BIM211 – Visual Programming

Double-Click OK Button and Write the Following Code

studentsTableAdapter1.Insert(tbFirstName.Text,tbLastName.Text,calBirthDay.Value,(short)numAge.Value);

7

Page 8: BIM211 – Visual Programming

Add a Button to Main Form

8

Page 9: BIM211 – Visual Programming

Double-Click the Button and Write the Following Code:

frmNewStudent frm = new frmNewStudent();DialogResult result = frm.ShowDialog();if (result == DialogResult.OK){ studentsTableAdapter.Fill( schoolDataSet.Students);}

9

Page 10: BIM211 – Visual Programming

Run the Program

10

Page 11: BIM211 – Visual Programming

Click “New Student” button, enter data, and click OK

11

Page 12: BIM211 – Visual Programming

Some Notes

• If you close your application, make some changes in your program, build the program again, and execute the program, the last added records won’t be visible!

• This is because each time you build the project, original database file is copied into the Debug folder in your solution and all operations are made on this copy.

12

Page 13: BIM211 – Visual Programming

d) Changing Student Info

13

Page 14: BIM211 – Visual Programming

Create a new form “frmChangeStudent” similar to “frmNewStudent”

14

Page 15: BIM211 – Visual Programming

Binding Data to the Controls

• In order to fill the controls, we are going to use data binding feature instead of changing contents of the controls.

• The frmChangeStudent form needs the StudentID to display the data of the selected student.

• For this purpose, create a property in frmChangeStudent form. This property will transfer student ID from main form to frmChangeStudent form.

15

Page 16: BIM211 – Visual Programming

In the class definition, write “prop”

16

Page 17: BIM211 – Visual Programming

Press “Tab” key two times

17

Page 18: BIM211 – Visual Programming

The type of the property is “int” and it is ok, so press “Tab” key

18

Page 19: BIM211 – Visual Programming

Write “StudentID” as the name of the property

19

Page 20: BIM211 – Visual Programming

All spaces are filled, so press “Enter” key. The property is ready now!

20

Page 21: BIM211 – Visual Programming

Binding Controls

21

Page 22: BIM211 – Visual Programming

Select “First Name” text box. You’ll see “DataBindings” section in the Properties window

22

Page 23: BIM211 – Visual Programming

We want to bind the Text property of the textbox, so click “Text”

23

Page 24: BIM211 – Visual Programming

Click the arrow button on the right

24

Page 25: BIM211 – Visual Programming

Click plus sign near “Other Data Sources”

25

Page 26: BIM211 – Visual Programming

Click plus sign near “Project Data Sources”

26

Page 27: BIM211 – Visual Programming

Click plus sign near “SchoolDataSet”

27

Page 28: BIM211 – Visual Programming

Click plus sign near “Students” and click “FirstName” field

28

Page 29: BIM211 – Visual Programming

Text is now bound and three controls have appeared

29

Page 30: BIM211 – Visual Programming

Bind “Last Name” text box

30

Page 31: BIM211 – Visual Programming

Bind “Value” (not “Text”) property of the calendar object

31

Page 32: BIM211 – Visual Programming

Bind “Value” of the numeric up/down object

32

Page 33: BIM211 – Visual Programming

Notice that a new “Fill” code has added into the “Load” event of the form

33

Page 34: BIM211 – Visual Programming

“FillByStudentID” instead of “Fill”

• We want to display the data of only one student.

• So, we need to use FillByStudentID method instead of Fill method.

• Delete the line and write this:this.studentsTableAdapter.FillByStudentID(

this.schoolDataSet.Students,this.StudentID);

34

Page 35: BIM211 – Visual Programming

Load event handler of the form

35

Page 36: BIM211 – Visual Programming

OK Button

• When the form is displayed, the data of the student is displayed on the controls.

• User changes these data and presses OK button.

• So, we need to write updating code into the Click event of the OK button.

36

Page 37: BIM211 – Visual Programming

Updating to Database

• The modifications made by the used must be applied to the data set. This is accomplished by the EndEdit() method of the BindingSource object.

• The changes on the data set is applied to the database by the Update() method of the studentTableAdapter object.

37

Page 38: BIM211 – Visual Programming

OK Button Click Event

• Double-click the OK button and write this code:

this.studentsBindingSource.EndEdit();this.studentsTableAdapter.Update(

this.schoolDataSet.Students);

38

Page 39: BIM211 – Visual Programming

All Codes

39

Page 40: BIM211 – Visual Programming

Passing Student ID

• Now, the form can display and update the data of the student whose ID is specified by the StudentID property.

• So, we need to set this property before the form is shown.

• This should be done in the main form.

40

Page 41: BIM211 – Visual Programming

Getting the ID of the selected student

• The ID of the student selected from the DataGridView object can be obtained in two ways:

1.Get the ID from the first cell of the selected row or the DataGridView.

2.Get the ID from the binding source object.

41

Page 42: BIM211 – Visual Programming

1. Getting ID from DataGridView

int studentID =(int)dataGridView1.SelectedRows[0].Cells[0].Value;

• In order this code to be successfully executed, you need to set MultiSelect property of the DataGridView object to False and SelectionMode property to FullRowSelect.

42

Page 43: BIM211 – Visual Programming

2. Getting ID from Binding Source

• When a row is selected in the DataGridView object, the information about the selected row is stored in binding source object. You can get the StudentID by using this code:

DataRowView rowView = (DataRowView)studentsBindingSource.Current;

SchoolDataSet.StudentsRow row = (SchoolDataSet.StudentsRow)rowView.Row;

int studentID = row.StudentID;

43

Page 44: BIM211 – Visual Programming

Creating the frmChangeStudent dialog and passing student ID

• Add a new button with the text “Change Student Info” into the main form.

• Write the code given in the next slide into the Click event handler of the button.

44

Page 45: BIM211 – Visual Programming

DataRowView rowView = (DataRowView)studentsBindingSource.Current;

SchoolDataSet.StudentsRow row = (SchoolDataSet.StudentsRow)rowView.Row;

int studentID = row.StudentID;

frmChangeStudent frm = new frmChangeStudent();frm.StudentID = studentID;DialogResult result = frm.ShowDialog();

if (result == DialogResult.OK){ // Update the DataGridView: this.studentsTableAdapter.Fill(this.schoolDataSet.Students);}

45

Page 46: BIM211 – Visual Programming

Run the program, select a student, and click “Change Student” button

46

Page 47: BIM211 – Visual Programming

The student info is displayed:

47

Page 48: BIM211 – Visual Programming

Change values and click OK

48

Page 49: BIM211 – Visual Programming

The data has been changed!

49

Page 50: BIM211 – Visual Programming

e) Deleting a Student

50

Page 51: BIM211 – Visual Programming

Add a “Delete Student” button into the main form

51

Page 52: BIM211 – Visual Programming

Double-click the button and write the following code:

// Get the selected row:DataRowView rowView =

(DataRowView)studentsBindingSource.Current;SchoolDataSet.StudentsRow row =

(SchoolDataSet.StudentsRow)rowView.Row;

// Delete the student:this.studentsTableAdapter.Delete(row.StudentID,

row.FirstName, row.LastName, row.BirthDay, row.Age);

// Update DataGridView:this.studentsTableAdapter.Fill(schoolDataSet.Students);

52

Page 53: BIM211 – Visual Programming

f) Displaying All Courses a Student Take

53

Page 54: BIM211 – Visual Programming

Add a “Display Courses” button into the form

54

Page 55: BIM211 – Visual Programming

Create a new form “frmStudentCourses”

55

Page 56: BIM211 – Visual Programming

Select “Courses” as the Data Source of the DataGridView object

56

Page 57: BIM211 – Visual Programming

Click “Add Query…” command of coursesTableAdapter object

57

Page 58: BIM211 – Visual Programming

Write “FillByStudentID” into the query name box and click “Query Builder”

58

Page 59: BIM211 – Visual Programming

Right-Click on the empty area and select “Add Table…” command

59

Page 60: BIM211 – Visual Programming

Select both Enrolment and Students tables and click Add button

60

Page 61: BIM211 – Visual Programming

Tables are displayed in Query Builder, click Close button

61

Page 62: BIM211 – Visual Programming

Tables are automatically joined on CourseID and StudentID fields

62

Page 63: BIM211 – Visual Programming

Scroll down to an empty “Column” cell and select “Students.StudentID”

63

Page 64: BIM211 – Visual Programming

Go to the “Filter” column and write “=?”

64

Page 65: BIM211 – Visual Programming

Go to the “Output” column and clear the check box

65

Page 66: BIM211 – Visual Programming

If you wish, you can execute the query. Click OK to return to the previous window

66

Page 67: BIM211 – Visual Programming

Click OK to return to the program

67

Page 68: BIM211 – Visual Programming

Select the toolstrip and delete it

68

Page 69: BIM211 – Visual Programming

Go to the code view and add a property named StudentID

69

Page 70: BIM211 – Visual Programming

Go to the Load event of the form and change the code

70

Page 71: BIM211 – Visual Programming

Go back to the main form and double-click “Display Courses” button

71

Page 72: BIM211 – Visual Programming

Write this code into the Click event

72

Page 73: BIM211 – Visual Programming

Execute the program, select a student, and click “Display Courses”

73

Page 74: BIM211 – Visual Programming

Courses taken by the student are displayed but first and last names of the student are missing!

74

Page 75: BIM211 – Visual Programming

Select “First Name” label and bind its Text to “FirstName” field as shown below

75

Page 76: BIM211 – Visual Programming

Select “Last Name” label and bind its Text to “LastName” field as shown below

76

Page 77: BIM211 – Visual Programming

Go to the Load event and change Fill method to FillByStudentID method

77

Page 78: BIM211 – Visual Programming

Execute the program, select a student, and click “Display Courses”

78

Page 79: BIM211 – Visual Programming

First and last names of the student are successfully displayed now!

79