Solved

Webservice: return multiple rows using SQLDataReader.

Posted on 2006-07-13
6
657 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
How our DevOps Teams Maximize Uptime

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

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Read about why website design really matters in today's demanding market.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

766 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