VB.NET 2010 How to Display Data from Database IN Textbox?

How To Display Data From Database In Textbox

There are more way to display data  from database in textbox but in this tutorial I will teach you a very simple  and easy method to get the required record from the database in vb.net.

To get the data from the database you should have the MS-Access 2016 or 2010 and VB.NET 2010 or later. If you not read my previous article vb.net program structure please read first.

Following are the basic step to get the required record from the database using vb.net.

  1. Create new MS-Access database.
  2. Create New table with the name of tbl_employee_info.
  3. Add the following field or column in tbl_employee_info table
How to Display Data from Database IN Textbox

     4. Enter the random date in your table your table data look like this.

How to Display Data from Database IN Textbox

VB.NET Connection String with MS-ACCESS 2016

After creating the database with table the next step is to create new project in your vb.net front end application.

Step 01:  Open you vb.net app and create new  project which you want.

Step 02: Click on the project menu and choose add Module and  writhe the name of your first module. Add following code in your module.

'How to Display Data from Database In TextBox
Module Mod_conn
Public rpt, dts As String
Public da As New OleDb.OleDbDataAdapter
Public cmd As OleDb.OleDbCommand
Public dr As OleDb.OleDbDataReader
Public sql As String

Public con As New OleDb.OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source =” & Application.StartupPath & “\db.accdb”)
End Module

Step 3 : Add 2nd module and copy the following and past it.

 

Imports System.Data.OleDb

Module Mod_connection

  Public conn As OleDbConnection

    Public constr As String = System.Environment.CurrentDirectory.ToString & "\db.accdb"

    Public Sub ConnDB()

        Try

            conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & constr & "")

            conn.Open()

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

End Module

Step 04 : Add win form and place the control from the tool box in the left side.

vb.net winform design

Stpe 05 : Add the following code in the search button for  get the required employee record from the database.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try
sql = "SELECT employee_name,designation,basic_pay,joining_date FROm tbl_employee_info Where employee_code = " & txtid.Text & ""
ConnDB()
cmd = New OleDbCommand(sql, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read = True Then
txtname.Text = dr(0)
txtdesignation.Text = dr(1)
txtpay.Text = dr(2)
mskjoining.Text = dr(3)




Exit Sub
Else
Return


End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Record Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
cmd.Dispose()
conn.Close()
End Try
End Sub

Step 06 : Enter the employee code in the employee code textbox and click on the search button. It will fetch the desired employee record and display in the textboxes.

How to Display Data from Database to TextBox

This Post Has 4 Comments

  1. click here

    Nice blog!
    Is your theme custom made or did you download it from
    somewhere?
    A design like yours with a few simple tweeks would really make my blog stand out.

    Please let me know where you got your design.
    Bless you

    1. gsstechnology

      I use elementor.3.3.1 pro plugin for page builder

  2. Nellie

    I feel this is one of the such a lot important information for me.
    And i’m happy studying your article. However should observation on few general issues,
    The site taste is great, the articles is in reality great :
    D. Good process, cheers

Leave a Reply