Windows Forms: Insert Update Delete From Database in VB.NET

This post shows you how to Insert Update Delete From Database using Entity Framework in VB.NET Windows Forms Application.

To create a simple CRUD in VB.NET using Entity Framework, you can drag the TextBox, Button, Label and DataGridView from the visual studio toolbox to your winform, then you can layout your UI as shown below.

vb.net crud

Through the example, you will learn how to implement CRUD functionality with the Entity Framework in VB.NET.

Now, we will create the model EF by right-clicking on your project, then select Add->New Item->ADO.NET Entity Data Model. Next, select the Northwind database, then select the Customer table.

vb.net entity framework

You should add a bindingsource to the Customer class, then add the data binding to the TextBox and DataGridView controls.

Next, Declare the db variable allows you to access the DbContext.

Private db As NorthwindEntities

Add the load event handler to your form allow you to load the customer table into the DataGridView control.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    db = New NorthwindEntities()
    db.Customers.Load()
    CustomerBindingSource.DataSource = db.Customers.Local
End Sub

Add the click event handler to the Add button allow you to add new customer object.

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    CustomerBindingSource.AddNew()
End Sub

Next, Add the click event handler to the Delete button allows you to remove the customer object from the bindingsource.

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    If MessageBox.Show("Are you sure want to delete this records?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        CustomerBindingSource.RemoveCurrent()
    End If
End Sub

To save data into sql database, you only need to call the SaveChanges method.

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    db.SaveChanges()
    MessageBox.Show("Your data has been successfully saved !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

Finally, Add the click event handler to the Cancel button allow you to restore the customer object state as the following vb.net code.

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
    Dim changed = db.ChangeTracker.Entries().Where(Function(x) x.State <> EntityState.Unchanged).ToList()
    For Each obj In changed
        Select Case obj.State
            Case EntityState.Modified
                obj.CurrentValues.SetValues(obj.OriginalValues)
                obj.State = EntityState.Unchanged
            Case EntityState.Added
                obj.State = EntityState.Detached
            Case EntityState.Deleted
                obj.State = EntityState.Unchanged
        End Select
    Next
    CustomerBindingSource.ResetBindings(False)
End Sub

And don't forget to dispose your db variable when closing your windows forms application.

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    db.Dispose()
End Sub

Press F5 to build and run your project, So you have learned how to add, edit, delete data in sql database using vb.net programming language.

VIDEO TUTORIAL