Solved

Webservice: return multiple rows using SQLDataReader.

Posted on 2006-07-13
6
650 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
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:mmarksbury
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Accepted Solution

by:
mmarksbury earned 100 total points
Comment Utility
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
Comment Utility
This is worth the try, Thanks.
Datasets works well, but I will test this collection approach
0
 
LVL 7

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
extra space below iframe 9 51
Simple Mosaic Web Library 8 60
Conditional Link URL - Easy Question 4 43
Upload PDF On Mobile 1 78
Uploading files to the web server has become common part of almost any kind of web application. People use different technologies to solve this, but regardless of the technology used, it is always useful to have some kind of progress indicator shown…
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now