In this article I will explain with an example, how to retrieve data from Database and set the value in TextBox in ASP.Net 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 TextBoxes in ASP.Net 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 set in TextBox in ASP.Net using C# and VB.Net
 
I have already inserted few records in the table.
Retrieve data from Database and set in TextBox in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The following HTML Markup consists of an HTML Table consisting of three TextBoxes.
<table border="0" cellpadding="5" cellspacing="0">
<tr>
    <td>Customer Id</td>
    <td><asp:TextBox ID = "txtCustomerId" runat = "server" /></td>
</tr>
<tr>
    <td>Name</td>
    <td><asp:TextBox ID = "txtName" runat = "server" /></td>
</tr>
<tr>
    <td>Country</td>
    <td><asp:TextBox ID = "txtCountry" runat = "server" /></td>
</tr>
</table>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Populating TextBoxes from Database using DataReader in ASP.Net
Inside the Page 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#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        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
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        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 If
End Sub
 
 
Screenshot
Retrieve data from Database and set in TextBox in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads