Solved

Webservice: return multiple rows using SQLDataReader.

Posted on 2006-07-13
6
661 Views
Last Modified: 2010-08-05
Hi, this is my first webservice
here is some code im making
 SqlDataReader itemData = sqlCmd.ExecuteReader();
            if (itemData.Read())
            {
                while (itemData.Read())
                {
                    item.Author = itemData.GetString(0);
                    item.Date = itemData.GetString(1);
                }}
            return item;
Is thera a way to store each row until while loop has finished and return all rows at in one go?
this returns the last or only row.
0
Comment
Question by:Snertill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:mmarksbury
ID: 17100537
Store the data in a data set or custom collection and read the data into it.  Then return the data set.
0
 

Author Comment

by:Snertill
ID: 17101088
thanks,
but I'm more looking for solution that does not involve datasets.
Not that I dont like datasets, just interessed in this approach for now.
0
 
LVL 7

Expert Comment

by:mmarksbury
ID: 17101127
Won't the nature of the logic in the above While loop ALWAYS set item.Author and item.Date to the last occurrence of data in the Reader?

If you're planning on sending this data somewhere from a method, you'll have to return something that allows for multiple rows...hence the DataSet or Collection suggestion.

Or maybe, I'm missing something.  Can you clarify a little more here?
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 7

Accepted Solution

by:
mmarksbury earned 100 total points
ID: 17101177
Perhaps look into creating an ItemCollection class or using Generics with C# 2.0.  Your code might change to look like this...

Using a Custom Collection (must create collection class first):
-------------------------------------------------------------------------------
ItemCollection Items = new ItemCollection();
SqlDataReader itemData = sqlCmd.ExecuteReader();
if (itemData.Read())
{
     while (itemData.Read())
     {
          Item NewItem = new Item();
          NewItem .Author = itemData.GetString(0);
          NewItem .Date = itemData.GetString(1);
          Items.Add(NewItem);
     }
}
return ItemCollection;

Using Generics (must be framework 2.0):
-------------------------------------------------------------------------------
List<Item> Items = new List<Item>();
SqlDataReader itemData = sqlCmd.ExecuteReader();
if (itemData.Read())
{
     while (itemData.Read())
     {
          Item NewItem = new Item();
          NewItem .Author = itemData.GetString(0);
          NewItem .Date = itemData.GetString(1);
          Items.Add(NewItem);
     }
}
return ItemCollection;
0
 

Author Comment

by:Snertill
ID: 17101355
This is worth the try, Thanks.
Datasets works well, but I will test this collection approach
0
 
LVL 7

Expert Comment

by:mmarksbury
ID: 17101409
The collection approach is my preference becuase you can treat the objects as typed objects not as Data Set values.  
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

636 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