I have an ASP.NET da开发者_开发问答ta repeater. This is currently set to a data source obtained from an SQL query, as follows:
IDataReader dr = GetData(sql);
myRepeater.DataSource = dr;
myRepeater.DataBind();
This works fine, but what I want to do now is to call a web service, passing all the data returned from the SQL query and make that available to the repeater as well. So, my question is, can I manipulate the data reader object before it is bound, or the data repeater afterwards in order to achieve this; for example:
IDataReader dr = GetData(sql);
var extraData = CallWS(dr);
foreach (MyData d in extraData)
{
dr.AddField(d.Value);
}
myRepeater.DataSource = dr;
myRepeater.DataBind();
There are many ways that you can do this. Probably the easiest way is to put the values from the first query into a Collection by looping around the DataReader and then adding them into the collection. Then call the web service and append those results to the collection also. Then you will have a collection containing all of your results that you can bind to the repeater.
IDataReader dr = GetData(sql);
var dataCollection = new List<string>();
while(reader.Read())
{
dataCollection.Add(reader.GetString(1));
}
var extraData = CallWS();
while(extraData.Read())
{
dataCollection.Add(extraData.GetString(1));
}
myRepeater.DataSource = dataCollection;
myRepeater.DataBind();
Something like the above. That's not that much extra code. I know you're not passing the results into the web service but I would advise against that for a number of reasons:
1) Extra data transfer. There is no point passing the results to the web service for the web service to just pass them back again (unless they are needed as part of the query). 2) If the web service cant function without a set of results from another query then the web service isnt very well encapsulated and one could argue the external query really belongs inside the web service if it's integral to it functioning.
精彩评论