• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Update and Delete Record in the Database using C#

stiflas

Network Sniffer Expert
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
73
Likes
37
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
This lesson is a continuation of our last topic called “How to Save Record in Database using C#”. At this time we will be focusing on how to update and delete a record from the database using C#. To start with, this application. Open our project called “usermanagement”, then we need to add another control such as buttons, textbox and label. Then arrange all the objects like as shown below.

a1_1.png


Before we proceed on adding a code to our application, we need to understand first the process on how the application works. First the user we will click the “Load record” button, then all the records will be listed in the datagridview. Next when the user wants to update the a specific record, the user should click the specific record listed in the datagridview. And the user can observe that the “User ID” of a specific record, we display in the textbox like as shown below.

a2_1.png


This time we’re going to add functionality to our application by adding a code that will get the user id and put it into the textbox when the specific record has been click by the user. And here’s the following code:
  1. int

    i =

    e.

    RowIndex

    ;
  2. DataGridViewRow r =

    dataGridView1.

    Rows

    [

    i]

    ;

  3. int

    id =

    Convert.

    ToInt32

    (

    r.

    Cells

    [

    0

    ]

    .

    Value

    )

    ;
  4. txtuserID.

    Text

    =

    r.

    Cells

    [

    0

    ]

    .

    Value

    +

    ""

    ;

Next for the “Update Record” button, add the following code:

  1. //set our SQL UPDATE statement
  2. string

    sqlUpdate =

    "UPDATE tbluseraccounts set username ='"

    +

    txtname.

    Text

    +

    "' , userusername = '"

    +

    txtuser .

    Text

    +

    "', userpassword = '"

    +

    txtpass.

    Text

    +

    "', usertype= '"

    +

    txttype .

    Text

    +

    "' where userID = "

    +

    txtuserID .

    Text

    +

    ""

    ;
  3. try
  4. {

  5. //open the connection
  6. conn.

    Open

    (

    )

    ;
  7. //set the connection
  8. cmd.

    Connection

    =

    conn;
  9. //get the SQL statement to be executed
  10. cmd.

    CommandText

    =

    sqlUpdate;
  11. //execute the query
  12. cmd.

    ExecuteNonQuery

    (

    )

    ;
  13. //display a message
  14. MessageBox.

    Show

    (

    "Record Updated!...."

    )

    ;
  15. //close the connection
  16. conn.

    Close

    (

    )

    ;

  17. }
  18. catch

    (

    Exception ex)
  19. {
  20. //this will display some error message if something
  21. //went wrong to our code above during execution
  22. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  23. }
  24. //we call the loadrecord() function after adding a new record
  25. loadrecord(

    )

    ;

And for the “Delete Record” button, add the following code:

  1. OleDbCommand cmd =

    new

    OleDbCommand(

    )

    ;
  2. //set our SQL DELETE statement
  3. string

    sqlUpdate =

    "Delete * from tbluseraccounts where userID= "

    +

    txtuserID.

    Text

    +

    ""

    ;
  4. try
  5. {

  6. //open the connection
  7. conn.

    Open

    (

    )

    ;
  8. //set the connection
  9. cmd.

    Connection

    =

    conn;
  10. //get the SQL statement to be executed
  11. cmd.

    CommandText

    =

    sqlUpdate;
  12. //execute the query
  13. cmd.

    ExecuteNonQuery

    (

    )

    ;
  14. //display a message
  15. MessageBox.

    Show

    (

    "Record Deleted!...."

    )

    ;
  16. //close the connection
  17. conn.

    Close

    (

    )

    ;

  18. }
  19. catch

    (

    Exception ex)
  20. {
  21. //this will display some error message if something
  22. //went wrong to our code above during execution
  23. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  24. }
  25. //we call the loadrecord() function after adding a new record
  26. loadrecord(

    )

    ;

If you have observed, our code in “Save Record”, “Update Record” and “Delete Record” button is similar except the query. Because when you are doing the saving of record you are Inserting a new record, and if you are modifying a record you are updating a record as well as in the deleting of record.

