Link to home
Start Free TrialLog in
Avatar of jgoodale
jgoodaleFlag for United States of America

asked on

DetailsView and Entity Framework

You can see from my code below that I have a detailsview and I'm inserting into a database but I have a couple issues with this. The entity returns a field named productTypeID but I want to assign a value to the field and have it saved to the database. The productTypeID is stored in a session variable but I don't know how to get it to bind and save to the database using the entity. Ideally the productTypeID would be a hidden field that is bound to the entity and saved when the insert is triggered.
<asp:DetailsView ID="createLableView" runat="server" AutoGenerateRows="False" CssClass="frmLabel" BorderStyle="None" GridLines="None"
                DefaultMode="Insert" DataSourceID="ShippingLabelDataSource" DataKeyNames="id"  CellPadding="3">
                <Fields>
                    <asp:TemplateField HeaderText="Name" SortExpression="PersonName">
                        <InsertItemTemplate>
                            <asp:TextBox ID="txtPersonName" runat="server"  Text='<%# Bind("PersonName") %>'></asp:TextBox>
                            <asp:requiredfieldvalidator id="CompanyNameRequiredValidator"
                                controltovalidate="txtPersonName"
                                display="Dynamic"
                                text="Please enter your name." 
                                CssClass="labelValidate"
                                runat="server"/>                                      
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="CompanyName" HeaderText="Company Name" 
                        SortExpression="CompanyName" />
                    <asp:TemplateField HeaderText="Address" SortExpression="Address">
                        <InsertItemTemplate>
                            <asp:TextBox ID="txtAddress" runat="server"  Text='<%# Bind("Address") %>'></asp:TextBox>
                            <asp:requiredfieldvalidator id="AddressRequiredValidator"
                                controltovalidate="txtAddress"
                                display="Dynamic"
                                text="Please enter your address." 
                                CssClass="labelValidate"
                                runat="server"/>                                      
                        </InsertItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="City" SortExpression="City">
                        <InsertItemTemplate>
                            <asp:TextBox ID="txtCity" runat="server"  Text='<%# Bind("City") %>'></asp:TextBox>
                            <asp:requiredfieldvalidator id="CityRequiredValidator"
                                controltovalidate="txtCity"
                                display="Dynamic"
                                text="Please enter your city." 
                                CssClass="labelValidate"
                                runat="server"/>                                      
                        </InsertItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="State" SortExpression="StateOrProvinceCode">
                        <InsertItemTemplate>
                            <asp:TextBox ID="txtStateOrProvinceCode" runat="server"  Text='<%# Bind("StateOrProvinceCode") %>'></asp:TextBox>
                            <asp:requiredfieldvalidator id="StateOrProvinceCodeRequiredValidator"
                                controltovalidate="txtStateOrProvinceCode"
                                display="Dynamic"
                                text="Please enter your state." 
                                CssClass="labelValidate"
                                runat="server"/>                                      
                        </InsertItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Zip Code" SortExpression="PostalCode">
                        <InsertItemTemplate>
                            <asp:TextBox ID="txtPostalCode" runat="server"  Text='<%# Bind("PostalCode") %>'></asp:TextBox>
                            <asp:requiredfieldvalidator id="PostalCodeRequiredValidator"
                                controltovalidate="txtPostalCode"
                                display="Dynamic"
                                text="Please enter your zip code." 
                                CssClass="labelValidate"
                                runat="server"/>                                      
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number" 
                        SortExpression="PhoneNumber" />
                    <asp:CheckBoxField DataField="residential" HeaderText="Is Residential?" 
                        SortExpression="residential" />
      
                    <asp:BoundField DataField="productTypeID" HeaderText="productTypeID" 
                        SortExpression="productTypeID" />

                    <asp:CommandField ShowInsertButton="True" CausesValidation="true" InsertImageUrl="/images/getStarted.jpg" ButtonType="Image" />
                </Fields>
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                
            </asp:DetailsView>

            <asp:EntityDataSource ID="ShippingLabelDataSource" runat="server" 
                ConnectionString="name=GreenEntities" 
                DefaultContainerName="GreenEntities" EnableFlattening="False" 
                EntitySetName="shippingLabels" EnableInsert="True">
            </asp:EntityDataSource>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jgoodale

ASKER

That looks like what I want todo but I can't seem to get it to work. I added OnInserting="EntityDataSource1_Inserting" to my EntityDataSource but how do i setup the call or the modification to the productTypeID in the code behind.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
used the advice to create the solution.