Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

so I have a visual webpart for registering/unregistering for an event on top of a custom lists' ("events"-list) display form (added via &ToolPaneView=2).

Now my use-case is this: potential Attendee opens the display form and wants to register. But he has to [get some coffee first, etc...] first - so he lets the display form openend.

While [attendee gets coffee etc...] an Editor deletes the corresponding Entry of the events-list.

Now later on, the potential attendee comes back and tries to register for the - now deleted - event. And now there's my problem: Instead of showing a) the error message as defined in my catch-block, or b) redirecting as you could see in the code I'll add, I get a server error in appliaction (you know, this yellow page which should never show up ;) ) - in this error page it shows the message, that the object is renamed or deleted by another user - but that's not wanted. I want a) error label showing the text (same as in server error page) or if that's not working because the displayform is not there b) redirect to a defined error page where I'd add the text. So, what am I missing? Would be great to get an answer!

So, this is my relevant code (I hope so, at least, ask for more if you want :) ):

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            ...
            currentEventItem = (SPListItem)SPContext.Current.Item;   
            ...
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = "Fehler: " + ex.Message 
                            + "StackTrace: " +ex.StackTrace;   
        } 
    }

and

    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        try
        {
            //trying to check if item exists here
            SPList eventslist = website.Lists["eventsListString"];
            if (eventslist.GetItemById(currentEventItem.ID) != null)
            {
                bool full = IsFull(registrations, maxRegistrations);
                ResetLabel();
                if (!full)
                {
                    ...
                }
                else
                {
                    ...
                }
            }
            else
            {
                //tried to redirect the response to an error page
                Response.Redirect("ErrorPageString");
            }
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = "Problem: " + ex.Message
            //...(loggingservice)                
        }
    }

So as I thought, the pot. attendee becomes a) redirected to my error page, or b) the catch-block is reached and I get output to my errorlabel (which is in the webpart in top of displayform... maybe that's the problem?) - but all I get is this huge messy error page.

Hope you could help me for I am out of Ideas.

Best regards, Dominik

share|improve this question
add comment

1 Answer 1

Create a base class for your web parts that handles exceptions. That way the page doesn't fail, just your web part. The error message is displayed on the page where your web part should be, the surrounding web parts are rendered normally. I use this as the base class for all my web parts.

http://andreascode.blogspot.com/2007/12/general-exception-handling-in-web-parts.html

share|improve this answer
    
First of all, thank you for your reply. But I think this won't work. The Problem is, that the whole ListItem-Object is missing when the user clicks on register, so the displayform.aspx is missing too, it's just locally shown on the users computer. So in my opinion it's not "really" the webpart which generates the exception, but the whole modal/displayform itself. Excuse me when I'm missing something. Will read through your link, though :) –  Dominik Jan 24 '13 at 14:38
    
Understood. If the ITEM no longer exists, then, yes, you will see an error if you are on the display form for that item. I just tested it with one of my exception-handling web parts and it gives the same results. Well, the code is still useful, sorry it doesn't fix your current dilemna. –  CigarDoug Jan 24 '13 at 14:52
    
yes, the code is definitely usefull and i'll send the blog post to the senior sp dev. here at university (writing my bachelor thesis) for I know they don't use something like this atm. For my use case, well... I'll wait for other answers, but it's no real problem because the exception is handled correctly. It's just the display method that's not very well, but... hey, my first SP project. First touch with asp.net/c# (they're teaching only c/c++/java here), so... nevermind ;) –  Dominik Jan 24 '13 at 15:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.