Friday, March 16, 2012

Populating a Textbox

This seems like a simple thing to do, but I cannot figure it out.
How can I populate a textbox with a value retreived from a SQL database so that it is editable?

Thank you!There's no true data binding in ASP; you've got to write code to retrieve the value and more code to update it. The following code will retrieve the value. You'll also need an Update query to update the value.
Good luck
<code>

Dim oConnAsNew Data.SqlClient.SqlConnection(sConnect)

Dim oCmdAsNew Data.SqlClient.SqlCommand

Dim oDSAsNew DataSet

Dim oDAAsNew SqlClient.SqlDataAdapter

Try

With oCmd

.CommandText = "Select Value From Table1"

.Connection = oConn

EndWith

With oDA

.SelectCommand = oCmd

oConn.Open()

.Fill(oDS)

Textbox1.text =ods.Tables(0).Rows(0).Item(0)


EndWith
</code>


What kind of object are you storing your value in? Is it just a single value you are getting back from the database? If so you could use the ExecuteScalar routine to put it into a string and then assign it to a text box. If you have it in a datatable, you just have to assign the proper cell to the text box:

textBox.Text = myDataTable.Rows(rowCounter).Cells("'myField")


As Mr Jkcnack said that if you are retrieving single value use ExecuteScaler method if you are retieving multiple values use dataReader.

0 comments:

Post a Comment