1 / 79

BİL528 – Bilgisayar Programlama II

BİL528 – Bilgisayar Programlama II. 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

lyris
Download Presentation

BİL528 – Bilgisayar Programlama II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 3. Writing the Program • Adding database file into solution • Displaying students • Adding new student • Changing student info • Deleting a student • Displaying all courses a student take

  4. c) Adding New Student

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

  6. Drag & Drop a StudentsTableAdapter object onto the form

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

  8. Add a Button to Main Form

  9. 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); }

  10. Run the Program

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

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

  13. d) Changing Student Info

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

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

  16. In the class definition, write “prop”

  17. Press “Tab” key two times

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

  19. Write “StudentID” as the name of the property

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

  21. Binding Controls

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

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

  24. Click the arrow button on the right

  25. Click plus sign near “Other Data Sources”

  26. Click plus sign near “Project Data Sources”

  27. Click plus sign near “SchoolDataSet”

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

  29. Text is now bound and three controls have appeared

  30. Bind “Last Name” text box

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

  32. Bind “Value” of the numeric up/down object

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

  34. “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);

  35. Load event handler of the form

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

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

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

  39. All Codes

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

  41. Getting the ID of the selected student • The ID of the student selected from the DataGridView object can be obtained in two ways: • Get the ID from the first cell of the selected row or the DataGridView. • Get the ID from the binding source object.

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

  43. 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: DataRowViewrowView = (DataRowView)studentsBindingSource.Current; SchoolDataSet.StudentsRowrow = (SchoolDataSet.StudentsRow)rowView.Row; intstudentID = row.StudentID;

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

  45. DataRowViewrowView = (DataRowView)studentsBindingSource.Current; SchoolDataSet.StudentsRow row = (SchoolDataSet.StudentsRow)rowView.Row; intstudentID = row.StudentID; frmChangeStudentfrm = new frmChangeStudent(); frm.StudentID = studentID; DialogResult result = frm.ShowDialog(); if (result == DialogResult.OK) { // Update the DataGridView: this.studentsTableAdapter.Fill(this.schoolDataSet.Students); }

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

  47. The student info is displayed:

  48. Change values and click OK

  49. The data has been changed!

  50. e) Deleting a Student

More Related