bİl528 – bilgisayar programlama ii database operations ii 1

79
BİL528 – Bilgisayar Programlama II Database Operations II 1

Upload: gertrude-garrison

Post on 29-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

BİL528 – Bilgisayar Programlama II

Database Operations II

1

Contents

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

2

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

c) Adding New Student

4

Create a New Form “frmNewStudent”

5

tbFirstName

tbLastName

calBirthDay

numAge

btnOK withDialogResult = OK

btnCancel withDialogResult = Cancel

Drag & Drop a StudentsTableAdapter object onto the form

6

Double-Click OK Button and Write the Following Code

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

7

Add a Button to Main Form

8

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

Run the Program

10

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

11

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

d) Changing Student Info

13

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

14

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

In the class definition, write “prop”

16

Press “Tab” key two times

17

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

18

Write “StudentID” as the name of the property

19

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

20

Binding Controls

21

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

22

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

23

Click the arrow button on the right

24

Click plus sign near “Other Data Sources”

25

Click plus sign near “Project Data Sources”

26

Click plus sign near “SchoolDataSet”

27

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

28

Text is now bound and three controls have appeared

29

Bind “Last Name” text box

30

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

31

Bind “Value” of the numeric up/down object

32

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

33

“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

Load event handler of the form

35

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

Updating to Database

• The modifications made by the user 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

OK Button Click Event

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

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

this.schoolDataSet.Students);

38

All Codes

39

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

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

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

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

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

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

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

46

The student info is displayed:

47

Change values and click OK

48

The data has been changed!

49

e) Deleting a Student

50

Add a “Delete Student” button into the main form

51

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

f) Displaying All Courses a Student Take

53

Add a “Display Courses” button into the form

54

Create a new form “frmStudentCourses”

55

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

56

Click “Add Query…” command of coursesTableAdapter object

57

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

58

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

59

Select both Enrolment and Students tables and click Add button

60

Tables are displayed in Query Builder, click Close button

61

Tables are automatically joined on CourseID and StudentID fields

62

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

63

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

64

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

65

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

66

Click OK to return to the program

67

Select the toolstrip and delete it

68

Go to the code view and add a property named StudentID

69

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

70

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

71

Write this code into the Click event

72

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

73

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

74

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

75

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

76

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

77

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

78

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

79