Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIM211 – Visual Programming Database Operations II 1.

Similar presentations


Presentation on theme: "BIM211 – Visual Programming Database Operations II 1."— Presentation transcript:

1 BIM211 – Visual Programming Database Operations II 1

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

3 3. Writing the Program a)Adding database file into solution b)Displaying students c)Adding new student d)Changing student info e)Deleting a student f)Displaying all courses a student take 3

4 c) Adding New Student 4

5 Create a New Form “frmNewStudent” 5 tbFirstName tbLastName calBirthDay numAge btnOK with DialogResult = OK btnCancel with DialogResult = Cancel

6 Alternatively, you can select “Details” in the Data Sources window and drag & drop Students DetailsView on the form 6

7 Delete the toolbar, put an OK and a Cancel button and delete the code in the Page Load 7

8 Drag & Drop a StudentsTableAdapter object onto the form (if there isn’t any) 8

9 Double-Click OK Button and Write the Following Code studentsTableAdapter1.Insert( tbFirstName.Text, tbLastName.Text, calBirthDay.Value, (short)numAge.Value); 9

10 Add a Button to Main Form 10

11 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); } 11

12 Run the Program 12

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

14 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. 14

15 Some Notes (continued) You can click the database file in the Solution Explorer window and select “Copy if newer” from the “Copy to output directory” property in the Properties window 15

16 d) Changing Student Info 16

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

18 Alternative Way You can select “Details” in the Data Sources window and drag & drop the Students DetailsView on the form (like slides 6 and 7) 18

19 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. 19

20 In the class definition, write “prop” 20

21 Press “Tab” key two times 21

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

23 Write “StudentID” as the name of the property 23

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

25 Binding Controls 25

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

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

28 Click the arrow button on the right 28

29 Click plus sign near “Other Data Sources” 29

30 Click plus sign near “Project Data Sources” 30

31 Click plus sign near “SchoolDataSet” 31

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

33 Text is now bound and three controls have appeared 33

34 Bind “Last Name” text box 34

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

36 Bind “Value” of the numeric up/down object 36

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

38 “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); 38

39 Load event handler of the form 39

40 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. 40

41 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. 41

42 OK Button Click Event Double-click the OK button and write this code: this.studentsBindingSource.EndEdit(); this.studentsTableAdapter.Update( this.schoolDataSet.Students); 42

43 All Codes 43

44 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. 44

45 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. 45

46 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. 46

47 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; 47

48 Alternative Way int pos = this.studentsBindingSource.Position; int studentID = (int)this.schoolDataSet.Students.Rows[pos]["StudentID"]; 48

49 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. 49

50 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); } 50

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

52 The student info is displayed: 52

53 Change values and click OK 53

54 The data has been changed! 54

55 e) Deleting a Student 55

56 Add a “Delete Student” button into the main form 56

57 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); 57

58 f) Displaying All Courses a Student Take 58

59 Add a “Display Courses” button into the form 59

60 Create a new form “frmStudentCourses” 60

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

62 Click “Add Query…” command of coursesTableAdapter object 62

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

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

65 Select both Enrolment and Students tables and click Add button 65

66 Tables are displayed in Query Builder, click Close button 66

67 Tables are automatically joined on CourseID and StudentID fields 67

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

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

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

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

72 Click OK to return to the program 72

73 Select the toolstrip and delete it 73

74 Go to the code view and add a property named StudentID 74

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

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

77 Write this code into the Click event 77

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

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

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

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

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

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

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


Download ppt "BIM211 – Visual Programming Database Operations II 1."

Similar presentations


Ads by Google