Solved

FormView and DataBinding

Posted on 2009-04-16
13
934 Views
Last Modified: 2013-12-17
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
Comment
Question by:allanmark
  • 7
  • 6
13 Comments
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24161666
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
 

Author Comment

by:allanmark
ID: 24161825
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
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24161921
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
 

Author Comment

by:allanmark
ID: 24162423
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
 
LVL 12

Assisted Solution

by:williamcampbell
williamcampbell earned 300 total points
ID: 24162657

  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
 

Author Comment

by:allanmark
ID: 24162838
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 12

Expert Comment

by:williamcampbell
ID: 24162967
Now your getting it...
0
 
LVL 12

Assisted Solution

by:williamcampbell
williamcampbell earned 300 total points
ID: 24163000
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
 

Author Comment

by:allanmark
ID: 24165290
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
 
LVL 12

Accepted Solution

by:
williamcampbell earned 300 total points
ID: 24168199
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
 

Author Comment

by:allanmark
ID: 24168295
Cool! Thanks!

Off to do the kiddie run thingy. Will sit with it later this evening.
0
 

Author Comment

by:allanmark
ID: 24168804
More gray hairs ....

private ArrayList<Couple> couples;   gives this:

"The non-generic type 'System.Collections.ArrayList' cannot be used with type arguments"
0
 

Author Closing Comment

by:allanmark
ID: 31571166
Many thanks!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now