Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception on adding / closing Web Part in SharePoint

Status
Not open for further replies.

vincenthandle

Programmer
Jun 3, 2009
2
NL
Hello,

I have created a Web Part using an web service to gather some data. I made it possible to change the web service url for the user, using the Tool Bar properties (modify webpart option). Ofcourse it is possible the user enters an invalid url so I have handled all expected exceptions, those I throw from the objectdatasource (which is calling the web service) and catch it on databinding as an TargetInvocationException, this works perfectly when the webpart is used properly.

Here is my problem, when you add the webpart, using a wrong url (when you add it the first time and a valid url is not set) AND when you close the webpart on the sharepoint page, using a wrong web service url, it will crash to the default ASP error page (Server Error in '/' Application.) telling me:

Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'.
The request failed with the error message: The file you are attempting to save or retrieve has been blocked from this Web site by the server administrators.

This is exactly the exception I receive in my catch area and is handled properly when u use the webpart normally, however the datasource is called when you close the webpart on the page and no error is handled. I Stepped through it, I even override every Page Life Cyle Event and make a try/catch around the base.OnPreRender(e) etc (and its internal code, produced by me). The error is nowhere catched and thrown as and error, making sharepoint crash. When you reload sharepoint, the web part is not closed when you closed it, when you added the webpart the webpart is added and will now show the user friendly error catched and created by me.

Has anyone had this problem before or do you know the solution, I will be very pleased!
If you need more information I will try to provide it.
Thanks in advance
 
The datasource file:

ServiceBinding serviceBinding = new ServiceBinding(webServiceUrl);
try
{
items = serviceBinding.GetChildren(new Guid?(), true, CatalogItemTypes.Folder);
}
catch (SoapOriginalException)
{
throw;
}
catch (WebException)
{
// Invalid Url for webservice
throw;
}
catch (InvalidOperationException)
{
// Wrong Url given
throw;
}

Then I change do not throw the error up but return null, the web part closes just fine, but I need to catch the error to show to the user.

The main page, performing the databind, showing the error:
try
{
_gridViewExplore.DataBind();
}
catch (TargetInvocationException e)
{
if (e.InnerException.GetType() == typeof(WebException))
{
// show error message
}
else if (e.InnerException.GetType() == typeof(InvalidOperationException))
{
// show error message
}
else
{
Log.Write(this, LogLevel.Error, "GridView DataBind error", e);
// Unexpected error, let the client crash!
throw;
}
}

You would expect when you close the webpart the page is rendered, it will catch the databind and show one of the error messages or write something to the log file, however the datasource seems to be called externaly because at the databind, it never reaches the catch area.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top