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 :
Error 1'System.EventArgs' does not contain a definition for'Exception'andno 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 10914 \\SKYNET\wwwroot\Budget\
How can I do this ?
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
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.
I think the image is more clear now. You said "the error handling I am doing is happenning after the update, insert or delete command". You can have more granular control on that.
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
Again, your suggestion does not resolve my problem as these event
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
GridViewDeletedEventArgs
Code
FormViewInsertedEventArgs
Do 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 :
C#
if (e is GridViewUpdatedEventArgs)
{
GridViewUpdatedEventArgs Erreur = (GridViewUpdatedEventArgs)e;
if (Erreur.Exception != null)
{
Afficher_Erreur(Erreur.Exception);
Erreur.ExceptionHandled = true;
}
}
elseif (e is FormViewInsertedEventArgs)
{
GridViewUpdatedEventArgs Erreur = (GridViewUpdatedEventArgs)e;
if (Erreur.Exception != null)
{
Afficher_Erreur(Erreur.Exception);
Erreur.ExceptionHandled = true;
}
}
I suppose your BLL class is only imposing security constraints to the users. I mean it's standings between the view and the model then its restricts some data transactions based on the user identity? or its functional aspect is more generic than that ?
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 ; )
Bechir BejaouiRank #144 · 15.9K pointsPosted Sep 8, 2010, 6:57 PM EDT
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
A C3 pointsPosted Sep 8, 2010, 8:10 PM EDT
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 BejaouiRank #144 · 15.9K pointsPosted Sep 9, 2010, 7:51 AM EDT
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 C3 pointsPosted Sep 9, 2010, 10:00 AM EDT
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 BejaouiRank #144 · 15.9K pointsPosted Sep 9, 2010, 3:19 PM EDT
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 ; )