Here’s all the code used in this application:
  1. using

    System

    ;
  2. using

    System.Collections.Generic

    ;
  3. using

    System.ComponentModel

    ;
  4. using

    System.Data

    ;
  5. using

    System.Drawing

    ;
  6. using

    System.Linq

    ;
  7. using

    System.Text

    ;
  8. using

    System.Windows.Forms

    ;
  9. using

    System.Data.OleDb

    ;

  10. namespace

    WindowsFormsApplication1
  11. {
  12. public

    partial

    class

    Form1 :

    Form
  13. {
  14. //declare new variable named dt as New Datatable
  15. DataTable dt =

    new

    DataTable(

    )

    ;
  16. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  17. static

    string

    connection =

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "

    +

    Application .

    StartupPath

    +

    "/usermgt.mdb"

    ;
  18. OleDbConnection conn =

    new

    OleDbConnection(

    connection)

    ;

  19. public

    Form1(

    )
  20. {

  21. InitializeComponent(

    )

    ;

    // calling the function
  22. }

  23. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  24. {

  25. }
  26. private

    void

    loadrecord(

    )
  27. {
  28. dt =

    new

    DataTable(

    )

    ;
  29. string

    sql =

    "Select * from tbluseraccounts"

    ;
  30. OleDbDataAdapter da =

    new

    OleDbDataAdapter(

    sql , conn)

    ;
  31. da.

    Fill

    (

    dt)

    ;
  32. dataGridView1.

    DataSource

    =

    dt;
  33. }

  34. private

    void

    button1_Click(

    object

    sender, EventArgs e)
  35. {
  36. loadrecord(

    )

    ;
  37. }

  38. private

    void

    btninsert_Click(

    object

    sender, EventArgs e)
  39. {

  40. OleDbCommand cmd =

    new

    OleDbCommand(

    )

    ;
  41. //set our SQL Insert INTO statement
  42. string

    sqlInsert =

    "INSERT INTO tbluseraccounts ( username, userusername, userpassword, usertype ) VALUES('"

    +

    txtname.

    Text

    +

    "','"

    +

    txtuser.

    Text

    +

    "','"

    +

    txtpass.

    Text

    +

    "','"

    +

    txttype.

    Text

    +

    "')"

    ;
  43. try
  44. {

  45. //open the connection
  46. conn.

    Open

    (

    )

    ;
  47. //set the connection
  48. cmd.

    Connection

    =

    conn;
  49. //get the SQL statement to be executed
  50. cmd.

    CommandText

    =

    sqlInsert;
  51. //execute the query
  52. cmd.

    ExecuteNonQuery

    (

    )

    ;
  53. //display a message
  54. MessageBox.

    Show

    (

    "New Record Added!...."

    )

    ;
  55. //close the connection
  56. conn.

    Close

    (

    )

    ;

  57. }
  58. catch

    (

    Exception ex)
  59. {
  60. //this will display some error message if something
  61. //went wrong to our code above during execution
  62. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  63. }
  64. //we call the loadrecord() function after adding a new record
  65. loadrecord(

    )

    ;


  66. }



  67. private

    void

    dataGridView1_CellContentClick(

    object

    sender, DataGridViewCellEventArgs e)
  68. {
  69. int

    i =

    e.

    RowIndex

    ;
  70. DataGridViewRow r =

    dataGridView1.

    Rows

    [

    i]

    ;

  71. int

    id =

    Convert.

    ToInt32

    (

    r.

    Cells

    [

    0

    ]

    .

    Value

    )

    ;
  72. txtuserID.

    Text

    =

    r.

    Cells

    [

    0

    ]

    .

    Value

    +

    ""

    ;

  73. }

  74. private

    void

    btnupdate_Click(

    object

    sender, EventArgs e)
  75. {
  76. OleDbCommand cmd =

    new

    OleDbCommand(

    )

    ;
  77. //set our SQL Insert INTO statement
  78. string

    sqlUpdate =

    "UPDATE tbluseraccounts set username ='"

    +

    txtname.

    Text

    +

    "' , userusername = '"

    +

    txtuser .

    Text

    +

    "', userpassword = '"

    +

    txtpass.

    Text

    +

    "', usertype= '"

    +

    txttype .

    Text

    +

    "' where userID = "

    +

    txtuserID .

    Text

    +

    ""

    ;
  79. try
  80. {

  81. //open the connection
  82. conn.

    Open

    (

    )

    ;
  83. //set the connection
  84. cmd.

    Connection

    =

    conn;
  85. //get the SQL statement to be executed
  86. cmd.

    CommandText

    =

    sqlUpdate;
  87. //execute the query
  88. cmd.

    ExecuteNonQuery

    (

    )

    ;
  89. //display a message
  90. MessageBox.

    Show

    (

    "Record Updated!...."

    )

    ;
  91. //close the connection
  92. conn.

    Close

    (

    )

    ;

  93. }
  94. catch

    (

    Exception ex)
  95. {
  96. //this will display some error message if something
  97. //went wrong to our code above during execution
  98. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  99. }
  100. //we call the loadrecord() function after adding a new record
  101. loadrecord(

    )

    ;
  102. }

  103. private

    void

    btndelteRecord_Click(

    object

    sender, EventArgs e)
  104. {
  105. OleDbCommand cmd =

    new

    OleDbCommand(

    )

    ;
  106. //set our SQL DELETE statement
  107. string

    sqlUpdate =

    "Delete * from tbluseraccounts where userID= "

    +

    txtuserID.

    Text

    +

    ""

    ;
  108. try
  109. {

  110. //open the connection
  111. conn.

    Open

    (

    )

    ;
  112. //set the connection
  113. cmd.

    Connection

    =

    conn;
  114. //get the SQL statement to be executed
  115. cmd.

    CommandText

    =

    sqlUpdate;
  116. //execute the query
  117. cmd.

    ExecuteNonQuery

    (

    )

    ;
  118. //display a message
  119. MessageBox.

    Show

    (

    "Record Deleted!...."

    )

    ;
  120. //close the connection
  121. conn.

    Close

    (

    )

    ;

  122. }
  123. catch

    (

    Exception ex)
  124. {
  125. //this will display some error message if something
  126. //went wrong to our code above during execution
  127. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  128. }
  129. //we call the loadrecord() function after adding a new record
  130. loadrecord(

    )

    ;
  131. }




  132. }
  133. }

You can now test the application by pressing the “F5” or the start button.


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

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,821

349,831

Top