[ASP.NET] Gridviews: Why on earth isn't this simple?

Soldato
Joined
18 Oct 2002
Posts
7,139
Location
Ironing
I'm trying to do a very simple thing. I have two tables in a database, people and jobroles. People has a foreign key "roleid" which maps to the "id" primary key in jobroles. My gridview/data sources look like this:

Code:
<asp:GridView ID="datagrid" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        DataSourceID="sqldb" DataKeyNames="id">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" Visible="False" />
            <asp:BoundField DataField="first_name" HeaderText="First Name" SortExpression="first_name" />
            <asp:BoundField DataField="last_name" HeaderText="Last Name" SortExpression="last_name" />
            <asp:TemplateField HeaderText="Role">
                <ItemTemplate>
                    <asp:Label ID="rolelabel" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:ListBox Rows="1" ID="rolelistbox" SelectionMode="Single" runat="server" DataValueField="roleid" DataTextField="name" DataSourceID="rolelist"></asp:ListBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ButtonType="Button" ShowEditButton="True" ShowDeleteButton="True" />
            
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="sqldb" runat="server" ConnectionString="<%$ ConnectionStrings:audit_trainingConnectionString %>" 
        SelectCommand="SELECT people.id,dbo.people.first_name, dbo.people.last_name, dbo.job_roles.name FROM dbo.people INNER JOIN dbo.job_roles ON dbo.job_roles.id = people.roleid"
        UpdateCommand="Update people set first_name=@first_name, last_name=@last_name, roleid=@id where id=@id"
        DeleteCommand="Delete from people where id=@id">
        <UpdateParameters>
            <asp:Parameter Name="roleid" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="rolelist" runat="server" ConnectionString="<%$ ConnectionStrings:audit_trainingConnectionString %>"
        SelectCommand="SELECT id as roleid,name FROM job_roles">
    </asp:SqlDataSource>

Now, when I go to edit a row in the gridview, it displays a dropdown box on the role column like it should. However, when I try and update the row, NULL just gets inserted as the roleid. How do I tell it that @rolelist should be the selected value of the listbox?
 
Last edited:
Back
Top Bottom