SqlDataSource - how get first row in SELECT?

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!
 
K

kahtava

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.
 
V

VB Programmer

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?
 
V

VB Programmer

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...
 
P

Patrick.O.Ige

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top