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 of the employee in Textbox whoos ID is specified in query, what can I do for that?
Aryan Kumar
06-Jul-2023Sure, 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:
VB.Net
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:
SqlConnection
class is used to connect to a SQL database.SqlCommand
class is used to execute a SQL command.SqlDataReader
class is used to read the results of a SQL query.Read
method of theSqlDataReader
class is used to read the next row of the result set.["ProductName"]
property of theSqlDataReader
class returns the value of theProductName
column in the current row.Sumit Kesarwani
28-Aug-2014You should use DataRow but to answer your question, try this.
TextBox1.Text = ds.Tables(0).Rows(0)("Name").ToString()