0

Basically I'm trying to create an instance of my Web User Control from code and I'm getting an Object Reference Not set to an Instance exception.

Here is the code behind for my user control.

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YodelShipping
{
    public partial class _Default : System.Web.UI.Page
    {
        private YodelLabel shippingLabel;

        protected void Page_Load(object sender, EventArgs e)
        {
            shippingLabel = (YodelLabel)LoadControl("~/YodelLabel.ascx");   
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            shippingLabel = new YodelLabel("express", "overnight", "Travel Toys", "555 Main Street", "Suite 102", "Hounslow", "middlesex", "TWJ 6JS", "John Doe", "0208 818 8000", "SALES", "Address Line 1", "Address Line 2", "TOWN", "County", "UB5 1AJ", "STD", "Day_Time", "1234586845", "YOUR #", "Your #", "1/1", "HAYES", "HATFIELD", "2LGBUB51aj+01000002", "(J)jd00 022 340 0100 0124");
            Controls.Add(shippingLabel);

        }
    }
}

And here is the code for my user control

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YodelShipping
{
    public partial class YodelLabel : System.Web.UI.UserControl
    {
        public YodelLabel()
        {

        }
        public YodelLabel(string productDescription, string serviceDescription, string fromCompanyName,
                          string fromAddressLine1, string fromAddressLine2, string fromTown, string fromCounty, string fromPostCode,
                          string toOrginizationName, string toPhoneNumber, string toDepartmentName, string toAddressLine1, string toAddressLine2,
                          string toTown, string toCounty, string toPostCode, string serviceCode, string dayTime, string shipmentNumber, string consignorReference, string cosigneeReference,
                          string pieceCount, string serviceCentre, string serviceHub, string routingCode, string licensePlate)
        {
            this.productDescription.Text = productDescription;
            this.serviceDescription.Text = serviceDescription;
            this.companyName.Text        = fromCompanyName;
            this.fromAddressLine1.Text   = fromAddressLine1;
            this.fromAddressLine2.Text   = fromAddressLine2;
            this.fromTown.Text           = fromTown;
            this.fromPostcode.Text       = fromPostCode;
            this.serviceCode.Text        = serviceCode;
            this.dayTime.Text            = dayTime;
            this.shipmentNo.Text         = shipmentNumber;
            this.consignorRef.Text       = consignorReference;
            this.consigneeRef.Text       = cosigneeReference;
            this.pieceCount.Text         = pieceCount;
            this.serviceCentre.Text      = serviceCentre;
            this.hub.Text                = serviceHub;
            this.routingCode.Text        = routingCode;
            this.licensePlate.Text       = licensePlate;

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

I'm just trying to create a new instance of the control and use the constructor to populate the labels on the control.

3
  • On what line do you get the exception? Commented Oct 10, 2011 at 15:40
  • 2
    Just a piece of advice, take a look at the number of parameters you are passing to the YodelLabel constructor. Consider passing one object that has these properties. Commented Oct 10, 2011 at 15:43
  • When I click the button on the page to add a new user control, it throws the exception. Commented Oct 10, 2011 at 16:01

1 Answer 1

3

I think you're doing something wrong with the instatiation. First you instantiate the control with LoadControl:

shippingLabel = (YodelLabel)LoadControl("~/YodelLabel.ascx");

then you create another instance with the constructor:

shippingLabel = new YodelLabel("express", "overnight", "Travel Toys", "555 Main Street", "Suite 102", "Hounslow", "middlesex", "TWJ 6JS", "John Doe", "0208 818 8000", "SALES", "Address Line 1", "Address Line 2", "TOWN", "County", "UB5 1AJ", "STD", "Day_Time", "1234586845", "YOUR #", "Your #", "1/1", "HAYES", "HATFIELD", "2LGBUB51aj+01000002", "(J)jd00 022 340 0100 0124");

You're creating two different control instances, by using two different methods. You should pick one of them.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.