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
Mark Fitzpatrick - 28 Dec 2006 15:36 GMT
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();
}

Signature
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
>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
[quoted text clipped - 17 lines]
>
> Rick