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