Avatar of jgoodale
jgoodale
Flag 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

Microsoft DynamicsVisual Basic.NETC#

Avatar of undefined
Last Comment
jgoodale

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
jgoodale

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jgoodale

ASKER
used the advice to create the solution.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck