WHAT'S NEW?
Loading...

Add new fields in Visual basic.net in existing database with databinding approach

Considered you have an existing database with the column name First name, Last name, and Middle name.
Using the databiding approach in Visual Basic.Net to connect your database source.
Here are the following steps:
1.   On your Solution Explorer click Data Source. If the Data Source is hidden, go to the Data in the menu above and click Add new DataSource.
2.   Click database and connection string. Make sure to used Microsoft Access Database file because we are using OLE DB. Finally browse you file in directory of your computer where the file save.
3.   Make sure that your database is close while trying to test your connection, when it happen, the error dialog box appear.
4.  Click continue and proceed to OK.
5.  While trying to next, another dialog box appear that ask if you want to copy your data file to your project. If YES, the new location of your database copied to the directory of your project and it is located to your bin directory in debug folder. If NO, the current directory will obtain your connection and data once the new records added. 
6.   After completed that section, check the box of your table which table do you want to used or check all.
7.   And now new database added to your solution explorer.
8.   Some common problem encountered while saving their input data is the lost of data in database once the program is close.
9.   Configure your database connection. In solution explorer, click your database name, the yellow  icon on your database. Once the yellow icon is click, go to your properties window and copy to output: copy if newer. With that, once the new data added to your database, the consistency will obtain every time you save new data to your database.
Now, if you want to add new field or column in your database. Follow the steps:
1.   In your project name of your folder, we have 2 databases copied. First, it is in your project name and the other is in your debug directory in bin folder.
2.   In your project name folder, that's the local copy of your database where you can modify and changes affects to your database. If any changes in your database on your project folder name, the update database will update on your database in debug directory in bin folder.
3.   In debug folder. That's the location of your database where the new data are added.
4.   Now, adding new column in your database is quite simple but you have to memorized the way to configured.
5.   First, in your project name folder, locate your database name and double click. In your design view. Add new field or column the name of your column. Example "address".
6.   Try to fill address in your datasheet view.
7.   Next, in your solution explorer. Double click your DataSet.xsd.
8.   In your DataSet table, right click and add new column. Let say "address".
9.   After adding new column which is address. Right click on your TableAdapter and configure your query.
10. In SELECT statement. Specify which fields do you want to retrieve.  In last section of your SELECT before FROM place comma (,) and your column name address.
11. If you want to see the output. Try to click query builder and execute query. Next until it finished. Note: do not change the fill method to maintain the fill data connection.
12. In you form, add new label and text from your toolbox. This textbox you are going to bind, the name is address base in your column name and change the label name to address in order to recognize.
13. Now, you are going to bind your textbox address. Click the textbox you are going to bind, then locate the properties settings. In DataBindings, click the plus sign to expand. Click the Text in order to bind and click the plus sign in binding source and click which column you want to bind. In our example click address
14. Finally, run your program. The new record filled your textbox address.
15. If you notice, the database on your project name folder will copy the new column and data on your debug folder.

How to add, delete, update records in Visual Basic


I used unbound approach to connect my database where you may able add,delete,update and load all records from database. I used OLEDBConnection,OLEDBDataAdapter, and OLEDBCommandBuilder to process all transaction, any way this is an old approach in Visual  Basic. Hopefully this is very helpful to everyone!

Good Luck.....

Here are the Sample Codes:

'-----------------This is Form1----------------------

'----------------------------------------------------

Public Class Form1

    'Public variables
    Dim max As Integer
    Dim inc As Integer

    Dim con As New OleDb.OleDbConnection
    Dim dbprovider As String
    Dim dbsource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim binding As BindingSource

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        btnadd.Focus()
        txtlastname.Enabled = False
        txtfirstname.Enabled = False
        txtmiddlename.Enabled = False
        btnsave.Enabled = False
        btnupdate.Enabled = False
        'you can used method name to call your connection everytime you connect your database
        '-------Private Sub myconnection()
        '-------con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/data/dbinfo.mdb;")
        '-------con.Open()
        dbprovider = "PROVIDER = Microsoft.Jet.OLEDB.4.0;"
        dbsource = "Data Source = ../Data/Info.mdb"
        con.ConnectionString = dbprovider & dbsource
        con.Open()

        sql = "SELECT * FROM tblinfo"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Info")

        txtlastname.Text = ds.Tables("Info").Rows(inc).Item("Lastname")
        txtfirstname.Text = ds.Tables("Info").Rows(inc).Item("Firstname")
        txtmiddlename.Text = ds.Tables("Info").Rows(inc).Item("Middlename")
        txtID.Text = ds.Tables("Info").Rows(inc).Item("ID")
        max = ds.Tables("Info").Rows.Count
        inc = -1

    End Sub
    Private Sub NavigateRecords() 'navigate records from my database

        txtlastname.Text = ds.Tables("Info").Rows(inc).Item("Lastname")
        txtfirstname.Text = ds.Tables("Info").Rows(inc).Item("Firstname")
        txtmiddlename.Text = ds.Tables("Info").Rows(inc).Item("Middlename")
        txtID.Text = ds.Tables("Info").Rows(inc).Item("ID")

    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

        If inc <> max - 1 Then
            inc = inc + 1
            NavigateRecords() 'this is where i call my method name to navigate records
        Else
            MsgBox("No Records Yet")
        End If

    End Sub
    Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevious.Click

        If inc > 0 Then
            inc = inc - 1
            NavigateRecords() 'this is where i call my method name to navigate records
        ElseIf inc = -1 Then
            MsgBox("No Records Yet")
        ElseIf inc = 0 Then
            MsgBox("First Row")
        End If

    End Sub
    Private Sub btnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlast.Click

        If inc <> max - 1 Then
            inc = max - 1
            NavigateRecords() 'this is where i call my method name to navigate records
        ElseIf MsgBox("No more records") Then
            MsgBox("Last Record")
        End If

    End Sub
    Private Sub btnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfirst.Click

        If inc <> 0 Then
            inc = 0
            NavigateRecords() 'this is where i call my method name to navigate records
        ElseIf MsgBox("No more records") Then
            MsgBox("You are in the first record")
        End If

    End Sub
    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

        'add new records to clear all textbox in order to add new entery
        'use enabled true/false to handled process

        txtlastname.Enabled = True
        txtfirstname.Enabled = True
        txtmiddlename.Enabled = True
        txtlastname.Clear()
        txtfirstname.Clear()
        txtmiddlename.Clear()
        txtlastname.Focus()

        btnedit.Enabled = False
        btndelete.Enabled = False
        btnsave.Enabled = True
        btnadd.Enabled = False

    End Sub
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

        'save command
        'you can used this command:
        '-----------sql = "INSERT INTO tblinfo(Firstname,Lastname) VALUES('" & txtfirstname.Text & "','" & txtlastname.Text & "')"
        '-----------Command = New OleDb.OleDbCommand(sql, con)
        '-----------count = Command.ExecuteNonQuery
        '-----------MessageBox.Show(count & " record added to your database")

        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsnewrow As DataRow

        If txtfirstname.Text = "" Or txtlastname.Text = "" Then
            MsgBox("Please fill up the textbox to continue your transaction", MsgBoxStyle.Exclamation, "Fill the textboxes")
            txtlastname.Focus()
            Exit Sub

        End If
        dsnewrow = ds.Tables("Info").NewRow()
        dsnewrow.Item("Lastname") = txtlastname.Text
        dsnewrow.Item("Firstname") = txtfirstname.Text
        dsnewrow.Item("Middlename") = txtmiddlename.Text
        ds.Tables("Info").Rows.Add(dsnewrow)

        da.Update(ds, "Info")

        MsgBox("New Record Added to your database", MsgBoxStyle.Information)

        If MsgBox("Do you want another transaction to Save?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then

            btnadd.Enabled = True
            btnsave.Enabled = False
            txtfirstname.Clear()
            txtlastname.Clear()
            txtmiddlename.Clear()

            Exit Sub
        Else
            txtfirstname.Clear()
            txtlastname.Clear()
            txtmiddlename.Clear()
            btnadd.Enabled = False
            btnsave.Enabled = True
            btnedit.Enabled = True
            btndelete.Enabled = True

        End If
    End Sub
    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click

        'enabled true to change/modify your records
        txtlastname.Enabled = True
        txtfirstname.Enabled = True
        txtmiddlename.Enabled = True
        If txtlastname.Text = "" Or txtfirstname.Text = "" Then

            MsgBox("You could not edit because the textbox is empty", MsgBoxStyle.Exclamation)

            btnupdate.Enabled = False
            btndelete.Enabled = True
            txtlastname.Enabled = False
            txtfirstname.Enabled = False
            txtmiddlename.Enabled = False

        Else

            txtlastname.Focus()
            btnupdate.Enabled = True
            btnedit.Enabled = False
            btnadd.Enabled = False
            btndelete.Enabled = False

        End If
    End Sub
    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click

        'this line of code enabled you to update the record(s)

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("Info").Rows(inc).Item("Lastname") = txtlastname.Text
        ds.Tables("Info").Rows(inc).Item("Firstname") = txtfirstname.Text
        ds.Tables("Info").Rows(inc).Item("Middlename") = txtmiddlename.Text

        da.Update(ds, "Info")

        MsgBox("New Record(s)updated", MsgBoxStyle.Information)

        btnedit.Enabled = True
        btnupdate.Enabled = False
        btnadd.Enabled = True

    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click

        'this line of code to delete the records from your database

        txtlastname.Enabled = True
        txtfirstname.Enabled = True
        txtmiddlename.Enabled = True
        btnedit.Enabled = False
        btnadd.Enabled = False

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        If txtfirstname.Text = "" Or txtmiddlename.Text = "" Then
            MsgBox("Could not delete because the textbox is empty, Please navigate the record to find what you are going to delete", MsgBoxStyle.Critical)

            btnedit.Enabled = True
            btnadd.Enabled = True
            txtlastname.Enabled = False
            txtfirstname.Enabled = False
            txtmiddlename.Enabled = False

            Exit Sub
        End If

        If MsgBox("Do you really want to delete this record?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then

            MsgBox("Cancelled Opereation", MsgBoxStyle.Information)

            btnedit.Enabled = True
            btnadd.Enabled = True
            txtlastname.Enabled = False
            txtfirstname.Enabled = False
            txtmiddlename.Enabled = False

            Exit Sub

        End If

        If txtfirstname.Text = "" Or txtmiddlename.Text = "" Then
            MsgBox("Could not delete because the textbox is empty, Please navigate the record to find what you are going to delete", MsgBoxStyle.Critical)

            btnedit.Enabled = True
            btnadd.Enabled = True
        Else
            ds.Tables("Info").Rows(inc).Delete()

            max = max - 1

            inc = 0

            NavigateRecords()

            da.Update(ds, "Info")
            MsgBox("Record(s) deleted", MsgBoxStyle.Information)

            btnedit.Enabled = True
            btnadd.Enabled = True

        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        If MsgBox("Do you want to exit the system?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then

            End
        Else

            MsgBox("The System will remain to served your request", MsgBoxStyle.Information, "System Remain")

        End If
    End Sub

    Private Sub BtnShowRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowRecords.Click

        Form2.Show() 'this line of code will show the next form and load all records from the database

    End Sub
End Class

This is the next form I create to show all records

'-----------------This is Form2----------------------
'----------------------------------------------------
Public Class Form2
    Dim max As Integer
    Dim inc As Integer

    Dim con As New OleDb.OleDbConnection
    Dim dbprovider As String
    Dim dbsource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim binding As BindingSource


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'this line of code automatically load the records once the program run or click show record button

        dbprovider = "PROVIDER = Microsoft.Jet.OLEDB.4.0;"
        dbsource = "Data Source = ../Data/Info.mdb"
        con.ConnectionString = dbprovider & dbsource
        con.Open()
        sql = "SELECT * FROM tblinfo"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Info")
        Try
            txtID.Text = ds.Tables("Info").Rows(0).Item("ID")
            txtlastname.Text = ds.Tables("Info").Rows(1).Item("Lastname")
            txtfirstname.Text = ds.Tables("Info").Rows(2).Item("Firstname")
            txtmiddlename.Text = ds.Tables("Info").Rows(3).Item("Middlename")
            txtID.Text = ds.Tables("Info").Rows(inc).Item("ID")
            DataGridView1.DataSource = ds.Tables("Info")

            max = ds.Tables("Info").Rows.Count
            Label5.Text = ds.Tables("Info").Rows.Count & " Number of Rows"
            inc = -1
        Catch ex As Exception
            MsgBox("No Rows")
        End Try
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        'this line of code enabled to click datagridview cells row
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(0).Value) Then
            txtID.Text = DataGridView1.CurrentRow.Cells(0).Value
        Else
            txtID.Text = ""
        End If
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(1).Value) Then
            txtlastname.Text = DataGridView1.CurrentRow.Cells(1).Value
        Else
            txtlastname.Text = ""
        End If
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(2).Value) Then
            txtfirstname.Text = DataGridView1.CurrentRow.Cells(2).Value
        Else
            txtfirstname.Text = ""
        End If
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(3).Value) Then
            txtmiddlename.Text = DataGridView1.CurrentRow.Cells(3).Value
        Else
            txtmiddlename.Text = ""
        End If
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If MsgBox("Do you want to exit the system?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then
            Me.Close()
        Else
            MsgBox("The System will remain to served your request", MsgBoxStyle.Information, "System Remain")
        End If
    End Sub
End Class

Note: This is an unbound approach of Visual Basic. If you want to make it fast by making a simple or complex program I suggest to used Visual Basic.Net where you can create a program in a minute. There's a lot of function you may used, it depend upon your choice.