TextBox and SQL Data

D

dave

Hi

Could someone please show me an example (vb pref) of how the hell you get
data into a text box??

I have a sub that is called on the page_load which connects to a DB and
returns one row of data, which i then want to use to populate the textboxes
on a form (details editing page).

Everything i have tried doesnt seem to work??

Here is the code for getting the data:

Sub BindUserData( )
Dim myConnection as New
SqlConnection(ConfigurationSettings.AppSettings("DBconnString"))
dim strSQL as String = "SELECT * FROM dbo.USERS WHERE dbo.USERS.ID = 1"
Dim myCommand as New SqlCommand(strSQL, myConnection)
Dim myDA as New SqlDataAdapter()
myDA.SelectCommand = myCommand
Dim myDS as New DataSet()
myDA.Fill(myDS)
End Sub


Also what is the best thing to use? Datareader or Dataset or Dataview, etc

Thanks in advance....
 
A

Axel Dahmen

Hi, Dave,

this is your mistake: In your code there is not a single line assigning the
data you're reading to any TextBox. That's what's missing. You just filled
an array (the DataTable) with DB data and left it there.

To copy the array's contents into any TextBox, just write something like:

Dim txtBox As System.Web.UI.WebControls.TextBox ' created by VS.NET

...

txtBox.Text = myDS.Tables(0).Rows(0)(0) ' put this into the PageLoad
function

HTH,
Axel Dahmen
 
D

dave

Thank Axel

I just didnt post the textbox assignment code i was trying in my previous
post, as that was the part i couldnt get working properly.

Thanks again,

What is the advantage/disadvantage between using a datareader, or a dataset
to populate edits, etc. Obv it is better to use a dataset when populating a
datagrid, but is there any benefits of using it to return a single record to
populate the edits?

Cheers
 
A

Axel Dahmen

My personal favoured is the DataReader as at any time it only contains one
single data row whereas the DataTable contains the whole result set, which
is quite memory and time consumptive, particularly on big result sets.

The only drawback on DataReaders is that you can only use one per
connection, which might become a programming challenge here and there.

Best wishes,
Axel Dahmen

-------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top