How to insert update & delete in VB.NET

Written by stephen byron cooper Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to insert update & delete in VB.NET
Create Visual Basic programs in Visual Studio. (Jupiterimages/ Images)

Visual Basic is a handy programming tool for creating desktop applications for the Windows environment. It is part of a range of programming tools that are tied together by Microsoft’s ASP.NET. You will have to set up a database and connect to it in your VB program before you can insert update or delete records from tables. MS Access is a companion application to Visual Basic and the two utilities integrate well. You will prepare your data in a “DataSet” within the VB environment and these will not pass into the database until you “commit” the records from the DataSet into the database.

Skill level:


  1. 1

    Create a DataSet to hold your records form the database. Also create a Data Adapter to fill the DataSet and also to commit your DataSet records back to the database. Create a DataSet, in this case called “daset” with the code: Dim daset As New DataSet. Define a connection name and a select statement to read in the contents of your database. Create a Data Adapter, called “dada” and pass that to your DataSet variable. You should read a table from the database into the DataSet. For this example, assume the table is called “PhoneBook” and contains two columns, “FriendName” and “PhoneNum.” Also create two Integer variables, one as an index on the DataSet, called “ind” and a one to how the total number of rows in the DataSet, called “MaxRow.”

  2. 2

    Create four buttons on your form to access data in the application you are creating. As an exercise, call these “btnAddRec” (labelled “Insert”), “btnUpdRec” (labelled “Update”), “btnDelRec” (labelled “Delete”) and “btnComRec” (labelled “Commit”). You might also want to add a “Cancel” button to enable the user to get out of form. Assume that the form has two text fields that correspond to the columns in the table and are called “FullName” and “TelNo.”

  3. 3

    Create a function to add to your btnAddRec button with the following code: Dim dasetNewRow As DataRow dasetNewRow = daset.Tables("PhoneBook").NewRow() dasetNewRow.Item("FriendName") = FullName.Text dasetNewRow.Item("PhoneNum") = Telno.Text daset.Tables("PhoneBook").Rows.Add(dasetNewRow) MaxRow = MaxRow + 1 The last line increases the number recording how many records are in the DataSet.

  4. 4

    Create a function to add to your btnUpdRec. You can only update an existing record and so you would have already read the data of a record into the form for display. At this point, the index number you use for your row numbers will have been left at the current row number. For this exercise, say that index is called “ind”. Update the current record in the DataSet daset with the values currently held in the two text boxes called “FullName” and “TelNo” with the following two lines: daset.Tables("PhoneBook").Rows(ind).Item(1) = FullName.Text daset.Tables("PhoneBook").Rows(ind).Item (2) = TelNo.Text

  5. 5

    Create a function to add to your btnDelRec to delete the current record. The record number should be stored in the variable “ind.” You should also have stored a count of the total number of records in your DataSet and this should be decreased to account for the deleted record. daset.Tables("PhoneBook").Rows(inc).Delete() MaxRows = MaxRows – 1

  6. 6

    Create a function for btnComRec to copy values from the DataSase into the database. The variable daset is the array forming the DataSet and dada is the Data Adapter that links to the database. dada.Update(daset, "PhoneBook")

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.