[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:
Have a read through the data tutorials (Data Access Layer and Business Logic Layer) on the ASP.NET website here, it will help.

It shows you how to create a 3-tier website (Data Access Layer, Business Logic Layer, Presentation Layer), which will help you construct insert, update and select statements that you can then use as data sources for the controls on your page, like your gridview.

Creating your websites using this method will allow you to easily manage it's structure and how your site uses data sources. This method also helps you to pinpoint problems when they arise by just finding the relevant bit of code.
 
DropDownList ModelNumber = (DropDownList)yourdatagrid.FindControl("ModelNumber");
ModelNumber.Items.Add((new ListItem((String)dr["ModelNumber"])));

I was thinking with the dropdownlist is that its a child control of the datagrid, it is not accessible directly. You have to locate the control by using FindControl() method.
 
Back
Top Bottom