We help IT Professionals succeed at work.

Updating with Dropdowns in DetailsView

colonialiu20
colonialiu20 asked
on
985 Views
Last Modified: 2012-05-05
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
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.

Author

Commented:
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?

Author

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.