Link to home
Start Free TrialLog in
Avatar of colonialiu20
colonialiu20Flag for United States of America

asked on

Updating with Dropdowns in DetailsView

I am attempting to make use of the DetailsView including Updates of information using dropdowns.
I can set up a base DetailsView with nothing but BoundFields and successfully update information.  It's when I include an EditItemTemplate containing a dropdown that my update functionality is failing.  

I have two dropdowns labelled 'student_program_id' and 'student_district_id'.  Both populate and bind to the database fine, but when I make a dropdown selection and click update a NULL value goes into the database.  What am I missing here?  My guess is it deals with the Update Parameters,  but I have no solution.

----------------------------
Relevant pieces of Code (I am leaving out the GridView since it doesn't affect this update problem)
------------------------------
            <asp:SqlDataSource ID="sdsDistricts" runat="server" SelectCommandType="StoredProcedure" SelectCommand="proc_viewall_district" />
            <asp:SqlDataSource ID="sdsPrograms" runat="server" SelectCommandType="StoredProcedure" SelectCommand="proc_viewall_program" />

            <asp:SqlDataSource ID="sdsDetail" runat="server"
            SelectCommandType = "StoredProcedure" SelectCommand = "proc_view_student"
            UpdateCommandType ="StoredProcedure" UpdateCommand="proc_update_student">
                <SelectParameters>
                    <asp:ControlParameter ControlID="gvMaster" Name="student_id" PropertyName="SelectedValue" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="student_lname" />
                    <asp:Parameter Name="student_fname" />
                    <asp:Parameter Name="student_program_id" />
                    <asp:Parameter Name="student_district_id" />
                    <asp:Parameter Name="student_id" />
                </UpdateParameters>
            </asp:SqlDataSource>



            <asp:DetailsView AutoGenerateRows="false" DataKeyNames="student_id" DataSourceID="sdsDetail" OnItemUpdated="DetailsView1_ItemUpdated"
            HeaderText="- Student Details -" ID="dvDetails" runat="server" CellPadding="5" Width="300"
             BorderWidth="0" FieldHeaderStyle-Width="100" FieldHeaderStyle-BackColor="#cccccc" FieldHeaderStyle-CssClass="bold" FieldHeaderStyle-HorizontalAlign="Right"
              HeaderStyle-BackColor="#eeeeee" HeaderStyle-CssClass="bold" CommandRowStyle-BackColor="#eeeeee">
                <Fields>
                <asp:BoundField DataField="student_id" HeaderText="ID" ReadOnly="true" />
                <asp:BoundField DataField="student_fname" HeaderText="First Name" />
                <asp:BoundField DataField="student_lname" HeaderText="Last Name" />
                <asp:TemplateField>
                    <HeaderTemplate>Program</HeaderTemplate>
                    <ItemTemplate><%#Eval("student_program_name")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="student_program_id"  DataSourceID="sdsPrograms" AppendDataBoundItems="true" SelectedValue='<%# CheckEmpty(Eval("student_program_id")) %>'  DataValueField="student_program_id" DataTextField="student_program_name" runat="server">
                            <asp:ListItem Text="-Select-" Value="0"/>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>Resident District</HeaderTemplate>
                    <ItemTemplate><%#Eval("district_name")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="student_district_id"  DataSourceID="sdsDistricts" AppendDataBoundItems="true" SelectedValue='<%# CheckEmpty(Eval("student_district_id")) %>'  DataValueField="district_id" DataTextField="district_name" runat="server">
                            <asp:ListItem Text="-Select-" Value="0"/>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="true" />
                </Fields>
            </asp:DetailsView>

--------------------------------
My update stored procedure is nothing more than:
---------------------------------
CREATE PROCEDURE dbo.proc_update_student
(      @student_fname varchar(100),
      @student_lname varchar(100),
      @student_id integer,
      @student_program_id integer,
      @student_district_id integer
)
AS

UPDATE studentID
SET student_fname = @student_fname,
      student_lname = @student_lname,
      student_program_id = @student_program_id,
      student_district_id = @student_district_id

WHERE student_id = @student_id
GO
ASKER CERTIFIED SOLUTION
Avatar of peterdungan
peterdungan
Flag of 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 colonialiu20

ASKER

In just trying to get at least one field to update I updated the Sub to:

    Protected Sub DetailsView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs)
        Dim row As DetailsViewRow = dvDetails.Rows(4)

        Dim ddProgram_id As DropDownList = CType(row.FindControl("student_program_id"), DropDownList)
        e.NewValues("student_program_id") = ddProgram_id.SelectedValue

        gvMaster.DataBind()
    End Sub

Despite this I still have the same effect.  The value is still being updated to NULL.
Any other thoughts or see what I am missing?
It should be in the itemupdating event. You have it it in the itemupdated one.
I corrected it and got it into the right sub.  But the results are still the same.
Dim row As DetailsViewRow = dvDetails.Rows(4)

/\ In this line I used 4 arbitrarily. The number should be the row number containing the dropdownlist.

Is the itemupdating event set to trigger the subroutine?
Yes I forgot to set that updating attribute.  Always something simple.
Until now I wasn't aware of the Updating/Updated events.
Thanks, it is working now.