0

Trying to get data from service and bind it to grid

I'm getting the data in localitem and the error occurs on the "foreach" line

any help would be appreciated

private IEnumerable<PersonalIDCheckerMvCKendo.Models.PersonInfo> Getlocalinfo(string personalNO)
{
    needsUpdate = false;

    using (PersonalInfoServiceClient serviceclient = new PersonalInfoServiceClient())
    {
        List<PersonalIDCheckerMvCKendo.Models.PersonInfo> personInfo = new List<PersonalIDCheckerMvCKendo.Models.PersonInfo>();

        try
        {
            IEnumerable localItem = serviceclient.GetLocalInfoForPerson(personalNO);

            if (localItem != null)
            {
                foreach (PersonalIDCheckerMvCKendo.Models.PersonInfo dalitem in localItem)
                {
                    personInfo.Add(new PersonalIDCheckerMvCKendo.Models.PersonInfo
                    {
                        DocumentSerie = dalitem.DocumentSerie,
                        DocumentNumber = dalitem.DocumentNumber,
                        DocumentType = dalitem.DocumentType,
                        DocumentIssuer = dalitem.DocumentIssuer,
                        DocumentValidDate = dalitem.DocumentValidDate
                    });                        
                }
            }
            return personInfo.ToArray();
        }
        catch
        {
            throw;
        }
        finally
        {
            serviceclient.Close();
        }
    }
}

EDIT:

GetLocalInfoForPerson is of type PersonalInformation

public partial class PersonalInformation : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged , System.Collections.IEnumerable 
Nathaniel
  • 11
  • 1
  • 4
  • Please show the complete stack trace. (It's not clear why you've got a pointless `catch` block, or quite so much whitespace in your question either... I'll fix the latter, but please pay more attention to questoin readability in the future.) – Jon Skeet Dec 24 '13 at 11:23
  • 1
    The catch-phrase is completely redundant and therefore pointless. – Thomas Weller Dec 24 '13 at 11:28
  • I intend to change the catch block, need to get the methods working first – Nathaniel Dec 24 '13 at 11:46
  • The problem is likely within the localItem object, and you've not shown the code that creates it. IEnumerable is an interface and I would guess some of the class of localItem has a stub for one of the functions to enumerate that throws the exception. Add more code to this question if you want help. – Ed Bayiates Dec 24 '13 at 22:02

1 Answers1

1

What concrete type does GetLocalInfoForPerson return ? Does it implement IEnumerable properly ?

Based on your code sample, it is hard to say what might be wrong since we don't know the types involved. However, you type the localItem as IEnumerable, and if this is a custom implementation, someone might have forgotten to implement the non-generic GetEnumerator method.

Do you get an error if you change the declaration of localItem to be:

IEnumerable<PersonalIDCheckerMvCKendo.Models.PersonInfo> localItem = serviceclient.GetLocalInfoForPerson(personalNO);

?

This would imply that IEnumerable.GetEnumerator is not implemented (ie. throws NotImplementedException), while IEnumerable<T>.GetEnumerator is in fact implemented. It could also be that both methods are not implemented. Look it up in the code for the type that is returned from GetLocalInfoForPerson.

driis
  • 161,458
  • 45
  • 265
  • 341
  • I get this error when I change the declaration of localItem - `Error 1 Cannot implicitly convert type 'PersonalIDCheckerMvCKendo.PersonalInfoService.PersonalInformation' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)` GetLocalInfoForPerson is of type PersonalInformation 'public partial class PersonalInformation : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged , System.Collections.IEnumerable' – Nathaniel Dec 24 '13 at 11:37
  • Then PersonalInformation does not properly implement IEnumrable. Take a look in the code, how does the `GetEnumerator` method look on that class ? – driis Dec 24 '13 at 11:52
  • `public System.Collections.IEnumerator GetEnumerator() { throw new NotImplementedException(); }` how can I change it ? – Nathaniel Dec 24 '13 at 11:54
  • @Nathaniel Write the method to implement it, think about how the `PersonalInformation` should be enumerated. – user692942 Dec 24 '13 at 11:58