2

I have an ASP.NET 4.5 web forms application using model binding. I've created a business logic layer as per this tutorial: http://www.asp.net/web-forms/tutorials/data-access/model-binding/adding-business-logic-layer

So the InsertMethod of my FormView is in the appropriate BLL class. The BLL class is defined as per above example in my OnCallingDataMethods method of the code-behind. The OnItemInserted method however, I've defined directly in my page's code-behind class, as I feel it is not business logic per say but rather front-end type logic to give the user feedback based on the completed insert.

My problem is that the AffectedRows property in the OnItemsInserted method is always -1, regardless of the Insert's result. So I can never validate whether the insert was successful or not as per this MSDN example: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserted.aspx

Is the AffectedRows property faulty or not populated in the FormViewInsertedEventArgs collection when using the new model binding technique of ASP.Net 4.5 Web Forms model binding? Or am I doing something wrong?

Neville
  • 1,080
  • 1
  • 12
  • 28
  • Not 100%. It certainly seems from my testing that the AffectedRows is not populated when using .Net 4.5 WebForms Model Binding + EF 5. I did a quick test using an EntityDataSource instead of Model Binding and then the property was populated, but I wanted to use the new Model Binding technique specifically. Seems like if the Item_Inserted event fires, you can be pretty sure that your insert was successful. Provided you have a robust business layer class that wraps your SaveChanges() method in a try-catch checking for any DbUpdateException erros. – Neville Nov 18 '13 at 15:42
  • I saw your other solution on the ASP.NET Forums where you were checking for the model being valid. I liked that and went that route. – Don Nov 20 '13 at 21:45
  • Thanks Don. Almost forgot about that myself :-) Will post it here for ease of access. – Neville Nov 21 '13 at 08:17

1 Answers1

1

My solution was a combination of above mentioned business layer checking for any model erros and DBUpdateException and then this in the ItemInserted event of the FormView:

protected void personForm_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        if (this.ModelState.IsValid)
        {
            labResponse.Text = "New record added. Woohoo!";
        }
        else
        {
            labResponse.Text = "";
        }
        personForm.DefaultMode = FormViewMode.Insert;
    }

This seems to work in my app and sort of covers my needs. Seems like from my testing, that if there is a model error it will never get to this event anyway, so it is safe to assume that if the modelState is valid and you've reached the OnInserted event, using EF 5 + Model Binding, your record has successfully been inserted.

We would definitely like to see more elaborate examples on MSDN and documentation on Model Binding in ASP Webforms as the current series of tutorials are very rudamentory to say the least.

Neville
  • 1,080
  • 1
  • 12
  • 28