• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create CRUD Application in C#

Alex9409

Cloud Infrastructure Coder
Divine
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
122
Likes
165
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, I will teach you how to create a CRUD application using C#. This simple application will help you how to organize your codes and make it into group of functions that perform a task together. See the procedure below.
Creating Database

Create a database named it “dbpeople
Execute the following query for creating table.
  1. CREATE

    TABLE

    `dbpeople`

    .`tblperson`

    (

    `PersonID`

    INT

    NOT

    NULL

    AUTO_INCREMENT

    ,

    `FNAME`

    VARCHAR

    (

    90

    )

    NOT

    NULL

    ,

    `LNAME`

    VARCHAR

    (

    90

    )

    NOT

    NULL

    ,

    `ADDRESS`

    VARCHAR

    (

    255

    )

    NOT

    NULL

    ,

    PRIMARY KEY

    (

    `PersonID`

    )

    )

    ENGINE

    =

    InnoDB

    ;

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application.
csharp_create_app_2.png

Step 2

Do the form just like shown below.
pscrudform1.png

Step 3

Open the code view and add a name space to access MySQL Libraries.
  1. using

    MySql.Data.MySqlClient

    ;

Step 4

Write these codes for the connection between C# and MySQL Database. After that, declare and initialize all the variables and classes that are needed.

  1. MySqlConnection con =

    new

    MySqlConnection(

    "server=localhost;user id=root;password=;database=dbpeople;sslMode=none"

    )

    ;
  2. MySqlCommand cmd;
  3. MySqlDataAdapter da;
  4. DataTable dt;
  5. string

    sql;
  6. string

    peopleid;
  7. int

    result;

Step 5

Create a method for retrieving data in the database.
  1. private

    void

    loadData(

    )
  2. {
  3. try
  4. {
  5. sql =

    "SELECT PersonID, `FNAME` as 'Firstname', `LNAME` as 'Lastname', `ADDRESS` as 'Address' FROM `tblperson`"

    ;
  6. con.

    Open

    (

    )

    ;
  7. cmd =

    new

    MySqlCommand(

    )

    ;
  8. cmd.

    Connection

    =

    con;
  9. cmd.

    CommandText

    =

    sql;
  10. da =

    new

    MySqlDataAdapter(

    )

    ;
  11. da.

    SelectCommand

    =

    cmd;
  12. dt =

    new

    DataTable(

    )

    ;
  13. da.

    Fill

    (

    dt)

    ;
  14. dtg_list.

    DataSource

    =

    dt;


  15. txt_address.

    Clear

    (

    )

    ;
  16. txt_fname.

    Clear

    (

    )

    ;
  17. txt_lname.

    Clear

    (

    )

    ;

  18. btn_delete.

    Enabled

    =

    false

    ;
  19. btn_update.

    Enabled

    =

    false

    ;
  20. btn_save.

    Enabled

    =

    true

    ;



  21. }

    catch

    (

    Exception ex)
  22. {
  23. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  24. }
  25. finally
  26. {
  27. con.

    Close

    (

    )

    ;
  28. da.

    Dispose

    (

    )

    ;
  29. }

  30. }

Step 6

Create a method for saving data in the database.
  1. private

    void

    saveData(

    )
  2. {
  3. try
  4. {
  5. sql =

    "INSERT INTO `tblperson` (`FNAME`, `LNAME`, `ADDRESS`) VALUES ('"

    +

    txt_fname.

    Text

    +

    "','"

    +

    txt_lname .

    Text

    +

    "','"

    +

    txt_address .

    Text

    +

    "')"

    ;
  6. con.

    Open

    (

    )

    ;
  7. cmd =

    new

    MySqlCommand(

    )

    ;
  8. cmd.

    Connection

    =

    con;
  9. cmd.

    CommandText

    =

    sql;
  10. result =

    cmd.

    ExecuteNonQuery

    (

    )

    ;

  11. if

    (

    result >

    0

    )
  12. {
  13. MessageBox.

    Show

    (

    "Data has been saved in the database."

    , "Save"

    )

    ;
  14. }
  15. else
  16. {
  17. MessageBox.

    Show

    (

    "Failed to execute the query"

    , "error"

    )

    ;
  18. }

  19. }
  20. catch

    (

    Exception ex)
  21. {
  22. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  23. }
  24. finally
  25. {
  26. con.

    Close

    (

    )

    ;
  27. }

  28. }

