• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Retrieving database values simply into paragraphs and not into a grid

Hi all

I want to retrieve a single row from a database, and embed values from certain columns into a paragraph.  All the examples I can find online use a datagrid which displays the data as a huge, well, errm, grid or at least in tabular form. The database values are only simple things like Names and House Numbers, so tabular layout is total overkill!  How is this direct placement into paragraphs achieved??

--------------------------------------------------------------------------------------------------------
Example output.
Paragraph text Paragraph text Paragraph text Paragraph text <%= DATABASE VAL 01 %> Paragraph text Paragraph text Paragraph text <%= DATABASE VAL 02 %> Paragraph text Paragraph text Paragraph text Paragraph text Paragraph text

Paragraph text Paragraph text Paragraph text Paragraph text Paragraph text Paragraph text
Paragraph text <%= DATABASE VAL 03 %> Paragraph text Paragraph text Paragraph text Paragraph text Paragraph text Paragraph text Paragraph text Paragraph text <%= DATABASE VAL 04 %>
--------------------------------------------------------------------------------------------------------

Can anyone point me in the direction of examples that don't use tabular formats to display database data, or at least post a rough example?  I've tried databinding to ASP:LABELs but that doesn't work.  In classic ASP this was really easy using a recordSet and a WHILE NOT RS.EOF statement.
0
evilmike
Asked:
evilmike
  • 5
  • 5
  • 3
  • +1
4 Solutions
 
stumpy1Commented:
evilmike,
You could do it in code behind, almost the same way as in ASP. You open a connection and a SQLDataReader and loop through the datareader to dynamically populate a StringBuilder object which you can then output to the page.

There are probably other options but this is 1.


0
 
stumpy1Commented:
I suggest using StringBuilder as opposed to string object bacause of memory considerations. StringBuilder is part of System.Text;
0
 
evilmikeAuthor Commented:
Okay that sounds good.  Is there an example of pulling individual values out of a sqlDataReader.  I don't want the 'firehose' thing to happen and show everything at once - only certain values at certain times!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jeff CertainCommented:
If you're looking for a subset of records, just use a standard SQL "WHERE" clause to restrict your dataset before retrieving from the server. Alternately, you can retrieve the entire table, and use the dataview object's RowFilter property to restrict what records you access at any given time.
0
 
evilmikeAuthor Commented:
I actually have the records as desired (as in the original post - 1 row).  I only now need to refer to these values in certain places of the page, and not display them altogether in a grid format.

It is possible to assign each value to a local variable and then refer to that when required?
0
 
stumpy1Commented:
You should be able to that as you would have done with classic ASP, maybe Im understanding you incorrectly or something
0
 
Jeff CertainCommented:
Call the datareader.read to populate the datareader with the row.

Then, reference the fields you want by using datareader("fieldname") and insert them wherever you want them.
0
 
gadget27Commented:
You could always just query the database, load the row into a datatable, and then use those individual values at the appropriate places inside your paragraph.

EXAMPLE>

In Code Behind-->
(in class member declarations)
Protected dt as New Datatable()
(in your databinding routine, called from Page_Load)
dim da as new SQLDataAdapter("SELECT * FROM Whereever WHERE something = whatever", yourConnString)
da.fill(dt)

In ASPX page-->
paragraph text..paragraph text..paragraph text.. <%= dt.Rows(0)("ColumnName") &> paragraph text..paragraph text..paragraph text..paragraph text..paragraph text.. <%= dt.Rows(0)("ColumnName") &>  paragraph text..paragraph text..paragraph text..paragraph text.. paragraph text..paragraph text..paragraph text..paragraph text.. para <%= dt.Rows(0)("ColumnName") &> graph text..paragraph text.. paragraph text..paragraph text..
0
 
evilmikeAuthor Commented:
gadget27 - I like that idea, but how does it fair up on server resources?  Is more strain on resources than when simply using the datareader and storing the values in variables??

Chaosian - Your idea also works nicely on my current page.  Is it possible to keep the dataReader data active after the database connection has closed, or does that automatically clear the dataReader data out of memory??
0
 
Jeff CertainCommented:
Well, the data reader won't have the data once you close the connection -- you need to load it into a dataset/datatable/variables to hold onto the data.

If you need to display the data for various rows in your dataset, and it changes infrequently, you may want to consider loading the data into a datatable  in global.asax, and filtering that data as you need it using a dataview. This would allow you to load the data once (providing it is relatively static), and save you repeated trips to the database server.
0
 
evilmikeAuthor Commented:
The example <%=dt.Rows(0)("ColumnName") %> refers to a certain row (0).  Is there a command therefore that returns how many rows there actually are in the dataReader?  This is so should anything go wrong in the WHERE clause, I can find the number of rows retrieved before I pass them to the page??
0
 
Jeff CertainCommented:
Rows(0) is probably used here because you suggested there would only be one row returned.

dt.rows.count returns the number of rows. If it = 0 then you have no data.
0
 
evilmikeAuthor Commented:
Am I right in thinking though that the database connection would need to remain open until the last possible instance occurring when the datatable was referenced?  Or can the connection be closed once the dataTable is populated, unlike the dataReader?
0
 
Jeff CertainCommented:
The datatable, once populated, does not need the connection. ADO .Net uses a disconnected data model, in which they assume that most people only want ot view the data, and that updates are relatively uncommon compared to "browsing" the data. This is an issue that creates some significant confusion for people who are trying to modify the data and then send is back to the server.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now