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
"cboTitle" runat="server" DataTextField="Title" DataValueField="TitleID" SelectedValue='<%# Bind("ContactTitleID") %>' DataSource='<%# (GetTitles()) %>'>
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
Regards, Scott Schecter EntitySpaces | Blog | Twitter
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.
"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
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.
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
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
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
Here is a very brief example of how to set values on your entity in an esPreInsert event
protected void EsDataSource2_esPreInsert(object sender, EntitySpaces.Web.esDataSourceInsertEventArgs e) { e.Values[EmployeesMetadata.PropertyNames.ReportsTo] = 1; }