Error With User Control Options
VirtualRichard
Posted: Tuesday, September 30, 2008 1:57:06 PM

Rank: Fanatic

Joined: 9/17/2007
Posts: 265
Location: London, UK.
I'm playing a little with User Controls (getting my head round how to get Umbraco discovering properties) and have the following:

XmlTransformer.ascx
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="XmlTransformer.ascx.cs" Inherits="UmbracoUserControls.XmlTransformer" %>
<asp:Literal ID="LiteralOutput" Text="" runat="server"></asp:Literal>
XmlTransformer.ascx.cs
Code:
using System;
using System.Configuration;
using System.Collections;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Text;

namespace UmbracoUserControls
{
    public partial class XmlTransformer : System.Web.UI.UserControl
    {
        private string xmlSource;
        private string xslSource;
        private string outputString;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (((xmlSource != null) && (xmlSource != "")) && ((xslSource != null) && (xslSource != "")))
            {
                try
                {
                    if (File.Exists(xmlSource) && File.Exists(xslSource))
                    {
                    XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();

                    XPathDocument xPathDocument = new XPathDocument(xmlSource);

                    xslCompiledTransform.Load(xslSource);

                    MemoryStream memoryStream = new MemoryStream();

                    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);

                    StreamReader streamReader = new StreamReader(memoryStream);

                    xslCompiledTransform.Transform(xPathDocument, xmlTextWriter);

                    memoryStream.Position = 0;

                    outputString = streamReader.ReadToEnd();

                    streamReader.Close();
                    memoryStream.Close();

                    LiteralOutput.Text = outputString;
                    }
                    else
                    {
                        LiteralOutput.Text = "<p>One or more required files not found.</p>";
                    }
                }
                catch (Exception)
                {
                    LiteralOutput.Text = "<p>Error processing transformation.</p>";
                }
            }
            else
            {
                LiteralOutput.Text = "<p>One or more property values were not supplied.</p>";
            }
        }
        public string XmlSource
        {
            set
            {
                if ((value != null) && (value != ""))
                {
                    xmlSource = value;
                }
            }
        }
        public string XslSource
        {
            set
            {
                if ((value != null) && (value != ""))
                {
                    xslSource = value;
                }
            }
        }
    }
}

I've got the control working great in Umbraco and the properties are being discovered when I create the Macro. Game over I thought but then when I run a page containing the Macro, I get:

Code:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   UmbracoUserControls.XmlTransformer.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\richardw\My Documents\Visual Studio 2005\Projects\UmbracoUserControls\XmlTransformer\UserControls\XmlTransformer.ascx.cs:61
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

That's not a very specific message :( I think it must be related to the literal control but when I place the control onto a normal aspx page in the project, everything is fine. The filepaths to XML and XSL files are both ok, so why the error in Umbraco?

Richard

P.s. While I'm here, this is only an example but it occurs to me that maybe Umbraco has the above functionality built in. That would be useful info to know how to do...

2 * 3 * 3 * 37 : The prime factorisation of The Beast.
VirtualRichard
Posted: Tuesday, September 30, 2008 2:24:08 PM

Rank: Fanatic

Joined: 9/17/2007
Posts: 265
Location: London, UK.
Aha, what a fool I am. I found it.

What I did wrong was to fill in the details as if it was a .NET Custom Control instead of a .NET User Control. No wonder it was complaining. I was using a custom control earlier and forgot the difference.

Richard

2 * 3 * 3 * 37 : The prime factorisation of The Beast.
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.