Form view

Posts   
 
    
bentos
User
Posts: 146
Joined: 12-Jul-2006
# Posted on: 24-Jul-2006 17:07:32   

Hello all,

I'm using a data source object in conjunction with a form view object (I'm only using the insert template of the object), in basically trying to insert a new entity into the database, but the form only initially contains a subset of the full entity and I will programmaticly add the rest of the values in the FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)

Could somebody quickly tell me how I access the new entity object from within the entity collection associated with the data source so I can add the values?

For example, something like this!

// CustomerEntity CE = (CustomerEntity)LLBLGenProDataSourceNew.EntityCollection[0]; // CE.Password = new PasswordGenerator().Generate();

Thank you for your help,

Regards,

Matthew

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Jul-2006 07:34:44   

A wild guess: EntityCollection collection = (EntityCollection)LLBLGenProDataSourceNew; CustomerEntity CE = (CustomerEntity)EntityCollection.Items[0]; (edit) You should check if the FormView control has a bound dataitem, and also you could bind to the EntityAdded event of the collection and in the handler you may knows which entity was added.

bentos
User
Posts: 146
Joined: 12-Jul-2006
# Posted on: 25-Jul-2006 11:40:49   

The form view is immediately in insert mode, I am trying to create an item, so I assume no item is data bound up front .

Sorry but I do not know what you mean in your explanation. could you possibly expand on this?

I was thinking of setting live persistence to false and somehow using the perform work event to add the new values before committing, but to be honest I am not sure how this will be done.

Am I even doing this the right way? I just want to access at the right time the entity that is about to be inserted into the database, so I can modify/add to its values.

Thanks for your help

dregs
User
Posts: 19
Joined: 28-May-2005
# Posted on: 25-Jul-2006 12:07:17   

Hi

I use code like this


Page

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" OnClick="SaveItem" runat="server" Text="Button" />

Code behind

protected void SaveItem(object sender, EventArgs e)
    {
    
        UserContentEntity usercontent = new UserContentEntity();

        usercontent.ContentTitle = TextBox1.Text;
        //Add any other values you need

        // save it. We require an adapter for this
        DataAccessAdapter adapter = new DataAccessAdapter();
        adapter.SaveEntity(usercontent, true);

    }

bentos
User
Posts: 146
Joined: 12-Jul-2006
# Posted on: 25-Jul-2006 13:38:03   

Hello dregs,

Thank you for your reply,

Your method is certainly clean and easy to understand and probably the way I was going to do it next if I cannot get a decent answer here, I was just investigating how to do this with as much drag and drop/little custom code as possible, basically using design time functionality as much as possible. Although I am in danger of complicating things if not careful, this should be fairly simple if I knew the correct procedure.

I'm not suggesting this is a problem with the software, I'm new to ASP.net anyway!

Thanks again

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 25-Jul-2006 17:05:13   

You should read the FormView's DataItem property and cast that to the entity type you're editing (e.g. CustomerEntity).

Frans Bouma | Lead developer LLBLGen Pro
bentos
User
Posts: 146
Joined: 12-Jul-2006
# Posted on: 26-Jul-2006 12:05:37   

Hello,

The form data item is null at this point (maybe require another event handler?), here is the code as it stands

Basically, the password field in this case cannot be null, I suppose I could work around by setting a hidden field to empty string, then after the insert reloading the entity and updating the required fields, but you would think we would be able to access the entity object at some point to save this messing around?

Thank you for your help,

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    LLBLGenProDataSourceExist.FilterToUse = new PredicateExpression(
    (CustomerFields.Email == e.Values["Email"]));
    LLBLGenProDataSourceExist.Select();
    if (LLBLGenProDataSourceExist.EntityCollection.Count == 1)
    {
        Panel1.Visible = true;
        e.Cancel = true;
    }
    else
    {
        Panel1.Visible = false;
        CustomerEntity CE = (CustomerEntity)FormView1.DataItem;
        CE.Password = new PasswordGenerator().Generate();

    }
}
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 26-Jul-2006 16:17:41   

bentos wrote:

Hello,

The form data item is null at this point (maybe require another event handler?), here is the code as it stands

It could be that it is null, because you're inserting an item, and not updating one.

Basically, the password field in this case cannot be null, I suppose I could work around by setting a hidden field to empty string, then after the insert reloading the entity and updating the required fields, but you would think we would be able to access the entity object at some point to save this messing around?

Thank you for your help,

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    LLBLGenProDataSourceExist.FilterToUse = new PredicateExpression(
    (CustomerFields.Email == e.Values["Email"]));
    LLBLGenProDataSourceExist.Select();
    if (LLBLGenProDataSourceExist.EntityCollection.Count == 1)
    {
        Panel1.Visible = true;
        e.Cancel = true;
    }
    else
    {
        Panel1.Visible = false;
        CustomerEntity CE = (CustomerEntity)FormView1.DataItem;
        CE.Password = new PasswordGenerator().Generate();

    }
}

I see you execute a Select() inside the ItemInserting event handler, I don't think that's wise. However I do understand why you're doing this, because otherwise the form won't show up when your collection is empty. You can show the insert template by doing:


protected void _switchViewButton_Click(object sender, EventArgs e)
{
    switch(_downloadItemEditFormView.CurrentMode)
    {
        case FormViewMode.Edit:
            _downloadItemEditFormView.ChangeMode(FormViewMode.Insert);
            break;
        case FormViewMode.Insert:
            _downloadItemEditFormView.ChangeMode(FormViewMode.Edit);
            break;
    }
}

Here I show an event handler for the button which flips the formview state: update or insert.

You could grab the last entity in the collection as the entity which was added. You could also subscribe to the LLBLGenProDataSourceExist.EntityCollection.EntityAdded event and handle it there.

You could also pre-set the password in an override of OnInitialized in the entity itself. When inserting data through a formview, the entity isn't there until the form is submitted. Then the data is passed to the datasourcecontrol, which calls ExecuteInsert of the datasourcecontrol, and in there a new entity is created, filled with the data and added to the entitycollection.

Frans Bouma | Lead developer LLBLGen Pro
bentos
User
Posts: 146
Joined: 12-Jul-2006
# Posted on: 26-Jul-2006 18:31:03   

this last bit is the crux of the matter as you say

When inserting data through a formview, the entity isn't there until the form is submitted. Then the data is passed to the datasourcecontrol, which calls ExecuteInsert of the datasourcecontrol, and in there a new entity is created, filled with the data and added to the entitycollection.

-

You could grab the last entity in the collection as the entity which was added. You could also subscribe to the LLBLGenProDataSourceExist.EntityCollection.EntityAdded event and handle it there.

You could also pre-set the password in an override of OnInitialized in the entity itself.

-

I'll look into it,

Thanks again,