none
asp web app - Update Gridview with dropdown list from sql server RRS feed

  • Question

  • I am trying to use a gridview for companies, edit the entries based on a list coming from a table in SQL Server.

    in this example, I have a gridview interfacing a company table.    I want the user to be able to select a service area from a uniform list (sql server again) and have those values update the EntityID in the Company Table.

    If I use a simple text box which is the default when you add the editing functionality - it binds without any problem.  When I change it to the dropdown list, it does not seem to bind.

    I'm kind of new to asp pages so forgive the elementary nature of the question.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewEditCompany.aspx.cs" Inherits="Enterprise.ViewEditCompany" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="EntityID" DataSourceID="dsCompany">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:BoundField DataField="EntityID" HeaderText="EntityID" InsertVisible="False" ReadOnly="True" SortExpression="EntityID" />
                    <asp:BoundField DataField="CompanyNumber" HeaderText="CompanyNumber" SortExpression="CompanyNumber" />
                    <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                    <asp:BoundField DataField="CompanyAbbreviation" HeaderText="CompanyAbbreviation" SortExpression="CompanyAbbreviation" />
                    <asp:TemplateField HeaderText="ServiceArea" SortExpression="ServiceArea">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False" DataSourceID="sqlSvcArea" DataTextField="ServiceArea" DataValueField="ServiceArea">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="sqlSvcArea" runat="server" ConnectionString="<%$ ConnectionStrings:dbPhysEnt %>" ProviderName="<%$ ConnectionStrings:dbPhysEnt.ProviderName %>" SelectCommand="SELECT DISTINCT [ServiceArea] FROM [T_SERVICE_AREA]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ServiceArea") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="SA_Mnemonic" HeaderText="SA_Mnemonic" SortExpression="SA_Mnemonic" />
                    <asp:BoundField DataField="INSRT_DTS" HeaderText="INSRT_DTS" SortExpression="INSRT_DTS" />
                    <asp:BoundField DataField="INSRT_USR" HeaderText="INSRT_USR" SortExpression="INSRT_USR" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="dsCompany" runat="server" ConnectionString="<%$ ConnectionStrings:dbPhysEnt %>" 
                            DeleteCommand="DELETE FROM [PE_Entity] WHERE [EntityID] = @EntityID" 
                            InsertCommand="INSERT INTO [PE_Entity] ([CompanyNumber], [CompanyName], [CompanyAbbreviation], [ServiceArea], [SA_Mnemonic], [INSRT_DTS], [INSRT_USR]) VALUES (@CompanyNumber, @CompanyName, @CompanyAbbreviation, @ServiceArea, @SA_Mnemonic, @INSRT_DTS, @INSRT_USR)" ProviderName="<%$ ConnectionStrings:dbPhysEnt.ProviderName %>" 
                            SelectCommand="SELECT * FROM [PE_Entity]" 
                            UpdateCommand="UPDATE [PE_Entity] SET [CompanyNumber] = @CompanyNumber, [CompanyName] = @CompanyName, [CompanyAbbreviation] = @CompanyAbbreviation, [ServiceArea] = @ServiceArea, [SA_Mnemonic] = @SA_Mnemonic, [INSRT_DTS] = @INSRT_DTS, [INSRT_USR] = @INSRT_USR WHERE [EntityID] = @EntityID">
                <DeleteParameters>
                    <asp:Parameter Name="EntityID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="CompanyNumber" Type="String" />
                    <asp:Parameter Name="CompanyName" Type="String" />
                    <asp:Parameter Name="CompanyAbbreviation" Type="String" />
                    <asp:Parameter Name="ServiceArea" Type="String" />
                    <asp:Parameter Name="SA_Mnemonic" Type="String" />
                    <asp:Parameter Name="INSRT_DTS" Type="DateTime" />
                    <asp:Parameter Name="INSRT_USR" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="CompanyNumber" Type="String" />
                    <asp:Parameter Name="CompanyName" Type="String" />
                    <asp:Parameter Name="CompanyAbbreviation" Type="String" />
                    <asp:Parameter Name="ServiceArea" Type="String" />
                    <asp:Parameter Name="SA_Mnemonic" Type="String" />
                    <asp:Parameter Name="INSRT_DTS" Type="DateTime" />
                    <asp:Parameter Name="INSRT_USR" Type="String" />
                    <asp:Parameter Name="EntityID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
        </div>
        </form>
    </body>
    </html>


    KDW


    • Edited by siera_gld Wednesday, March 13, 2019 7:57 PM
    • Moved by CoolDadTx Thursday, March 14, 2019 2:46 AM ASP.NET related
    Wednesday, March 13, 2019 7:56 PM

All replies

  • Hi siera_gld,

    According to your description and code, your problem is related about asp.net, so please ask your question at asp.net forum for professional help.

    Here is the asp.net forum link:

    https://forums.asp.net/

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 14, 2019 2:36 AM