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: ,

AddThis Social Bookmark Button

12 Comments:

Anonymous Anonymous said...

Thanks so much for this little gem of information! I had been banging my head against a wall for several hours trying to get my update stored procs to work.

8/13/2007 5:26 AM

Anonymous Anonymous said...

Thank you. I had similar issue....

8/29/2007 1:50 PM

Anonymous Anonymous said...

God Bless You.

I have been working on this for two weeks.........

11/20/2007 6:55 AM

Anonymous COMALite J said...

Unfortunately, I do have my Primary Key in the DataKeyNames Property. It still doesn’t work. No error, no update.

I created an Updating Event Handler for both the FormView and the attached SqlDataView, to try to see what’s going on, by Response.Writeing the Parameters and their values to the output.

Unfortunately, the FormViewItemUpdateArgs structure has no way I can determine of counting the parameters. No Count(), no Items collection. Just e.OldValues and e.NewValues (and e.Keys) Dictionary collections, but none of them have Counts either, and using For Each also fails. I have to manually code each Parameter to be displayed.

Doing that seems to show that e.NewValues contains the old values, and so does e.OldValues except for one parameter that I attempt to modify the e.NewValue of in code (and it shows Nothing for the e.OldValue but still shows the old value for the e.NewValue as all the others do)!

SqlDataSourceCommandArgs, on the other hand, has no such limitation. You can enumerate e.Command.Parameters just fine. But the information it displayed was less than helpful: it shows that the Parameters exist as expected, but they all have the old values in them, despite being bound with <#% Bind("Param") %> constructs to all form controls (which are indeed all either <asp:… server controls, or HTML INPUT tag controls, all with runat="server" applied)!

HAAAALLLLP!!

(Note: we recently upgraded to ASP.NET 3.5 RTM. Could it be that the ASP.NET 2.0 SP1 that it installed broke things somehow?)

1/01/2008 11:07 PM

Blogger MAS said...

Comalite,
Sounds like you have more experience on FormViews than me. Since this March post, I went back to building FORMs old-school. I left with the impression that the FormView control did not earn my trust.

Wish I could be of help.

1/02/2008 6:21 AM

Anonymous Anonymous said...

Good one.. !! Had similar problem.. Works perfect now.. Thanx :)

2/11/2008 12:56 PM

Anonymous Anonymous said...

Great help. I found several other site with suggestions that didn't work.

5/22/2008 11:31 AM

Anonymous K5 said...

Hi
I am using stored procedure to get data to be displayed in the FormView. To update this data I want to use another stored procedure. In order to Bind the Form controls do I need to name the parameters in the Update SP exactly same as the select list of the select stored procedure?
Thanks for any help

5/30/2008 2:18 PM

Blogger MAS said...

k5 - I don't believe so.

Note that I dislike the FormView control and prefer to handle the data access programatically.

5/30/2008 7:56 PM

Anonymous K5 said...

Thanks for your reply Michael.
You might be right about the FormView. My experience with .Net 2.0 is not very extensive, so I am not yet solid on my opinions. My primary reason of using FormView was to save on my time reading/writing some 30 fields from DB.

6/02/2008 7:18 AM

Anonymous Anonymous said...

you are an ANGEL!!! 8.35 on a rainy saturday evening in Bristol, England. I can now go to the pub. thanks!

7/25/2008 12:36 PM

Anonymous Anonymous said...

Thanks so much for this info....I tried so many different things in vain..and this worked like a charm!!!

10/15/2008 8:21 AM

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.