Solved

Webservice: return multiple rows using SQLDataReader.

Posted on 2006-07-13
6
656 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Read about why website design really matters in today's demanding market.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

813 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

11 Experts available now in Live!

Get 1:1 Help Now