Update command not working with hidden field as a parameter

B

Brad Baker

I have a formview with a datasource that contains a select and update
command. The select statement works fine but the update command doesn't seem
to be working. After some troubleshooting I have narrowed the problem down
to the department_id parameter which is set from a hidden field.

I can confirm this by hard coding the UpdateCommand to: UPDATE configuration
SET special_notes=@special_notes WHERE (student_id=@student_id) AND
(department_id='1234'). I know my hidden field is getting set correctly
because I am do a "view source" on the rendered page and see the department
id being set with no extra spaces, correct case and so forth. Could someone
help me figure out what I am missing? :)

Thanks,
Brad


<asp:SqlDataSource ID="formview_datasource" runat="server"
ConnectionString="Connection Info" ProviderName="System.Data.SqlClient"
SelectCommand="select command" UpdateCommand="UPDATE configuration SET
special_notes=@special_notes WHERE (student_id=@student_id) AND
(department_id=@department_id)" OnSelecting="SetParameters">
<SelectParameters>
<asp:QueryStringParameter Name="student_id"
QueryStringField="student_id" Type="String" />
<asp:parameter Name="department_id" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:FormParameter FormField="student_id" Name="student_id" />
<asp:FormParameter FormField="department_id" Name="department_id" />
<asp:FormParameter FormField="special_notes" Name="special_notes" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:FormView ID="formview" runat="server"
DataSourceID="formview_datasource" Width="100%">
<EditItemTemplate>
<table>
<tr>
<td class="right_labels">Special Notes:</td>
<td colspan="3" > <asp:TextBox ID="special_notes" runat="server"
rows="5" style="width: 85%" Text='<%# Bind("special_notes") %>'
TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="4">
<asp:HiddenField ID="department_id" Value='<%#
Department.GetDepartmentID() %>' runat="server" />
<asp:HiddenField ID="student_id" Value='<%#
Bind("student_id") %>' runat="server" />
<asp:Button ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update" Text="Update"> </asp:Button>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
 
S

Steven Cheng[MSFT]

Hello Brad,

From your description, you're encountering some problem when using FormView
with some update parameter reference to HiddenField control ,correct?

As for the updating command, what's the exact problem behavior, is there
any error message such as "parameter is not supplied or method signature
not match"?

Based on the aspx template you provided, you've the following
EditTemplate(used for updating command)

===============
<EditItemTemplate>
<table>
<tr>
<td class="right_labels">Special Notes:</td>
<td colspan="3" > <asp:TextBox ID="special_notes" runat="server"
rows="5" style="width: 85%" Text='<%# Bind("special_notes") %>'
TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="4">
<asp:HiddenField ID="department_id" Value='<%#
Department.GetDepartmentID() %>' runat="server" />
<asp:HiddenField ID="student_id" Value='<%#
Bind("student_id") %>' runat="server" />
<asp:Button ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update" Text="Update"> </asp:Button>
</td>
</tr>
</table>
</EditItemTemplate>
===================

Only "special_notes" and "student_id" is supplied through "Bind" keyword in
binding expression. however, the update parameters in DataSource control
expected three parameters:

================
<UpdateParameters>
<asp:FormParameter FormField="student_id" Name="student_id" />
<asp:FormParameter FormField="department_id" Name="department_id" />
<asp:FormParameter FormField="special_notes" Name="special_notes" />
</UpdateParameters>
</asp:SqlDataSource>
====================

and the <asp:HiddenField ID="department_id" Value='<%#
Department.GetDepartmentID() %>' runat="server" /> doesn't seem to be a
valid two-way databinding expression.

Also, I have tried a very simple test with FormView edittemplate using a
HiddenField to reference a primary key column (in database table) and the
updating can work correctly.

You can check the above setting or try a simplified table to see whether
the parameters and binding expression setting is the cause.

If there is any other finding, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brad Baker

<asp:HiddenField ID="department_id" Value='<%#
Department.GetDepartmentID() %>' runat="server" /> doesn't seem to be a
valid two-way databinding expression.

I wasn't trying to update the department_id value, only use it as a SQL
WHERE criteria. Apparently even though I don't want to update it, a two way
databinding expression was what I needed. When I changed the above to read:

<asp:HiddenField ID="department_id" Value='<%# Bind("department_id") %>'
runat="server" />

Everything started to work as I expected.

Thank you! :)
Brad
 
S

Steven Cheng[MSFT]

You're welcome :)

Have a nice day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top