-1

History: I have a small site in PHP which I wanted to make a better front end for. Having looked around, spending an hour or so on each of FlashBuilder etc., I found Silverlight 4 the quickest to get moving with, but I have hit an issue.

I have a class for simplicity

 public partial class data
        {
            public String Software;
            public int OK;
            public int warn;
            public int falsepos;
            public int failed;
            public int total;
        }

I then load the class using a call to my service going something like:

    [OperationContract]
    public List<data> GetSum(){
     String sql="SELECT Software, OK, warn, falsepos, failed from sum";
        List<data> res = new List<data>();
            if (!DBConnect()) { throw new Exception("Unable to contact Database"); }

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand(sql, _sqlConnection);
            DataSet ds = new DataSet();
            da.Fill(ds);
            _sqlConnection.Close();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                data d = new data();
                d.Software = dr["software"].ToString();
                d.OK  = Int16.Parse(dr["OK"].ToString());
                d.warn  = Int16.Parse(dr["Warning"].ToString());
                d.falsepos = Int16.Parse(dr["False Positive"].ToString());
                d.failed = Int16.Parse(dr["Failed"].ToString());
                d.total = d.OK+d.warn+d.falsepos+d.failed;
                res.Add(d);
            }
            return res;}

Now, that compiles fine, except, the moment I add the foreach, it starts falling apart,

private void Summary(object Sender, ServiceProxy.GetSumCompletedEventArgs e)
{
    foreach (data d in e.Result)
    {

    }
}

The moment I go to parse through the list as a result, it says

Error    2    foreach statement cannot operate on variables of type 'SilverlightApplication1.ServiceProxy.GetSumResponse' because 'SilverlightApplication1.ServiceProxy.GetSumResponse' does not contain a public definition for 'GetEnumerator'    C:\Users\me\Documents\Visual Studio 2010\Projects\SilverlightApplication1\SilverlightApplication1\Page1.xaml.cs    52    13    SilverlightApplication1

Now, I've looked around and read and seen a number of things, now, a very very similar function which just returns a list of servernames, works. Because I guess it's of type String, however, I understand it's not generated the how to read the single item within the list, but this is bugging me. I just want to return a simple list of stuff. Can someone please point me in the right direction?

I'm sure it's something simple. Like an attribute, or something, but this seems silly that I can't find the answer, be kind, I've only been looking at Silverlight for a couple of hours.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • See this answer http://stackoverflow.com/questions/2297100/wcf-operationcontract-which-generic-collection-type-should-i-expose/2297166#2297166 – Bala R May 05 '11 at 12:42
  • Thanks, however, there were too many unknowns in that code for me to follow it. I realise my "data" class is the same as the "order" class, but, where does orderrequest come from? – BugFinder May 05 '11 at 12:52
  • The `OrderResult` is a wrapper class for a list of `Order`s. You can create a similar class `DataResult`. – Bala R May 05 '11 at 12:55
  • Can you please give me an example bit of code? Sorry to seem so thick, but this seems frustratingly simple on paper, and obviously I have failed to understand it well. – BugFinder May 05 '11 at 12:58
  • 1
    @BugFinder something along the lines of this http://pastie.org/1867861 – Bala R May 05 '11 at 13:03
  • AHA.. Thank you, now how to work out the grid/datagrid to display my results. Thank you. – BugFinder May 05 '11 at 13:22

1 Answers1

1

Following the lines of this answer

You can try

[DataContract]
public class DataResult
{
    [DataMember]
    public List<data> DataList{ get; set; }

    DataResult(List<data> dataList)
    { 
        DataList = dataList;
    }

}

[OperationContract]
public List<data> GetSum(){
    ...
    ...

    return DataResult(res);
}



private void Summary(object Sender, ServiceProxy.GetSumCompletedEventArgs e)
{
    foreach (data d in e.Result.DataList)
    {

    }          
}

and to display the list to a grid, you can do

DataGrid1.ItemsSource = e.Result.DataList;

and that should do it if your columns are configured properly or set to auto generate.

Community
  • 1
  • 1
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • See, you are wonderful. OK - the datagrid thing I had got that far, but, no data shows. I must be missing the auto generate columns.. as I know the data is there (as Im currently shoving it into a text box to show myself its there) – BugFinder May 05 '11 at 13:35
  • @BugFinder see if the example in this post helps with populating the grid https://msmvps.com/blogs/deborahk/archive/2011/01/22/populating-a-datagrid-in-a-silverlight-application.aspx – Bala R May 05 '11 at 14:12
  • Thanks.. Im getting there. A few things to sort out, but, its getting there. Seriously irritating the auto generated columns are sorted by name, but, its looking good. I can knock out a number of the other pages very quickly after Ive got the hang of these. – BugFinder May 05 '11 at 14:23