Saving LLBLGen entities in web form without using data binding

Posts   
 
    
Posts: 254
Joined: 16-Nov-2006
# Posted on: 15-Mar-2007 14:01:44   

Guys,

I've been having issues using databinding for quite a complex entity which consists of a number of related objects which would require a form view and a number of different grid views to contain data related to the main entity.

I thought I'd try and implement this without data binding and could simply add rows to a GridView programmatically but can't seem to do this.

I'm still using LLBLGen data source controls to bind some things on the page e.g. drop down list boxes but the main saving / updating / inserting of the entity will be performed manually by capturing data from each control manually.

Has anyone else used this approach without data binding and added rows to a grid for LLBLGen objects. Any thoughts or sample code would be appreciated?

Cheers

Matt

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Mar-2007 16:47:27   

Has anyone else used this approach without data binding and added rows to a grid for LLBLGen objects. Any thoughts or sample code would be appreciated?

I used to use this approach before adopting the .NET 2.0 databinding thing. In short I used to have forms to insert and update data, and then a grid to show the data. After an Insert or update, I used to refetch data into the grid.

Posts: 254
Joined: 16-Nov-2006
# Posted on: 15-Mar-2007 16:50:26   

Thanks, would you maintain the actual entity your saving within a session variable e.g. if the form is saving an Employee entitty would you save this to the session and then manage all changes their and call Save after any changes were made?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Mar-2007 10:16:16   

It depends on the cost of saving the entity in the session. (with the estimated number of sessions).

But most of the time I did not, and I used to get the data back from the controls (Grid).

Posts: 254
Joined: 16-Nov-2006
# Posted on: 19-Mar-2007 15:28:08   

Can you expand on the detail of the approach you used as I'm trying this using a grid view and have some code where I'm trying to extract a GUID value which is a hidden column on a grid view. I use the code below

protected void usersGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (Control gridControl in usersGridView.Rows[usersGridView.SelectedIndex].Controls)
        {
            if (gridControl is DataControlFieldCell)
            {
                DataControlFieldCell nameFieldCell = gridControl as DataControlFieldCell;
                if (nameFieldCell.ContainingField.HeaderText == "UserGuid")
                {
                    eGuid userGuid = new Guid(nameFieldCell.Text);
                    
                    AspnetMembershipEntity userMember = new AspnetMembershipEntity(userGuid);
                    telephoneNoTextBox.Text = userMember.User.TelephoneNo;
                    nameTextBox.Text        = userMember.UserName;
                }
            }
        }
    }

I also have defined the column using the ASPX markup as below

    <asp:BoundField DataField="UserId" HeaderText="UserGuid" SortExpression="UserGuid"
        Visible="False" />

Is this the correct approach, if so how do I access values such as this?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Mar-2007 09:59:36   

Your code looks fine to me.

Most of the times I did this, I used to edit entities outside of the grids, in a controls (textBoxes and stuff). I used the editCommand of the dataGrid to capture cells values and pass them to the controls text propertis. tb_Name.Text = e.Item.Cells[1].Text; // you can cell positions or better to use cell names tb_Name.Text = e.Item.Cells["Name"].Text;

Posts: 254
Joined: 16-Nov-2006
# Posted on: 20-Mar-2007 11:01:37   

Actually this approach doesn't work because hidden fields in a grid view are not accessible in this way ( visible = false )

However I thought a better approach would have been to access a unique identifier for the entity using the SelectedDataKey property of the GridView e.g.

        String selectedKeyValue = usersGridView.SelectedDataKey.Value.ToString();

and then load the entity from the database to access other properties. The grid and the database should ALWAYS be in sync because the grid is built from the database on post back.

However I can't get this approach to work too i.e. in the GridView ASPX mark up I have

    DataKeyNames="UserId" 

But the value above simply returns nothing.

Any ideas?

Posts: 254
Joined: 16-Nov-2006
# Posted on: 20-Mar-2007 21:18:22   

Ah realised it was because I defined the key field UserId in the BoundField tag as invisible. As soon as I remove this the SelectedDataKey property works fine.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Mar-2007 14:38:45   

Actually this approach doesn't work because hidden fields in a grid view are not accessible in this way ( visible = false )

In .NET 1.1 VS 2003, it used to work for me, I could programatically access hidden fields, in .NET 2.0 I always use DataBinding.