BIM211 – Visual Programming Database Operations II 1.

Slides:



Advertisements
Similar presentations
Access Manual 4 By Elham S.Khorasani Ismail Guneydas Dhawala Kovuri.
Advertisements

Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Microsoft Office 2007 Access Chapter 3 Maintaining a Database.
BIM211 – Visual Programming Database Operations 1.
BIM313 – Advanced Programming Database Operations 1.
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Access Lesson 2 Creating a Database
1 Web-Enabled Decision Support Systems Advance Topics in Database Connectivity Prof. Name Position (123) University Name.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
EGR 141 Computer Problem Solving in Engineering and Computer Science Interfacing with a Database in Visual Basic.NET 2005.
Creating And Maintaining A Database. 2 Learn the guidelines for designing databases When designing a database, first try to think of all the fields of.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Microsoft Visual Basic 2012 CHAPTER TEN Incorporating Databases with ADO.NET.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Access Project 3 Maintaining a Database Using the Design and Update Features of Access.
Access Tutorial 10 Automating Tasks with Macros
Chapter 2 Querying a Database
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2005 CHAPTER 12 Cell Phone Applications and Web Services.
Chapter 3 Maintaining a Database
Copyright 2007, Information Builders. Slide 1 Maintain & JavaScript: Two Great Tools that Work Great Together Mark Derwin and Mark Rawls Information Builders.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 2 Querying a Database MICROSOFT ACCESS 2010.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Working with a Database
Office 2003 Advanced Concepts and Techniques M i c r o s o f t Excel Project 5 Creating, Sorting, and Querying a List.
Chapter 1 Databases and Database Objects: An Introduction
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Lesson No:9 MS-Word Tools, Mail Merge and working with Tables CHBT-01 Basic Micro process & Computer Operation.
BİL528 – Bilgisayar Programlama II Database Operations II 1.
Chapter 6 Advanced Report Techniques
Copyright 2007, Information Builders. Slide 1 WebFOCUS Maintain – Enhancing Objects with JavaScript Mark Derwin Information Builders Information Builders.
Office 2003 Advanced Concepts and Techniques M i c r o s o f t Access Web Feature Data Access Pages.
Chapter 6 Generating Form Letters, Mailing Labels, and a Directory
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Lesson 2.  To help ensure accurate data, rules that check entries against specified values can be applied to a field. A validation rule is applied to.
Office 2003 Advanced Concepts and Techniques M i c r o s o f t Access Project 6 Switchboards, PivotTables, and PivotCharts.
XP New Perspectives on Microsoft Access 2002 Tutorial 21 Microsoft Access Tutorial 2 – Creating And Maintaining A Database.
Microsoft Office 2007 Access Chapter 6 Using Macros, Switchboards, PivotTables, and PivotCharts.
MS-ACCESS BY SANGEETHA PARTHASARATHY Topics to be covered §Comparing Values in Selection Criteria §Calculating Values in a Query §Changing the appearance.
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
Microsoft Office 2007 Access Chapter 3 Maintaining a Database.
Office Management Tools II Ms Saima Gul. Office Management Tools II Ms Saima Gul.
Learning With Computers II (Level Orange) ©2012 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly.
Using Microsoft Visual Studio 2005 Original by Suma Rao Revised by John G. McMahon ( 9/6/2008 )
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Access Project 7 Advanced Report and Form Techniques.
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
CS 281 – Fall 2015 Lab 4 Parametric Query and Forms in MS Access.
INTRODUCTION TO ACCESS. OBJECTIVES  Define the terms field, record, table, relational database, primary key, and foreign key  Create a blank database.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
CS 281 – Fall 2010 Lab 4 Parametric Query and Forms in MS Access.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Incorporating Databases with ADO.NET
Incorporating Databases with ADO.NET
Chapter 7 Advanced Form Techniques
Presentation transcript:

BIM211 – Visual Programming 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 solution b)Displaying students c)Adding new student d)Changing student info e)Deleting a student f)Displaying all courses a student take 3

c) Adding New Student 4

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

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

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

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

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

Add a Button to Main Form 10

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

Run the Program 12

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

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

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

d) Changing Student Info 16

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

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

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

In the class definition, write “prop” 20

Press “Tab” key two times 21

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

Write “StudentID” as the name of the property 23

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

Binding Controls 25

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

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

Click the arrow button on the right 28

Click plus sign near “Other Data Sources” 29

Click plus sign near “Project Data Sources” 30

Click plus sign near “SchoolDataSet” 31

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

Text is now bound and three controls have appeared 33

Bind “Last Name” text box 34

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

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

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

“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

Load event handler of the form 39

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

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

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

All Codes 43

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

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

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

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

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

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

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

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

The student info is displayed: 52

Change values and click OK 53

The data has been changed! 54

e) Deleting a Student 55

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

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

f) Displaying All Courses a Student Take 58

Add a “Display Courses” button into the form 59

Create a new form “frmStudentCourses” 60

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

Click “Add Query…” command of coursesTableAdapter object 62

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

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

Select both Enrolment and Students tables and click Add button 65

Tables are displayed in Query Builder, click Close button 66

Tables are automatically joined on CourseID and StudentID fields 67

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

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

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

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

Click OK to return to the program 72

Select the toolstrip and delete it 73

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

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

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

Write this code into the Click event 77

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

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

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

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

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

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

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