Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 960
  • Last Modified:

FormView and DataBinding

Greetings all

I am trying to bind a List to a FormView (see code snippets below)  and am getting this error, when I run it - "Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource."

What do I need to do?

In advance, thanks!!

    allanmark
THE CODE:
 
private Couple theCouple;
{
        theCouple = BusinessLogic.Business.GetCouple(coupleId);
        Session["theCouple"] = theCouple;
        
        fvSpousal.DataSource = theCouple;   <============== ERROR
        fvSpousal.DataBind();
}
 
 
THE CLASSES:
 
   public class Couple
    {
        private int id;
        private string surname;
        private int? portfolio;
        private DateTime? wedAnniv;
        private string marStatus;
         ............................ 
        ............................ 
 
 public class Spouse
    {
        private int id;
        private string idNumber;
        private string sex;
        private int title;
        private string surname;
        private string firstName;
       ................................
       ................................
 
THE FORMVIEW:

Open in new window

0
allanmark
Asked:
allanmark
  • 7
  • 6
3 Solutions
 
williamcampbellCommented:
declare your class like this

public class Couple : IEnumerable

when you compile you will get some errors that you haven't implemented some functions

Follow the instructions here :

http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx



0
 
allanmarkAuthor Commented:
Hi, williamcampbell

I had a look at the MS site, as per your posting. As I was browsing through, it dawned on me that I may be using the wrong structures.

In a nutshell, what I am trying to do ......

There is a listbox with Couple names in it. As the user scrolls down the list, the approrpiate data is loaded into an object (ie there will only be one record) which is linked to the FormView.

Should I still be going the IEnumerable  route?

0
 
williamcampbellCommented:
Yeah because the class you have right now can't be converted to a Data Type that can be "bound" to a control that can bind Data to itself ... List Boxes ComboBoxes Grids etc


P.S.
You can also convert it to XML using LINQ  but that might be for another day :)




0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
allanmarkAuthor Commented:
Ok!~ Confusion time! Never done this b4, so I'm hoping I don't give U 2 many gray hairs!!

The MS example  is structured around a People class which is an array of Person class.

Mine is a single record, with the bulk of the fields falling under the Couple record (in the MS this would be the People class). There are no occurences of the Spouse class (in the MS eg the Person class) save where it is used as a definition in the Couple class.

Does that mean that I have to restructure ...

   -  My Couple class must be on the level fo the Person class
 -    I must introduce a new class "CoupleMain" which will contain only one field - an array of Couple
     - (even thoguh there will  only be one

Or am I  totally missing the boat (quite likely!!)



0
 
williamcampbellCommented:

  Couple is to People
  Spouse is to Person
 
 You Enumerate the Spouses in the Couple

 The FormView can then Call your Enumerator

  Goes like this...

 Couple Give me your First Spouse
    Here is a Spouse
 Couple give me your Next Spouse
    Here is the next Spouse
 Couple Give me your Next Spouse
   There are no More
 
From you Classes it looks like you need a member in Couple called ..

  ArrayList<Spouse>  theSpouses;

Or some coupling (no pun) between couple and Spouse

(6 gray airs)










 
0
 
allanmarkAuthor Commented:
It's beginning to make more sense!!   :)  :)  :)

Fields in the Couple class ( private int? portfolio;  private DateTime? wedAnniv;  private string marStatus;)  belong to the Couple ands not the individual spouse.

Possibly, I must change Spouse to Spousal (for lack of a better name!) and have that class contain the couple-specific fields and the Spouse.

All that Couple class would contain would be an array of Spousal (which would be the coupling that you referred to)

0
 
williamcampbellCommented:
Now your getting it...
0
 
williamcampbellCommented:
Here something I did a while back
        public class AutoBorrowItems : IEnumerator
        {
            private  Int16 _index;
 
            public AutoBorrowItems()
            {
                _index = -1;
            }
 
            public  void Reset()
            {
                _index = -1;
            }
 
            public object Current
            {
                get
                {
                    return (Int16)FeatureManager.requiredFeatures[_index];
                }
            }
 
            public  bool MoveNext()
            {
                _index++;
                while ( _index < FeatureManager.requiredFeatures.Length && FeatureManager.autoBorrow[FeatureManager.requiredFeatures[_index]] == true)
                {
                    _index++;
 
                    if (_index >= FeatureManager.requiredFeatures.Length)
                        return false;
                }
 
                if (_index >= FeatureManager.requiredFeatures.Length)
                    return false;
                else
                    return true;
            }
        }    
        
        public IEnumerator GetEnumerator ()
        {
            return new AutoBorrowItems();
        }

Open in new window

0
 
allanmarkAuthor Commented:
Hi!

Attached snippet contains the class / IEnumerable.

Two questions:

(1)    How do I add to this? Previously I was  saying "Couple theCouple = new Couple(); theCouple.Id =
        xyz; theCouple.Spouse1.FirstName = abc; etc)

(2)  Tried the following:

          foreach (Spousal sp in myCouple)
        {
            TextBox1.Text = "?";
        }

     It complains that "'DataTransferObjects.Spousal' is inaccessible due to its protection level"

   If I change "internal class Spousal"  to "public class Spousal" then is fine. My understanding here is
  that this is correct? AM I wrong in saying this?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
 
namespace DataTransferObjects
{
    public class Couples : IEnumerator, IEnumerable
    {
        private Spousal[] Couple;
        private int cnt;
 
        public Couples()
        {
 
        }
 
        public void Reset()
        {
            cnt = -1;
        }
 
        public bool MoveNext()
        {
            return (++cnt < Couple.Length);
        }
 
        public object Current
        {
            get
            { return Couple[cnt]; }
        }
 
        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)this;
        }
 
    }
 
 
    internal class Spousal
    {
        private int id;
        private string surname;
        private int? portfolio;
        private DateTime? wedAnniv;
        private string marStatus;
        private bool activeME;
        private bool involvedInME;
        private string comments;
        private string email;
        private Spouse spouse1;
        private Spouse spouse2;
        private int index = -1;
 
 
        public Spousal()
        {
            spouse1 = new Spouse();
            spouse2 = new Spouse();
        }
 
        public int Id
        {
            get { return id; }
            set {id = value; }
        }
 
        public string Surname
        {
            get { return surname; }
            set { surname = value; }
        }
 
        public int? Portfolio
        {
            get { return portfolio; }
            set {portfolio = value; }
        }
 
        public DateTime? WedAnniv
        {
            get { return wedAnniv; }
            set { wedAnniv = value; }
        }
 
        public string MarStatus
        {
            get { return marStatus; }
            set { marStatus = value; }
        }
 
        public bool ActiveME
        {
            get { return activeME; }
            set { activeME = value; }
        }
 
        public bool InvolvedInME
        {
            get { return involvedInME; }
            set {involvedInME = value; }
        }
 
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
 
        public string Comments
        {
            get { return comments; }
            set { comments = value; }
        }
 
        public Spouse Spouse1
        {
            get { return spouse1; }
            set {spouse1 = value; }
        }
 
        public Spouse Spouse2
        {
            get { return spouse2; }
            set {spouse2 = value; }
        }
 
    }
 
}
 
