locked
Help Inserting Related data RRS feed

  • Question

  • User-1425666379 posted

    I have a gridview that contains data about "Problem Reports". It has related Comments that exist in another table, that is, a user can add to a list of comments related to an existing Problem report. I can do Master/Detail just fine, so that isn't a problem. But what I want to do is be able to insert new comments related to the currently selected item in the GridView. The comments are displayed in a ListView under the "ProblemReports" gridview.

    My problem is figuring out how to get the insert on the Comments ListView to know that it should be inserting using the key of the selected Problem Report. I used the wizard to get where I'm at now, but it assumes that you are selecting a key from a dropdown. I want that key selected by virtue of the master grid selection. Code looks something like this...

     

    <asp:GridView ID="ProblemsGrid" runat="server" DataSourceID="ProblemsDataSource" AutoGenerateSelectButton="True"
         AutoGenerateColumns="false" 
         DataKeyNames="CreditRequestID"
         DataMember="CreditRequestID">
         <Columns>
              <!--DynamicFields-->
         </Columns>
    </asp:GridView>
    
    <asp:EntityDataSource ID="ProblemsDataSource" runat="server"
         EnableInsert="true">
         <WhereParameters>
              <asp:DynamicControlParameter ControlId="FilterRepeater" />
         </WhereParameters>
    </asp:EntityDataSource>
    
    <asp:ListView ID="ListView_Comment" 
         InsertItemPosition="LastItem" 
         DataSourceID="CommentsDataSource"
         runat="server" >
        <LayoutTemplate>
            <!--Layout and ItemPlaceholder -->    
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td >
                    <asp:DynamicControl DataField="CommentText" Mode="ReadOnly"
                        runat="server"/>
                </td>
                <td>
                    <asp:DynamicControl DataField="UserName" Mode="ReadOnly"
                        runat="server"/>
                </td>
                <td>
                    <asp:DynamicControl DataField="CommentDate" Mode="ReadOnly"
                        runat="server"/>
                </td>
                <td colspan="2">
                <asp:DynamicControl ID="DynamicControl21" DataField="ProblemReport"
                        Mode="ReadOnly" runat="server" />
                </td>
            </tr>
        </ItemTemplate>
         <InsertItemTemplate>
            <tr>
                <td colspan="3">
                    <asp:LinkButton ID="InsertLinkButton" CommandName="Insert"
                        runat="server"> Insert </asp:LinkButton>
    
                    <asp:DynamicControl ID="DynamicControl24" DataField="CommentText"  Mode="ReadOnly"
                        UIHint="MultilineText_Edit" runat="server"/>
                </td>
                <td >
                    <asp:DynamicControl ID="DynamicControl25" DataField="ProblemReport"
                        Mode="Insert"  ValidationGroup="CommentsList_Insert" runat="server" />
                </td>
            </tr>
        </InsertItemTemplate>
    </asp:ListView>
    
    <asp:EntityDataSource ID="CommentsDataSource" 
        runat="server" EntitySetName="Comment"
        EnableInsert="true" 
        EnableUpdate="true" 
        ContextTypeName="PDCCreditRequestEntities">
        <WhereParameters>
            <asp:DynamicControlParameter Name="ProblemReport" ControlId="ProblemsGrid" />
        </WhereParameters>
    </asp:EntityDataSource>>
     

    What this produces is a ListView with a dropdown to select a key for the comment insert. I want it to be able to insert the correct ProblemReport Key without selecting it.
     

    Thursday, September 18, 2008 5:17 PM

