THESE FORUMS ARE NOW FROZEN
Please choose "Forums" from the Main menu of www.entityspaces.net to get to our new forums.

How to perform an automatic update when you can't call Bind on one of the form controls

rated by 0 users
This post has 9 Replies | 1 Follower

Top 50 Contributor
Posts 47
andieje Posted: 07-29-2009 8:31 AM

HI

I am using a telerik grid. This grid uses the same form template for both updates and inserts and this is causing me some problems.

My form template is used to update Customer records. One of the fields in my customer table is TitleID which is a foreign key to the Titles table. This table contains records such as Mr, Mrs Miss etc. The form template contains a drop down list of these titles which is populated from the database. The SelectedValue property of the dropdown list is set to the TitleID of the current record. This is achieved like so

Code:
 "cboTitle" runat="server" DataTextField="Title" DataValueField="TitleID" 
SelectedValue='<%# Bind("ContactTitleID") %>'     DataSource='<%# (GetTitles()) %>'>   
                

This code works fine for automatic updates. However when i try to perform an insert I get an error binding to the drop down list

'cboTitle' has a SelectedValue which is invalid because it does not exist in the list of items.  
Parameter name: value  

 

I understand what is causing this error. Naturally when you are performing an insert you are calling Bind for an empty record. Therefore the value for Bind("ContactTitleID") is an empty string whereas this is not one of the items in the drop down list of titles.

How can i overcome this whilst still performing automatic inserts and updates? I can populate the drop down list of titles and the selected value property depending on whether the user is performing an insert / update in the code behind in the data bind event for the grid but then the SelectedValue property of the of the cboTitles dropdown list is not passed to the ContactTitleID field because this control is not bound to this field.

How can i tell the esDataSource to get the values for the ContactTitleID field from the dropdown list without calling Bind?

Many thanks

Top 10 Contributor
Posts 905
You are going to need to create a custom function like I assume you are doing with GetTitles() and not set the selected value if it is an insert (ie if there is no selected value to set).

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 50 Contributor
Posts 47

Hi I'm sorry but i don't know what you mean.If i call a custom function in the aspx to populate the SelectedValue property of the drop down list then the ContactTitleID property is not updated/set when you perform an  automatic update/insert because there is nothing telling asp.net that the ContactTitleID field gets its value from the drop down list. Normally the call to the Bind function associates controls with fields.

 

Code:
 "cboTitle" runat="server" DataTextField="Title" DataValueField="TitleID" 
SelectedValue='<%# SomeFunction() %>'     DataSource='<%# (GetTitles()) %>'>  

 

So if i have something like this the automatic update/insert is performed but the ContactTitleID field remains unchanged for an update or isn't set for an insert

If i am missing something please could you give me an example in psuedocode. thanks

Top 10 Contributor
Posts 905

Your not missing anything as I said before ASP.NET does not support true hierarchical databinding, therefore you have to hook up that key yourself. The DataSoruce control is not going to do that for you. You can use our hierarchical dynamic query to do this though, see here for examples

http://www.developer.entityspaces.net/documentation/HierarchicalModel/HierarchicalModel.aspx

So at a high level in your custom function you are going to have to set the keys for the fk tables. We only save downstream on hierarchical saves to prevent circular references so this may or may not work in your case. If not you will have to save the fk record yourself. Please remember ASP.NET databinding s NOT going to save hierarchically. This is not a limitation of EntitySpaces, but of ASP.NET databinding.

 

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 50 Contributor
Posts 47

Hi

Thank you for your reply. Unfortunately I am still stuck. I don't think I am explaining very well what my problem is. I understand that I will have to save the value for the foreign key field myself but I don't understand how to hook into the process. At the moment updates/inserts are performed automatically and all i have is this code

Code:
    Protected Sub EsDataSource1_esCreateEntity(ByVal sender As Object, ByVal e As EntitySpaces.Web.esDataSourceCreateEntityEventArgs) Handles EsDataSource1.esCreateEntity



        Dim c As New Customers
        If e.PrimaryKeys Is Nothing Then
            c.AddNew()
        Else
            c.LoadByPrimaryKey(e.PrimaryKeys(0))
        End If

        e.Entity = c

   


    End Sub

    Protected Sub EsDataSource1_esInsert(ByVal sender As Object, ByVal e As EntitySpaces.Web.esDataSourceInsertEventArgs) Handles EsDataSource1.esInsert

        gridCustomers.Rebind()
    End Sub

    Protected Sub EsDataSource1_esUpdate(ByVal sender As Object, ByVal e As EntitySpaces.Web.esDataSourceUpdateEventArgs) Handles EsDataSource1.esUpdate

        gridCustomers.Rebind()

    End Sub

 

Where abouts in this would i update/set the ContactTitleID field? I understand how to do the whole thing automatically or the whole thing manually but not how to tap into the automatic process. It's probably easier to just do it manually without the data source but I am interested now

 

many thanks

Top 10 Contributor
Posts 905
Have a look at the events we expose on the datasource. Specifically, the inserting and updating events which are there to allow tap into to the event cycle pre insert/update

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 50 Contributor
Posts 47

Hi I have looked at the events but i can't see anywhere any information on how i would pass any information into the esDataSource that would in turn feature in a database insert/update. I've looked at the event args too but its not immediaely apparent from the docs if any of them are parameters for an update/insert.

Thanks

Top 10 Contributor
Posts 905
You do not pass the info to the esDataSource for update. What you need to do is trap the inserting/updating event and then check the e.Colleciton or e.Entity to get the underlying data from the datasource. Then you can grab the fk from the e.Entity or e.Collection and perform the FK update just like you would with our Collection or Entity normally. You are still expecting the esDataSoruce to update hierarchical data which is not going to work for the reasons I have mentioned previously. You are probably swimming upstream trying to use a DataSource control for his vs using a Collection or Entity directly. The DataSource controls are great for quick and easy binding but once you get into more complex stories like this you are really better off using Entities and Collections.

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 10 Contributor
Posts 905
One other  quick thought, if you trap the DataSoruce preinsert event I think it is you should be able to set the e.Entity's Title property to cboTitle.SelectedValue. Then the save would automatically work as you have provided the fk id to the datasource and it is only updating the main table.

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Top 10 Contributor
Posts 905

Here is a very brief example of how to set values on your entity in an esPreInsert event

 

Code:
protected void EsDataSource2_esPreInsert(object sender, EntitySpaces.Web.esDataSourceInsertEventArgs e)
{
	e.Values[EmployeesMetadata.PropertyNames.ReportsTo] = 1;
}

Regards, Scott Schecter EntitySpaces | Blog | Twitter

Page 1 of 1 (10 items) | RSS
Copyright © 2005 - 2009, EntitySpaces, LLC