SqlDataSource - how get first row in SELECT?

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

I have a sqldatasource on a webform. How do I (in code) retrieve the value
of a particular column in the first row? Do I have to save the
sqldatasource SELECT results into a datatable and look at it that way? A
code sample would help.

Thanks!
 
You have a number of solutions, here are a few:

You could use an SQL statement such as "SELECT TOP 1 [column name] FROM
[table name];" then use the ExecuteScalar Method.
http://msdn.microsoft.com/library/d...qlclientsqlcommandclassexecutescalartopic.asp

You could use an SQL statement such as "SELECT [column name] FROM
[table name];" and a DataReader to read the first row.
http://msdn.microsoft.com/library/d...ystemDataSqlClientSqlDataReaderClassTopic.asp

You can fill a DataTable with the SELECT results, but you are not bound
to that single technique. Code examples can be found on the proceeding
links.
 
In ASP.NET 2.0 it seems like I'm so close to already having the data with
the new SqlDataSource object. Am I missing something?
 
That's what I want.

I have a webform with 1 item on it: An SQLDataSource that has a SELECT
statement.

I just want to retrieve (Page_Load) a field in the first record of the data
result set. Any ideas?

Again, this is 2.0...
 
Do you want to return only one value after executing the query -
ExecuteScalar returns the first field in the first row
Is that what you want?
Patrick
 
Back
Top