You are on page 1of 45

C#

CRUD
(Local DB)
VERA A. PANAGUITON
Setting up the
GUI
Create CRUD Form

Sample names:
studentidtb -> for textbox
nametb -> for textbox
addbtn -> for buttons
studentdgv -> for datagridview

APDEV I by Vera A. Panaguiton 3


Creating the
Database
Add Local database
Right Click on the Project Name

Select Add -> New Item

APDEV I by Vera A. Panaguiton 5


Local database (MDF)
Select Data -> Service-based Database

You can change


database name.

APDEV I by Vera A. Panaguiton 6


Local database (MDF)

Double click the mdf file or


go to the Server Explorer.

APDEV I by Vera A. Panaguiton 7


Add Table
Right Click Table then select Add New Table

APDEV I by Vera A. Panaguiton 8


Change Table Name
Change the table
name here.

APDEV I by Vera A. Panaguiton 9


Save table
CTRL + S to save
or click the save
icon.

Save as SQL file

APDEV I by Vera A. Panaguiton 10


Update table
Update Table to
save changes

APDEV I by Vera A. Panaguiton 11


Refresh Database
Refresh Table to view
changes. Right click Table
then Select Refresh

APDEV I by Vera A. Panaguiton 12


Add data
Right click specific Table then
Select Show Table Data

Note: Update DB if you have changes and refresh in the Server Explorer.
APDEV I by Vera A. Panaguiton 13
Coding
View Connection String
Just click the dataset (Database1.mdf)
in the Server Explorer to show its
Properties. You can copy this to add in
your code.

APDEV I by Vera A. Panaguiton 15


Add library
Add library to the project.

APDEV I by Vera A. Panaguiton 16


Instantiate
Add Connection String,
Command, Data Adapter
and Data Reader.

APDEV I by Vera A. Panaguiton 17


Add Connection String
Double click your Form to
add FormLoad() method.
Copy the connection string
from the properties of your
DB then paste in your
SqlConnection instantiation
before the @ value.

APDEV I by Vera A. Panaguiton 18


READ
READ (SELECT)
Add open
connection
method

Create method
for viewing data
in datagridview

Note: Use try catch for errors.


APDEV I by Vera A. Panaguiton 20
Call method in Form Load
Call method that
show data once
form is loaded.

APDEV I by Vera A. Panaguiton 21


Data is displayed when
the form is loaded

APDEV I by Vera A. Panaguiton 22


CREATE
ADD Button Code
Create method for button ADD.

APDEV I by Vera A. Panaguiton 24


OTHER METHOD FOR INSERT COMMAND
Other method used cmd.Parameters.AddWithValue()

APDEV I by Vera A. Panaguiton 25


Note: Add a clear method to clear data in the form.
APDEV I by Vera A. Panaguiton 26
SEARCH
Add event for Search Button.

Note: Just type the fields you want to filter for the
search.

APDEV I by Vera A. Panaguiton 27


UPDATE
DatagridView Properties
When Selecting a row in datagridview and display the Change ReadOnly to True so that data can’t be
data in the form. Change SelectionMode to edited.
FullRowSelect.

APDEV I by Vera A. Panaguiton 29


Event for Datagridview
To add event for datagridview
click. Click the lightning icon and
Double Click the CellContentClick
to automatically add the method.

APDEV I by Vera A. Panaguiton 30


CellContentClick Code
Code for selecting a row in the datagridview and display in the form

APDEV I by Vera A. Panaguiton 31


Click any data in the
datagridview.

APDEV I by Vera A. Panaguiton 32


UPDATE
Same statements as the
Add Button except the
SQL UPDATE command.

APDEV I by Vera A. Panaguiton 33


APDEV I by Vera A. Panaguiton 34
DELETE
Status Field
As much as possible avoid using SQL Delete command. Just insert new field status, and change your
query for displaying data.

To edit table fields,


double click the table
in the Server Explorer
and select Open Table
Definition

Add status field(column).

Note: Don’t forget to update every


time there are changes. If error
occurs in editing table data just
press the ESCAPE key.
APDEV I by Vera A. Panaguiton 36
Code for Delete Button
Update the status to deleted.

APDEV I by Vera A. Panaguiton 37


Edit Display Method
Edit status value and change all to active then change the query to show active students only.

APDEV I by Vera A. Panaguiton 38


APDEV I by Vera A. Panaguiton 39
Login
(ExecuteScalar)
Login Code

APDEV I by Vera A. Panaguiton 41


Others
DateTimePicker
Format of data if table field type is date or you can just use methods to change the format in your code.

APDEV I by Vera A. Panaguiton 43


COMBOBOX
Click Edit Items to add to choices.

APDEV I by Vera A. Panaguiton 44


- END -

APDEV I by Vera A. Panaguiton 45

You might also like