Newbie Question: How Does One Access an Individual Field in a Record?

R

Rick

I can connect to a sql server database, I can query the database, and I can
display the results in a datalist, but how can I refer to an individual
field from a query? My query is from a sql command in a datasource.

For example: SELECT CUSTOMER FROM CUSTOMERS.

How do I use asp.net to refer to customer[1], customer[2], etc. Let's say
if I just wanted to display customer number 45 in a table:

<table>
<td>***customer[45]***</td>
</table>

How can I do that?

I'm normally a coldfusion developer, and I'm having difficulty
transitioning.

Thanks in advance,

Rick
 
M

Mark Fitzpatrick

It all depends upon which method you are using. If you are using a
datareader, you can't do this because a datareader provides a row by row
look a the records. It doesn't jump around, just moves to the next row. A
datareader also doesn't know how many records are being returned so it's not
possible to look for a single one.

What you want is a datatable probably. A datatable is an xml structure
created by ado.net to hold the results of your query. It has rows and column
and you can visualize it as a spreadsheet. You can use the Rows.Count
property to find the number of rows, just in case there is no row 45 for
your customers (and remember, 0 is the first row and 1 is the second, all
arrays begin with zero).

DataTable myTable = ... populate table from query
string customer = string.Empty;
if(myTable.Rows.Count >= 45)
{
// the following gets the value in column 0 of row number 45
customer = myTable.Rows[44][0].ToString();
}
 

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

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top