//-------------------------------
    public class Couple
    {
        private int id;
        private string surname;
        private int? portfolio;
        private DateTime? wedAnniv;
        private string marStatus;
        private bool activeME;
        private bool involvedInME;
        private string comments;
        private string email;
        private Spouse spouse1;
        private Spouse spouse2;
        private int index = -1;
 
 
        public Couple()
        {
            spouse1 = new Spouse();
            spouse2 = new Spouse();
        }
 
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
 
        public string Surname
        {
            get { return surname; }
            set { surname = value; }
        }
 
        public int? Portfolio
        {
            get { return portfolio; }
            set { portfolio = value; }
        }
 
        public DateTime? WedAnniv
        {
            get { return wedAnniv; }
            set { wedAnniv = value; }
        }
 
        public string MarStatus
        {
            get { return marStatus; }
            set { marStatus = value; }
        }
 
        public bool ActiveME
        {
            get { return activeME; }
            set { activeME = value; }
        }
 
        public bool InvolvedInME
        {
            get { return involvedInME; }
            set { involvedInME = value; }
        }
 
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
 
        public string Comments
        {
            get { return comments; }
            set { comments = value; }
        }
 
        public Spouse Spouse1
        {
            get { return spouse1; }
            set { spouse1 = value; }
        }
 
        public Spouse Spouse2
        {
            get { return spouse2; }
            set { spouse2 = value; }
        }
 
    }
 
}

Open in new window

0
 
williamcampbellCommented:
Ok here are a few things

 Couples are made up of Couple contain Spouse

 Get rid of spousal you don't need it

In Couples
Change    private Spousal[] Couple to private ArrayList<Couple>  couples;

In Couples
Add      public NewCouple ( Couple theCouple )  { couples.Add ( theCouple); }

Now you call it like this

Couples theCouples = new Couples();
Couple campbells = new Couple (  "William", "Maura" );  // Change the contructor if you like
campbells.Surname = "Campbell"; .....
theCouples.NewCouple( campbells );
Couple Smiths = new Couple ( "Tom", "Bob" ); //
Smiths.Surname = "Smith";
theCouples.NewCouple( Smiths );

// Now to Access the Data

foreach ( Couple cp in theCouples )
{
    txtbox1.text = cp.spouse1.firstname;
    txtbox2.text = cp.spouse2.firstname;
}

which doesnt really make sense for one couple better to have something like

Couple theCouple = theCouples.Couple["Campbells"];

ie access by surname

in which case you change ArryList<Couple> to Dictionary<String,Couple>

and in the NewCouple you change it to be

NewCouple ( Couple theCouple ) {  couples[theCouple.Surname] = theCouple; }


to pass it to the form

 fvspousal = theCouples;

Hope this has confused you even more :)

(13 gray hairs and counting)
















 
 
0
 
allanmarkAuthor Commented:
Cool! Thanks!

Off to do the kiddie run thingy. Will sit with it later this evening.
0
 
allanmarkAuthor Commented:
More gray hairs ....

private ArrayList<Couple> couples;   gives this:

"The non-generic type 'System.Collections.ArrayList' cannot be used with type arguments"
0
 
allanmarkAuthor Commented:
Many thanks!!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now