Answers

  • User-1425666379 posted

    For anybody interested, David Ebbo and Diego Vega came up with a solution for me on this. The problem and solution are also posted on the EF forum.

    As I suspected, I needed an InsertParameter, but didn't know how to use it, but here it is. The Parent Entity is on the page in a GridView, and the Child entities are listed in a ListView control. In Northwind terms, the EntityDataSource for the child entities would look like this:

    <asp:EntityDataSource 
         ID="ProductsDataSource" runat="server" 
         EntitySetName="Products"
         EnableInsert="true" 
         EnableUpdate="true" 
         ContextTypeName="NorthwindEntities">
    
        <WhereParameters>
            <asp:DynamicControlParameter Name="Categories" ControlId="GridView1" />
        </WhereParameters>
    
        <InsertParameters>
            <asp:ControlParameter Name="Categories.CategoryID" ControlId="GridView1" 
                 Type="Int32" />
        </InsertParameters>
    
    </asp:EntityDataSource>

     The other possibility, which is less desirable in my opinion, but worked quite well regardless, would be to do this in code-behind:

    protected void ProductsDataSource_Inserting(object sender, 
         EntityDataSourceChangingEventArgs e) 
    {
            var newProduct = e.Entity as Products;
            var categoryId = GridView1.SelectedDataKey.Value;
            newProduct.CategoriesReference.EntityKey = new EntityKey
                 ("NORTHWNDEntities.Categories", "CategoryID", categoryId);
    }

     Hope this helps somebody..

    Jeff

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 25, 2008 5:58 PM

All replies

  • User-330204900 posted

    I'm making the asumtion that this is custom page and so what i would and have done is setup a WHERE clause on the CommentsDataSource so that it's key set via the selectted row of the GridView.

    Hope that helps [:D]

    Thursday, September 18, 2008 5:42 PM
  • User-1425666379 posted

    Steve, can I have just a little more of your time? 

    If I understand correctly, you are saying that in a Where clause I can get the ID I need from the ProblemsGrid and assign it to something in the InsertTemplate? I think I need the caveman instructions. I'm not very familiar with this and I'm not finding a lot out there to explain it. How do I do what you are saying?

     Thank you for you help!

    Thursday, September 18, 2008 9:51 PM
  • User-330204900 posted

    Hi this post on my blog does thing the oposite of what you are doing but the principal should be the same:

    Custom Pages Part 4 - A DetailsView and a GridView using Validation Groups

    <asp:LinqDataSource 
        ID="ldsOrder_Details" 
        runat="server" 
        ContextTypeName="NorthwindDataContext"
        TableName="Order_Details" 
        Where="OrderID == @OrderID" 
        EnableUpdate="True">
        <WhereParameters>
            <asp:ControlParameter 
                ControlID="dvOrders" 
                Name="OrderID" 
                PropertyName="SelectedValue"
                Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>

    Hope this helps [:D]

    Friday, September 19, 2008 2:34 AM
  • User-1425666379 posted

    For anybody interested, David Ebbo and Diego Vega came up with a solution for me on this. The problem and solution are also posted on the EF forum.

    As I suspected, I needed an InsertParameter, but didn't know how to use it, but here it is. The Parent Entity is on the page in a GridView, and the Child entities are listed in a ListView control. In Northwind terms, the EntityDataSource for the child entities would look like this:

    <asp:EntityDataSource 
         ID="ProductsDataSource" runat="server" 
         EntitySetName="Products"
         EnableInsert="true" 
         EnableUpdate="true" 
         ContextTypeName="NorthwindEntities">
    
        <WhereParameters>
            <asp:DynamicControlParameter Name="Categories" ControlId="GridView1" />
        </WhereParameters>
    
        <InsertParameters>
            <asp:ControlParameter Name="Categories.CategoryID" ControlId="GridView1" 
                 Type="Int32" />
        </InsertParameters>
    
    </asp:EntityDataSource>

     The other possibility, which is less desirable in my opinion, but worked quite well regardless, would be to do this in code-behind:

    protected void ProductsDataSource_Inserting(object sender, 
         EntityDataSourceChangingEventArgs e) 
    {
            var newProduct = e.Entity as Products;
            var categoryId = GridView1.SelectedDataKey.Value;
            newProduct.CategoriesReference.EntityKey = new EntityKey
                 ("NORTHWNDEntities.Categories", "CategoryID", categoryId);
    }

     Hope this helps somebody..

    Jeff

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 25, 2008 5:58 PM