Solved

Best way to get a single row

Posted on 2004-08-08
5
242 Views
Last Modified: 2012-03-15
Hi, if I have a stored procedure that when executed will return one row, with three columns, what is the best way to get the data from this. At the mo I have a sqldataadapter filling a dataset, but ho do I get the data from there, I want three vars say v1,v2 and v3 to be populated with the string vals in the row?

Thanks Kieran.
0
Comment
Question by:kplonk
5 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 20 total points
ID: 11747590
DataAdapter.Fill(yourDataSet, "tablename");

if(yourDataSet.Tables["tablename"].Rows.Count == 1) {
    DataRow dr = yourDataSet.Tables["tablename"].Rows[0];
    //here you can access variables like dr["name"] ...
}

given that you only return 3 values you might want to look at output parameters from your stored procedure.
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 11747733
Even a DataAdapter and DataSet may be a bit overkill. If you're just reading the data and not updating it, a DataReader should do the job.

string col1, col2, col3;
SqlCommand cmd = new SqlCommand("storedProcedure", connectionObject);
cmd.CommandType = CommandType.StoredProcedure;
connectionObject.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
col1 = dr.GetString(0);
col2 = dr.GetString(1);
col3 = dr.GetString(2);
dr.Close();
connectionObject.Close();
0
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11748015
0
 

Author Comment

by:kplonk
ID: 11748180
Thanks just what i wanted
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11748227
Anytime. Glad to help.

Cheers,

Greg
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now