Carl Weis
Carl Weis

Reputation: 7042

Dynamically create ASP.NET Web User Control from code - Object reference not set to an instance

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.

Upvotes: 0

Views: 2289

Answers (1)

Anders Abel
Anders Abel

Reputation: 69260

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.

Upvotes: 3

Related Questions