Step 7

Create a method for updating data in the database.
  1. private

    void

    updateData(

    )
  2. {
  3. try
  4. {
  5. sql =

    "UPDATE `tblperson` SET `FNAME`='"

    +

    txt_fname.

    Text

    +

    "', `LNAME`='"

    +

    txt_lname.

    Text

    +

    "', `ADDRESS` ='"

    +

    txt_address.

    Text

    +

    "' WHERE PersonID="

    +

    peopleid;
  6. con.

    Open

    (

    )

    ;
  7. cmd =

    new

    MySqlCommand(

    )

    ;
  8. cmd.

    Connection

    =

    con;
  9. cmd.

    CommandText

    =

    sql;
  10. result=

    cmd.

    ExecuteNonQuery

    (

    )

    ;
  11. if

    (

    result >

    0

    )
  12. {
  13. MessageBox.

    Show

    (

    "Data has been updated in the database."

    , "Update"

    )

    ;
  14. }
  15. else
  16. {
  17. MessageBox.

    Show

    (

    "Failed to execute the query"

    , "error"

    )

    ;
  18. }

  19. }
  20. catch

    (

    Exception ex)
  21. {
  22. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  23. }
  24. finally
  25. {
  26. con.

    Close

    (

    )

    ;
  27. }

  28. }

Step 8

Create a method for deleting data in the database.
  1. private

    void

    deleteData(

    )
  2. {
  3. try
  4. {
  5. sql =

    "DELETE FROM `tblperson` WHERE PersonID="

    +

    peopleid;
  6. con.

    Open

    (

    )

    ;
  7. cmd =

    new

    MySqlCommand(

    )

    ;
  8. cmd.

    Connection

    =

    con;
  9. cmd.

    CommandText

    =

    sql;
  10. result=

    cmd.

    ExecuteNonQuery

    (

    )

    ;
  11. if

    (

    result >

    0

    )
  12. {
  13. MessageBox.

    Show

    (

    "Data has been deleted in the database."

    , "Delete"

    )

    ;
  14. }
  15. else
  16. {
  17. MessageBox.

    Show

    (

    "Failed to execute the query"

    , "error"

    )

    ;
  18. }

  19. }
  20. catch

    (

    Exception ex)
  21. {
  22. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  23. }
  24. finally
  25. {
  26. con.

    Close

    (

    )

    ;
  27. }

  28. }

Step 9

These codes are for saving data in the database when the button is clicked.
  1. private

    void

    btn_save_Click(

    object

    sender, EventArgs e)
  2. {
  3. saveData(

    )

    ;
  4. loadData(

    )

    ;
  5. }

Step 10

These codes are for retrieving data in the database when the button is clicked.

  1. private

    void

    btn_retrieve_Click(

    object

    sender, EventArgs e)
  2. {
  3. loadData(

    )

    ;
  4. }

Step 11

These codes are for updating data in the database when the button is clicked.
  1. private

    void

    btn_update_Click(

    object

    sender, EventArgs e)
  2. {
  3. updateData(

    )

    ;
  4. loadData(

    )

    ;
  5. }

Step 12

These codes are for deleting data in the database when the button is clicked.
  1. private

    void

    btn_delete_Click(

    object

    sender, EventArgs e)
  2. {
  3. deleteData(

    )

    ;
  4. loadData(

    )

    ;
  5. }

Step 13

These codes are for passing data from datagridview to the textboxes when the row of the datagridview is clicked
  1. private

    void

    dtg_list_Click(

    object

    sender, EventArgs e)
  2. {
  3. peopleid =

    dtg_list.

    CurrentRow

    .

    Cells

    [

    0

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  4. txt_fname.

    Text

    =

    dtg_list.

    CurrentRow

    .

    Cells

    [

    1

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  5. txt_lname .

    Text

    =

    dtg_list.

    CurrentRow

    .

    Cells

    [

    2

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  6. txt_address .

    Text

    =

    dtg_list.

    CurrentRow

    .

    Cells

    [

    3

    ]

    .

    Value

    .

    ToString

    (

    )

    ;

  7. btn_delete.

    Enabled

    =

    true

    ;
  8. btn_update.

    Enabled

    =

    true

    ;
  9. btn_save.

    Enabled

    =

    false

    ;

  10. }

Note: The database file is included inside the folder.
The complete source code is included. You can download it and run it on your computer
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top