Solved

Webservice: return multiple rows using SQLDataReader.

Posted on 2006-07-13
6
658 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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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…

713 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