Groups | Blog | Home
all groups > visual studio .net general > may 2006 >

visual studio .net general : How to display DataSet field in TextBox?


kvr901
5/28/2006 12:10:02 AM
I created a DataSet in VS2005. I want to fill a bunch of TextBoxes on a
Webform with the data, but for now will settle on 1 field of 1 record in 1
TextBox. If I can accomplish that, then I should be able to do the rest.
(Datagrids work fine with the dataset, but want to use TextBoxes).



The fieldname of the data I want is "fldLastName". What do I need to do to
the below sub? Thanks!


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim adapter As MyTestDataSet1TableAdapters.TestTable1TableAdapter = New
MyTestDataSet1TableAdapters.TestTable1TableAdapter()

Me.TextBox1.Text =
End Sub

pvdg42
5/28/2006 4:21:48 PM

[quoted text, click to view]
Study the class hierarchy under the DataSet class. What you have is a series
of collections that will allow you to drill down to the field you wish to
display in a given text box.
There is (potentially) a collection of DataTable objects in a DataSet,
depending on the query(ies) used to populate the DataSet.
Within each DataTable, there is a DataRow collection. Each row represents a
record in the DataTable. There is also a DataColumn class that allows you to
identify the column in a given row to display.
Use these classes to isolate the field you want displayed.

kvr901
5/30/2006 9:32:01 AM
Thank you for your reply, but I am so lost at this point that I need some
help with basics. For now, I have given up on datasets, etc., and am trying
to use one "in-line" SQL statement to populate 3 textboxes.

I created a simple table named "TestTable1" with 3 fields.
Column Name Type
fldPersonID int
fldFirstName nvarchar(50)
fldLastName nvarchar(50)

There are 3 records:
1 GeorgeBush
2 Bill

What I have so far:

Dim testDataSource As New SqlDataSource()
testDataSource.ConnectionString =
ConfigurationManager.ConnectionStrings("TestDataConnectionString1").ToString()


testDataSource.SelectCommandType = SqlDataSourceCommandType.Text
testDataSource.SelectCommand = "SELECT fldPersonID, fldFirstName,
fldLastName FROM TestTable1"


Me.txtboxPersonID.Text =
Me.txtboxFirstName.Text =
Me.txtboxLastNameName.Text =
kvr901
5/30/2006 9:44:01 AM
Please disregard my previous reply. I accidently posted it before finishing
it, and I can't find a way to edit it.



Thank you for your reply, but I am so lost at this point that I need some
help with basics. For now, I have given up on datasets, etc., and am trying
to use one "in-line" SQL statement to populate 3 textboxes.

I created a simple table named "TestTable1" with 3 fields.
Column Name Type
fldPersonID int
fldFirstName nvarchar(50)
fldLastName nvarchar(50)

There are 3 records:
1 George Bush
2 Bill Clinton
3 Ronald Reagan




At this point in time I don't care about parameters. I don't care which
record
is displayed in the textboxes. I just want to see how to get the data into
a textbox.

I have searched the net for days, but all I find is how to populate datagrids,
dataviews, datareaders, etc. I want my data in textboxes.

My connection string works fine, and I can query the data fine using
datagrids.


Any help would be appreciated.



What I have so far:

Dim testDataSource As New SqlDataSource()
testDataSource.ConnectionString =
ConfigurationManager.ConnectionStrings("TestDataConnectionString1").ToString()


testDataSource.SelectCommandType = SqlDataSourceCommandType.Text
testDataSource.SelectCommand = "SELECT fldPersonID, fldFirstName,
fldLastName FROM TestTable1"


Me.txtboxPersonID.Text =
Me.txtboxFirstName.Text =
Me.txtboxLastNameName.Text =


kvr901
5/31/2006 7:37:02 AM
Perhaps I have not phrased my questions properly.

I might use datasets rather than the SQLDataSource (at this point I don't
know).

Regardless of what I use, my main question is:
How do I get the field data (be it from a SQL statement, or a stored
procedure, etc) into a TextBox? For the sake of learning I can hard-code
parameters in the query, or even have only one record in the table. Every
example I have seen is related to databound controls like Listboxes,
datagrids, etc.

TextBox1.Text = (what does it equal?) (how to I put the data here?)

TextBox1.Text = DataSet.fieldname?
TextBox1.Text = DataSet.Row.Fieldname?
TextBox1.Text = Some other type of syntax?

And are there examples anywhere? I have seen many examples using TextBoxes
to enter data that will be updated to a table, but I have not seen any
examples where the TextBox is used to display data.

I am not trying to display multiple records, so I am not interested in
datagrids.

Eventually (BUT NOT NOW) I plan to have multiple TextBoxes and multiple
DropDowns on a webform (probably 30 to 50 total) from which the user will
read, and edit data. Right now all I need to figure out is how to put data
in a single query (or table) field into a TextBox. I can write the VB code
if I can figure out how to display the data in the TextBox.


Thanks again.





[quoted text, click to view]
pvdg42
5/31/2006 7:47:06 AM

[quoted text, click to view]
OK, and which of the three rows returned will be used to populate your text
boxes?
I'm hoping somebody else will jump into this thread, as all I have found is
bad news for you.
Basically, there is no "easy" way to populate the text boxes from any
in-memory representation of database data without writing the logic to
select a given field, convert that field to a String (if necessary), then
assign the string to the text property of a textbox.

I'm not at all sure you've simplified your problem by using the
SqlDataSource class:

Please read here:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource(VS.80,d=ide).aspx

http://msdn2.microsoft.com/en-us/library/703tk8bc(d=ide).aspx

the SqlDataSource class is designed to fill complex controls with data, with
little or no coding. At a minimum, populating text boxes will require that
you either parameterize your SELECT query to limit rows returned to one,
then select columns for display in text boxes, or that you write the logic
to select the row in question from those in the returned dataset.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource(d=ide).aspx

You should also read this article explaining the DataSourceMode property:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.datasourcemode(d=ide).aspx

AddThis Social Bookmark Button