Binding an entity to controls

Posts   
 
    
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 17-Mar-2010 14:18:35   

winforms,llblgen2.6,vb.net, v2.6

Just curious..is it possible to bind an entity to controls on a formss or do i have to use an entitycollection.

I am creating a winform to add an entity..hence i will always be dealing with one entity and not a collection?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-Mar-2010 08:39:13   

Sure you can. You can use a BindingSource and set it's datasource to any entity.

Steps to test it: 1- Add new windows form, and open it in design view. 2- VS menu -> Data -> Add New Data Source. 3- Select "Object" for the Type. 4- Select the entity class you want to bind to. 5- VS menu -> Data -> Show Data Sources. 6- A Dockable window will appear having the selected entity shown as a dataSource. 7- Select this entity, and then a dropDown List arrow will appear next to it. 8- Press the arrow button and select "Details" from the drop down list. 9- Drag the entity from the Data Source window and drop it on the form. 10- Now you should see some controls created on the form to be binded with the entity. 11- Also you should see an entityBindingSource created automatically for you. 12- Go to the code view fo the Form and write the following to fill the bindingSource.

            var entity = new CustomersEntity("ALFKI");
            using(var adapter = new DataAccessAdapter())
            {
                adapter.FetchEntity(entity);
            }
            customersEntityBindingSource.DataSource = entity;

In the above code, I've fetched a customer entity and passed it to the bindingSource DataSource property. 13- Run the application.

Hope this was helpful.

Richie
User
Posts: 5
Joined: 28-May-2012
# Posted on: 07-Jun-2012 07:42:10   

I tried the same but it's not working for me rage

where do i paste the code? which event?

i get an error when i pass parameters to the entity.

eg- var entity = new CustomersEntity("ALFKI");

it does not allow me to pass alfki.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 07-Jun-2012 12:21:44   

what error do you get? With stacktrace please. We can't look at your code nor the error on your screen.

and please next time don't re-open old threads.

Frans Bouma | Lead developer LLBLGen Pro
Richie
User
Posts: 5
Joined: 28-May-2012
# Posted on: 08-Jun-2012 00:48:31   

sorry for the inconvenience as caused.

here is my code:

using System; using System.IO; using System.Data; using System.Collections.Generic; using System.ComponentModel;

using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Northwind.Tutorial.DatabaseSpecific; using Northwind.Tutorial.EntityClasses; using Northwind.Tutorial.FactoryClasses; using Northwind.Tutorial.HelperClasses; using Northwind.Tutorial.RelationClasses; using Northwind.Tutorial.TypedViewClasses; using Northwind.Tutorial; using DevExpress.XtraGrid.Views.Card; using DevExpress.XtraEditors.Repository; using SD.LLBLGen.Pro.ORMSupportClasses;

namespace NorthwindConsole { public partial class SecondViewer : Form { private DataAccessAdapter DAA; public SecondViewer() { InitializeComponent(); DataLoad();

    }
    private void DataLoad()
    {

        DAA = new DataAccessAdapter();
        PatientEntity patient = new PatientEntity();
        BookingEntity booking = new BookingEntity();
        DAA.FetchEntity(booking);


    }





    private void button1_Click(object sender, EventArgs e)
    {


        IPrefetchPath2 path= new PrefetchPath2((int)EntityType.PatientEntity);
        path.Add(PatientEntity.PrefetchPathBookings);




        EntityCollection<PatientEntity> patients = new EntityCollection<PatientEntity>();
        IRelationPredicateBucket bucket = new RelationPredicateBucket();
        bucket.Relations.Add(PatientEntity.Relations.BookingEntityUsingPatient_ID);
        bucket.PredicateExpression.Add(PatientFields.Surname == "Andersen");
        bucket.PredicateExpression.Add(BookingFields.Duration > 15);

        SortExpression sorter = new SortExpression();
            sorter.Add(PatientFields.FirstName | SortOperator.Ascending);

            DAA.FetchEntityCollection(patients, bucket, 0, sorter, path);                               // syntax - DAA.FetchEntityCollection(ec,rpb,0,prefetchpath)


    }
    private void SecondViewer_FormClosing(object sender, FormClosingEventArgs e)
    {
        try
        {
            DAA.CloseConnection();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        this.Hide();
        Viewer v = new Viewer();
        v.Show();

    }

    private void SecondViewer_Load(object sender, EventArgs e)
    {

        var entity = new BookingEntity();

            DAA.FetchEntity(entity);


        bookingEntityBindingSource.DataSource = entity;

        //gridControl2.DataSource = bookingEntityBindingSource;
    }



          }

}

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Jun-2012 08:43:19   

Hi Richie,

I'm not sure what is your real problem (you didn't specify it). Anyway I see that it might be here:

var entity = new BookingEntity();           
DAA.FetchEntity(entity);

Here you are saying "fetch this entity" however you didn't specify the primary key for BookingEntity so it won't fetch anything. Actually LLBLGen will try to fetch a BookingEntity with PK null (which is unlikely). So, specify the PK you want to fetch in the BookingEntity constructor. For instance, if you PK is an int and you want to fetch the Booking 5 you will write something like:

var entity = new BookingEntity(5);          
DAA.FetchEntity(entity);

If your PK is string and you want to fetch Booking "B1" then you would write something like:

var entity = new BookingEntity("B1");           
DAA.FetchEntity(entity);
David Elizondo | LLBLGen Support Team