Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding a text box control to data

Status
Not open for further replies.

asafb

Programmer
Jun 17, 2003
80
US
Hello, I was able to successfully bind combo boxes to data. Now I need your help again (jennifer? ;) ) Here's my code that works:

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
' Create a connection to the "pubs" SQL database located
' on the local computer.
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
Dim myCommand2 As SQlDataAdapter


' Connect to the SQL database using a SQL SELECT query to get
' all the data from the "Products" table.
myConnection = New SqlConnection("server=x;database=x;user id=x;pwd=x")
myCommand = New SqlDataAdapter("SELECT nameofschool FROM schools",myconnection)
myCommand2 = New SqlDataAdapter("SELECT name FROM healthclubs ",myconnection)


' Create and fill a DataSet.
Dim dsschools As Dataset = new DataSet()
Dim dshealth As Dataset = new DataSet()
myCommand.Fill(dsschools)
myCommand2.Fill(dshealth)

PriceBox.DataSource = dsschools
PriceBox.DataTextField = "nameofschool"
PriceBox.DataValueField = "nameofschool"
PriceBox.DataBind()
AgentNameBox.DataSource = dshealth
AgentNameBox.DataTextField = "name"
AgentNameBox.DataValueField = "name"
AgentNameBox.DataBind()

End Sub

</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<form>

<select id="PriceBox" style="FONT-SIZE: 8.6pt; FONT-FAMILY: Verdana" runat="server">
</select>
<select id="agentnamebox" style="FONT-SIZE: 8.6pt; FONT-FAMILY: Verdana" runat="server">
</select>
</form>
</body>
</html>
*******
Now I want to add a textbox control which will take the source from a sql statement like: select price from prices where id = 5.... thanks!
 
You wouldn't bind a textbox like you do a list or grid.

If you are looking for only one value then:

Create your command object (cmd) as you normally would.
intResult = cmd.ExecuteScalar
Textbox1.Text = intResult

If you are looking for more than one value try filling a datareader. Then read through it and fill the textbox:
DR = cmd.ExecuteReader

While DR.Read
If Not TypeOf DR("FieldName") is DBNull Then
Textbox1.Text = DR("FieldName")
End If
End While

DR.close


HTH

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top