Sign In  Sign Up Live-Chat

capture exception when saving a document

Last post 06-05-2008, 8:38 PM by mf. 5 replies.
Sort Posts: Previous Next
  •  05-29-2008, 11:10 AM 129257

    capture exception when saving a document

    Hey,

     

    I’m evaluating you tool Aspose.Editor for .NET 2.0. My question is  how to capture error when a document is saved. I use the demo project from your web site. Your help is greatly appreciated. I need to make a decision very soon to use this tool in my web application.

     

    Thanks,

     

    Mike

     

    Step 1.  User clicks the SAVE icon in the toolbar

     

    Step 2. In aspx code behind as shown below, If there  is an error thrown by DocumentStorage.StoreDocument, how can I display the error on the same page ?

     

    /// <summary>

    /// This method is called when the server receives a document from the client.

    /// </summary>

    private void OnDocumentReceived(object sender, DocumentEventArgs e)

    {

    try

    {

    // Retrieve the document into a memory stream. You can request the document in any format.

    MemoryStream docStream = e.GetDocument(DocumentFormat.WordML);

    // Store the document. In this demo we store the document to the document cache.

    // In a real-word application it is up to you where to store the document,

    // it could be anywhere (file system, database, sharepoint etc).

    string fileName = e.Param;

    DocumentStorage.StoreDocument(fileName, docStream);

    }

    catch(Exception ex)

    {

    //how can I capture exception?

    //throw new ApplicationException("{ DemoDocumentStorage - StoreDocument -: " + ex.Message.ToString() + "}");

    }

    }

     
  •  05-30-2008, 1:02 AM 129331 in reply to 129257

    Re: capture exception when saving a document

    Hi,
    if you want to throw exception on server and display its message on client (page)then you don't have to do anything special. Just don't catch it on server.

    Here is how it's implemented in server code:

    /// <summary>
    /// Analyses a client request and fires <see cref="DocumentReceived"/> and
    /// <see cref="DocumentRequested"/> events.
    /// </summary>
    public
    void OnPageLoad()
    {
       
    ...
       try
       {
          ...
          // Here goes code in DocumentRequested and/or DocumentReceived event handler
          ...

       }
       catch ( Exception ex )
       {
          Page.Response.StatusCode = 500;
    // HttpStatusCode.InternalServerError;
          Page.Response.StatusDescription = ex.Message;
       }
       Page.Response.Flush();
       Page.Response.End();
    }

    Basically if exception is thrown on server it's caught and packed into the server response 500 which is sent to the client where it's received and re-thrown.

    If this doesn't answer your question, please provide more details.

    Kind regards,
    Michael

     
  •  06-04-2008, 12:01 PM 130068 in reply to 129257

    Re: capture exception when saving a document

    Thanks Michael for the help. Catching the exception works fine. I do have another questions. After saving a document to the database I save the document ID in session variable so I can retrieve it and send it to another aspx page. It seems the sessions and viewstate variables are reset after a postback. Any way to capture the document id?

    Thanks

     
  •  06-04-2008, 7:28 PM 130104 in reply to 130068

    Re: capture exception when saving a document

    Hi,
    if my understanding is correct in the DocumentReceived event handler you save document Id into the session variable and later when page makes a post back you want to get this Id back.

    You need to make sure than session Id you see in the DocumentReceived handler is the same as you see during post back. If values are same then you should be able to access your document Id, however if values are different then client editor uses new session each time it saves document to the server. The reason for this is that session Id cookie is not passed to the editor because of the "httponly" flag introduced starting from .NET 2.0.

    As a workaround for this you can save document Id into the viewstate of the page before document is sent to the server, you will pass this id as a param to the Save method. On the server this id is used and during post back this id is sent to the server. Even if your sessions are different, both will get same id.

    Kind regards,
    Michael

     
  •  06-05-2008, 3:39 PM 130244 in reply to 129257

    Re: capture exception when saving a document

    Hi Michael,

    Thanks for the reply, but my question wasn't very clear. What I'm doing is storing a new a new document in a SQL database with the strored procedure returning the new document ID.  I need to pass this id onto another web page.

    Thanks
    Tom

     

     
  •  06-05-2008, 8:38 PM 130277 in reply to 130244

    Re: capture exception when saving a document

    Hi,
    there are several ways to do this. None is trivial though.

    1) Easy. Duplicate session cookie and pass it to the editor so it can work in the same session as the page itself. In this way you will be able to save document Id in the session variable.
    Alternatively you can pass session Id to the page in any other way (e.g. encode and embedd) and then append it to the param which goes along with the document to the server. On the server you should be able to use this session Id to get directly to the required session state and save document Id there.

    2) Advanced. Use client side jscript to send document to the server. Don't call Send() on the control, but use SaveChannel() and then XMLHttpRequest (http://en.wikipedia.org/wiki/XMLHttpRequest) to save document. After document is saved on the server create cookie with document Id and add it to the response. On the client side get this cookie from the response and put it into the view state. When page posts back you can get document Id from the view state.

    3) Questionable. After document is saved on the server throw exception which carries document Id. On the client side catch it and get the document Id from it. Save document Id to the view state. When page posts back you can get document Id from the view state.

    Kind regards,
    Michael

     
View as RSS news feed in XML