Digital Colony!

Update Fails on FormView

After a miserable afternoon chasing down this bug, I thought it may be helpful to step through why a database update could fail when using the asp:FormView control.

Symptoms

  1. The Database Connection String works, because the "Get" is displaying the record.
  2. The Stored Procedure works, because you ran it inside the database using dummy values.
  3. You stepped through the wizard to build both the asp:SqlDataSource and asp:FormView controls.
  4. When you modify a record and click the Update link, the record stays the same. No error is thrown and no changes are made to the database.

Try This

If this is your situation, then you may have the same problem I ran into. Only after I setup debugging in the stored procedure itself did I realize that the asp:FormView was returning a NULL as the primary key. And if a NULL is passed to an UPDATE stored procedure it will execute error free with an updated ROWCOUNT of zero.

My fix was to add the primary key to DataKeyNames field on the asp:FormView.
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="AuthorID">

Labels: ,

posted by MAS on Mar 7,2007

Legacy Comments

Digital Colony Copyright © 1999-2010 XHTML   508
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.