In this article I will explain with an example, how to retrieve data from Database and display in TextBox in Windows Application (WinForms) using C# and VB.Net.
This article will illustrate how to fetch records (data) from SQL Server Database using SqlDataReader and then set the retrieved values in Windows Application (WinForms) using C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Retrieve data from Database and display in TextBox in Windows Application using C# and VB.Net
 
I have already inserted few records in the table.
Retrieve data from Database and display in TextBox in Windows Application using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Form Design
The Form consists of an HTML Table consisting of three TextBoxes.
Retrieve data from Database and display in TextBox in Windows Application using C# and VB.Net
 
Namespaces
You will need to import the following namespace.
C#
using System.Data.SqlClient;
 
VB.Net
Imports System.Data.SqlClient
 
 
Populating TextBoxes from Database in Windows Application using C# and VB.Net
Inside the Form Load event, the Customers Table is queried to fetch the data for Customer Id 2. The record is fetched using SqlDataReader and then each column value is assigned to the respective TextBoxes.
C#
private void Form1_Load(object sender, EventArgs e)
{
    string constr = @"Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security=true";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers WHERE CustomerId = 2"))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                sdr.Read();
                txtCustomerId.Text = sdr["CustomerId"].ToString();
                txtName.Text = sdr["Name"].ToString();
                txtCountry.Text = sdr["Country"].ToString();
            }
            con.Close();
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim constr As String = "Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security=true"
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers WHERE CustomerId = 2")
            cmd.CommandType = CommandType.Text
            cmd.Connection = con
            con.Open()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                sdr.Read()
                txtCustomerId.Text = sdr("CustomerId").ToString()
                txtName.Text = sdr("Name").ToString()
                txtCountry.Text = sdr("Country").ToString()
            End Using
            con.Close()
        End Using
    End Using
End Sub
 
 
Screenshot
Retrieve data from Database and display in TextBox in Windows Application using C# and VB.Net
 
 
Downloads