colonialiu20
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="StoredP rocedure" SelectCommand="proc_viewal l_district " />
<asp:SqlDataSource ID="sdsPrograms" runat="server" SelectCommandType="StoredP rocedure" SelectCommand="proc_viewal l_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="SelectedValu e" />
</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="DetailsView 1_ItemUpda ted"
HeaderText="- Student Details -" ID="dvDetails" runat="server" CellPadding="5" Width="300"
BorderWidth="0" FieldHeaderStyle-Width="10 0" FieldHeaderStyle-BackColor ="#cccccc" FieldHeaderStyle-CssClass= "bold" FieldHeaderStyle-Horizonta lAlign="Ri ght"
HeaderStyle-BackColor="#ee eeee" 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</H eaderTempl ate>
<ItemTemplate><%#Eval("stu dent_progr am_name")% ></ItemTem plate>
<EditItemTemplate>
<asp:DropDownList ID="student_program_id" DataSourceID="sdsPrograms" AppendDataBoundItems="true " SelectedValue='<%# CheckEmpty(Eval("student_p rogram_id" )) %>' DataValueField="student_pr ogram_id" DataTextField="student_pro gram_name" runat="server">
<asp:ListItem Text="-Select-" Value="0"/>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Resident District</HeaderTemplate>
<ItemTemplate><%#Eval("dis trict_name ")%></Item Template>
<EditItemTemplate>
<asp:DropDownList ID="student_district_id" DataSourceID="sdsDistricts " AppendDataBoundItems="true " SelectedValue='<%# CheckEmpty(Eval("student_d istrict_id ")) %>' DataValueField="district_i d" DataTextField="district_na me" 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
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="StoredP
<asp:SqlDataSource ID="sdsPrograms" runat="server" SelectCommandType="StoredP
<asp:SqlDataSource ID="sdsDetail" runat="server"
SelectCommandType = "StoredProcedure" SelectCommand = "proc_view_student"
UpdateCommandType ="StoredProcedure" UpdateCommand="proc_update
<SelectParameters>
<asp:ControlParameter ControlID="gvMaster" Name="student_id" PropertyName="SelectedValu
</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="DetailsView
HeaderText="- Student Details -" ID="dvDetails" runat="server" CellPadding="5" Width="300"
BorderWidth="0" FieldHeaderStyle-Width="10
HeaderStyle-BackColor="#ee
<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</H
<ItemTemplate><%#Eval("stu
<EditItemTemplate>
<asp:DropDownList ID="student_program_id" DataSourceID="sdsPrograms"
<asp:ListItem Text="-Select-" Value="0"/>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Resident District</HeaderTemplate>
<ItemTemplate><%#Eval("dis
<EditItemTemplate>
<asp:DropDownList ID="student_district_id" DataSourceID="sdsDistricts
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It should be in the itemupdating event. You have it it in the itemupdated one.
ASKER
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?
/\ 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?
ASKER
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.
Until now I wasn't aware of the Updating/Updated events.
Thanks, it is working now.
ASKER
Protected Sub DetailsView1_ItemUpdated(B
Dim row As DetailsViewRow = dvDetails.Rows(4)
Dim ddProgram_id As DropDownList = CType(row.FindControl("stu
e.NewValues("student_progr
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?