Hi,
This is regarding an ASP.NET project coded in C# using the .NET 3.5 framework.
I want to build a generic error handling
function for handling inseted, updated, deleted events from formview,
gridview and detail view. I am still a bit new to C# so I have
encoutered a simple problem that I cannot resolve and cannot find an
answer while searching google.
Right now I handle error with validator for generic validation but I need to catch BLL error.
I have different event handler for the different type of event :
protected void FormView_AjoutCompte_ItemInserted(object sender, FormViewInsertedEventArgs e) { if (e.Exception != null) { Afficher_Erreur(e.Exception); e.ExceptionHandled = true; } }
protected void GridView_ListeUtilisateurs_RowUpdated(object sender, GridViewUpdatedEventArgs e) { if (e.Exception != null) { Afficher_Erreur(e.Exception); e.ExceptionHandled = true; } }
protected void GridView_ListeUtilisateurs_RowDeleted(object sender, GridViewDeletedEventArgs e) { if (e.Exception != null) { Afficher_Erreur(e.Exception); e.ExceptionHandled = true; } }
|
I want to create a generic function that will handle them all, a function such as :
protected void HandleError(object sender, EventArgs e) { if (e.Exception != null) { Afficher_Erreur(e.Exception); e.ExceptionHandled = true; } }
|
But I get the error :
Error 1 'System.EventArgs' does not contain a definition for 'Exception' and no extension method 'Exception' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?) \\SKYNET\wwwroot\Budget\UtilisateursEtComptes\Comptes.aspx.cs 109 14 \\SKYNET\wwwroot\Budget\
|
How can I do this ?
Bechir BejaouiPosted Sep 9, 2010, 7:19 PM
The more you undestand the .Net framework , the more building pwerfull solution you can. In my point of view the design of the .Net framwork is the best one but it needs to be experienced to know how to use it appropriatly.
Anyway it is far better than Windev ; )
A CPosted Sep 9, 2010, 2:00 PM
I am using both SQL and LinQ data source. I beleive what I want to do is not possible due to the design of the .NET framework.
It appear that the
GridViewDeletedEventArgsDo not have a common root except EventArgs, which is a shame really. I can only hope this can of logic design will be implement in future version.
My formviews and gridviews and such are using a BLL Class (business logic) which is then pushing data to the TableAdapter or a LinQ to SQL DataContext. In my BLL I will raise an exception when an argument is invalid (for example user is not authorize to make this change or specific business rules) and I want to stop the processing.
Unfortunately I will have to make an ugly casting depending and if-then-else structure in my general error handling function for each type of event... one for Inserted, Updated and Delete event for FormView, GridView and DetailsView...
Is there a way to give suggestion to MS or to contact them directly regarding such question or suggestion ?
Here the kind of structure my general error handling function will look like :
if (e is GridViewUpdatedEventArgs)
{
GridViewUpdatedEventArgs Erreur = (GridViewUpdatedEventArgs)e;
if (Erreur.Exception != null)
{
Afficher_Erreur(Erreur.Exception);
Erreur.ExceptionHandled = true;
}
}
else if (e is FormViewInsertedEventArgs)
{
GridViewUpdatedEventArgs Erreur = (GridViewUpdatedEventArgs)e;
if (Erreur.Exception != null)
{
Afficher_Erreur(Erreur.Exception);
Erreur.ExceptionHandled = true;
}
}
Bechir BejaouiPosted Sep 9, 2010, 11:51 AM
Use sqldatasource as a data provider of your fromview. The advantage of that data source that is offering a couple of events like Deleting,Deleted,Inserting,Inserted, Updating,Updated those could be either pre or post execute code when an event is happened.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource_events.aspx
Of Corse you can use other data sources if you're not consuming data from sql server like Access data souce, xml data source or even linq data source
A CPosted Sep 9, 2010, 12:10 AM
The FormViewCommandEventArgs and ItemCommand event appear to be triggered when a button is clicked or when an action happen, the error handling I am doing is happenning after the update, insert or delete command (or when I raise an exception from my BLL class because the logic is invalid).
I do not see how I can use this to resolve my problem.
Bechir BejaouiPosted Sep 8, 2010, 10:57 PM
You can twick your code to make use of the itemcommad event
http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.formview.itemcommand(v=VS.80).aspx
et puis voila