This is a migrated thread and some comments may be shown as answers.

[Solved] Using Entity Framework to add/edit child record in hierarcy grid

1 Answer 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mindy
Top achievements
Rank 1
Mindy asked on 11 Sep 2009, 07:51 PM

I have two tables: Country, Province. All i want to do is to click on the country to add province/state to that selected country. However, i am having a hardtime to pass the countryid as whereparameter. 


 <asp:EntityDataSource ID="edsCountry" runat="server"   
        ConnectionString="name=PurchaseOrderEntities" Include="ProvinceState" 
        DefaultContainerName="PurchaseOrderEntities" EnableDelete="True"   
        EnableInsert="True" EnableUpdate="True" EntitySetName="Country" >  
    </asp:EntityDataSource>  
    <asp:EntityDataSource ID="edsProvince" runat="server"   
        ConnectionString="name=PurchaseOrderEntities"   
        DefaultContainerName="PurchaseOrderEntities" Where="it.Country.CountryID=@CountryID" Include="Country" 
        EnableDelete="True"    
        EnableInsert="True" EnableUpdate="True" EntitySetName="ProvinceState">  
 
         <WhereParameters>  
             <asp:Parameter Name="CountryID" DbType="Int32"  DefaultValue="1"/>  
        </WhereParameters>  
          
    </asp:EntityDataSource> 

 

 

 

Code behind:
 

 

 protected void gvCountry_InsertCommand(object source, GridCommandEventArgs e)  
        {  
            if ("Province/State".Equals(e.Item.OwnerTableView.Name))  
            {  
                try 
                {  
                    GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;  
                    string countryID = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["CountryID"].ToString();  
                    edsProvince.InsertParameters["CountryID"].DefaultValue = countryID;  
       
 
                }  
                catch (Exception ex)  
                {  
                    DisplayMessage(gvCountry, ex.Message);  
                }  
           
                 
            }  
              
        } 

 

Is it possible to populate the selected country in the inline add new form within the grid?
This is the error i got: Object reference not set to an instance of an object.Province/State cannot be inserted. Reason: Entities in 'PurchaseOrderEntities.ProvinceState' participate in the 'FK_ProvinceState_Country' relationship. 0 related 'Country' were found. 1 'Country' is expected.

Thanks
Mindy

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 16 Sep 2009, 02:20 PM
Hi Mindy,

How to set default values for the editors in child item of grid hierarchy on init insert or extract the new values and pass them to the InsertParameters collection of the nested table data source you can see from the resources below:

http://www.telerik.com/community/code-library/submission/b311D-ghdhk.aspx
http://www.telerik.com/help/aspnet-ajax/grdextractprimarykeyforparentiteminhierarchyonupdateinsert.html

I also noticed that you do not have CountryID InsertParameter defined for the edsProvince data source which is referenced inside the InsertCommand handler.

Update your code accordingly to see whether this helps.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Mindy
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or