---
title: "Getting value from SQL query to textbox"  
description: "Getting value from SQL query to textbox"  
author: "Anonymous User"  
published: 2014-08-28  
updated: 2023-07-06  
canonical: https://www.mindstick.com/forum/2183/getting-value-from-sql-query-to-textbox  
category: "vb.net"  
tags: ["vb.net"]  
reading_time: 3 minutes  

---

# Getting value from SQL query to textbox

```
Protected Sub Button3_Click(ByVal sender As Object, ByVal e
As System.EventArgs) Handles Button3.Click    Dim cons, query As String    Dim con As OdbcConnection    Dim adpt As OdbcDataAdapter    'Dim num As Integer    cons = "dsn=Courier;
UID=Courier; PWD=123;"    con = New OdbcConnection(cons)    con.Open()    query ="select Name from EMPLOYEE where EMPLOYEE_ID=" +DropDownList1.SelectedValue    Dim ds As DataSet    adpt = New OdbcDataAdapter(query, con)    ds = New DataSet    adpt.Fill(ds,"Courier")    ' TextBox1.Text =ds    con.Close()End Sub
```

I want to display the [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) of the [employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) in [Textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview) whoos ID is specified in [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver), what can I do for that?

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that. Here are the steps on how to get a value from a SQL query to a textbox in VB.NET:

1. Create a new Windows Forms project in VB.NET.
2. Add a TextBox control to the form.
3. In the code behind file, add the following code:

VB.Net

```plaintext
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Create a connection to the database.
    Dim connection As New SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True")

    ' Create a command object.
    Dim command As New SqlCommand("SELECT ProductName FROM Products WHERE ProductID = 1", connection)

    ' Open the connection.
    connection.Open()

    ' Execute the command.
    Dim reader As SqlDataReader = command.ExecuteReader()

    ' Get the value from the first row.
    If reader.Read() Then
        Dim productName As String = reader["ProductName"]
        TextBox1.Text = productName
    End If

    ' Close the connection.
    connection.Close()
End Sub
```

1. Run the project. When you click the button, the code in the `Button1_Click` event handler will be executed. The code will create a connection to the database, execute a SQL query, and get the value from the first row of the result set. The value will then be displayed in the TextBox control.

Here is an explanation of the code:

- The `SqlConnection` class is used to connect to a SQL database.
- The `SqlCommand` class is used to execute a SQL command.
- The `SqlDataReader` class is used to read the results of a SQL query.
- The `Read` method of the `SqlDataReader` class is used to read the next row of the result set.
- The `["ProductName"]` property of the `SqlDataReader` class returns the value of the `ProductName` column in the current row.

### Reply by Sumit Kesarwani

Hi Tanuj,

You should use DataRow but to answer your question, try this.

TextBox1.Text = ds.Tables(0).Rows(0)("Name").ToString()


---

Original Source: https://www.mindstick.com/forum/2183/getting-value-from-sql-query-to-textbox

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
