You are on page 1of 15

WINDOWS

PROGRAMMING

Abstract
The practical curriculum of Programming on Windows helps students
master the C # programming language, Windows Form programming.
Provides basic knowledge of visual programming on the Windows
operating system. Basic operations with files, folders, databases,
processes, services, and libraries on Windows. At the end of the course,
students are provided with a full range of knowledge to apply to solve
real problems.

FACULTY OF INFORMATION TECHNOLOGY


Internal circulation, 2020
Windows Programming 1

LAB 4. WINDOWS FORM APPLICATION WITH


DATABASE BASIC

Overview
- In this part, we are going to learn how to perform CRUD operations in a Windows
Forms application using Entity Framework.
- List of Entity Framework: EF Basics, EF 6 DB-First, EF 6 Code-First, EF Core, EF
Cheat Sheet, EF Quiz

PRACTICE CONTENT

1.2.1 CRUD Operation Using Entity Framework In Windows Form


ApplicationCreate a project

1.2.1.1 Create database

First, we have to create database and table in SQL Server. Please find the below
scripts for creating the database and table respectively.
DB Script

1. CREATE DATABASE StudentInformation;


2. GO;

Table Script

1. CREATE TABLE [dbo].[StudentDetails](


2. [Id] [int] IDENTITY(1,1) NOT NULL,
3. [Name] [varchar](50) NULL,
4. [Age] [int] NULL,
5. [City] [varchar](50) NULL,
6. [Gender] [varchar](50) NULL,
7. CONSTRAINT [PK_StudentDetails] PRIMARY KEY CLUSTERED
8. (
9. [Id] ASC
10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE
_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = O
N) ON [PRIMARY]
11. ) ON [PRIMARY]

Here, they have used SQL Server 2016 laster version.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 2

1.2.1.2 Create Windows Forms Application

Create a Windows application project in Visual Studio. Please find the below
images for your reference.
Step 1: Create project

Rename the project to AppWDFConnectDMMSSqlServer, Then press the OK button


to complete.
Step 2: Design a form as per your requirement

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 3

After creating the project successfully, we proceed to design the interface. Here, they
have used labels, text-boxes, combo-boxes, Data GridView, and buttons.

Note: Here, the ID label is a hidden field.


Step 4 Connect Database
Add the ADO.NET Entity model in your project. To add, right click your solution
and select Add - > New Item -> Select "Data" in left pane and select "Ado.Net Entity
Model".
Note:
Here, you have used DB-First approach in Entity Framework. If you are not aware
of DB-First approaches, you have already provided the link at the top
Please find the below image for your reference.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 4

Select ADO.NET Entity Data Model, then rename Model1 to Student

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 5

After, Select EF Designer from Database in Windows “Entity Data Model


Wizard”, then click Next button. Then select “New Connection...”

At windows “Connection Properties”, We are choose the server name and select
the name of the database defined earlier. In case we don't remember the server name we
can enter the Server name with "."
Please find the below image for your reference.

They can test whether the connection is successful or not by clicking the "Test
Connection" button. Then click OK button, to continue.
Continue to press the Next button.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 6

Continue, we choose “Entity Framework 6.x” and click Next button.

At Windows “Entity Data Model Wizard”, We select all the defined tables or
some necessary ones. In the case where the database has "View, Stored Procedures and
Function" then we choose "View" and “Stored Procedures and Function”. After click
Finish button to Complete.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 7

Now, you can see the added Entity Model in your Solution Explorer.

Model Diagram

The next, let us create our custom model class for binding and displaying the values.

Note: Don't forget to declare your class as "public". Then only you can access this
class outside of any other class.

First, add a new folder, then rename "Models". Next, we add a new class and
rename to "StudentInfo".

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 8

Please find the below Images for your reference

Code Example: class StudentInfo. Include properties to display

Display value from Database into Data GridView


Bind the Student Details in Data GridView when form is loading and as well
as we have to bind the values for Gender combo-box and City combo-box values at
Event Form_Load.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 9

First, we define a function that retrieves data from the database. Then pour the
data into Data GridView. Please find the below code for your reference.

After defining the "Display" method, at the "Form_Load" event, we call the
"Display" method to execute. Please find the below code for your reference.

Press the F5 key to see the result. Result:

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 10

Now, let's write the code for "Save" button.

Click the "Save" button and write the below code. "SaveStudentDetails" is a
method to update the entity. Please find the below code for your reference.

Here, we are binding our input values (Name, Age, City, Gender) into
StudentDetails class and passing this to "SaveStudentDetails" method and saving the
entity. Please find the below code for your reference.

Press the F5 key, then enter the information in the box then click the Save button

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 11

Result:

Update
Next, write the code for update and delete operations. To update the record, we
have to select the record from data GridView. Here, I have written datagridview cell
click event to get the values from datagridview to fields. Please find the below code for
your reference.
We have to select the record from datagridview for update and delete. The same
event will help us to get the records.

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 12

Please find the below code for "Update". You define a function that updates data,
which returns the correct value if the update is successful, otherwise false if the update
fails.

After defining the data update function, you will call it in the update button click
event.

Clear the fields after Insert or Update or Delete operation

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 13

Delete
You define a function that delete data, which returns the correct value if the
delete is successful, otherwise false if the delete fails.

After defining the data delete function, you will call it in the delete button click
event. Please find the below code for "Delete".

So we have done the following operations: view, add, delete, and edit data of a
table in the database

WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY


Windows Programming 14

1.2.2 CRUD on multiple data tables

Same as above, now we do: view, add, delete, edit data across multiple tables.
Request student information management, student information including: student
number, student's name, date of birth, gender, email, phone number, place of birth
(province / city). Each student belongs to a class.
1. Design the student information management interface (view, add, delete, edit)
2. Design interface for managing the list of provinces / cities (view, add, delete,
edit)
3. Design the class list management interface (view, add, delete, edit)
4. Update student information into the database. Place of birth and class information
will be taken from two tables: Class and province / city. Show status (success /
failure)
5. Delete student information by student ID
6. Search student information by code or full name
Sample database

---The End ---


WINDOWS PROGRAMMING FACULTY OF INFORMATION TECHNOLOGY

You might also like