User control dataset object not set to an instance

Posted on 2006-04-11
Last Modified: 2010-04-16
Hi Experts,

I'm trying to load a dropdownlistbox in a user control.    I receive the following error when I try to view the control in web browser: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.  Please find below my user control.  The code that cause the exception is DDLCountry.DataSource = dataTable.DefaultView; Your help is appreciated.

namespace CWebSite1
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public abstract class CountryDropDown : System.Web.UI.UserControl
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DDLCountry;

public delegate void ListChangedHandler(object sender, CountryListArgs e);
public event ListChangedHandler ListChanged;

protected virtual void OnListChanged(CountryListArgs e)
if (ListChanged != null)
      ListChanged(this, e);
public CountryDropDown()
     this.Init += new System.EventHandler(Page_Init);

private void Page_Load(object sender, System.EventArgs e)
if (!IsPostBack)
      DataSet DS1 = new DataSet();
      DS1 = DCPIntranet_ClassLibrary.

      if (DS1.Tables[0].Rows.Count != 0)
      DataTable dataTable = DS1.Tables[0];
      DDLCountry.DataSource = dataTable.DefaultView; /*this is were I get the exception*/
      DDLCountry.DataTextField = "Country_name";
      DDLCountry.DataBind(  );

private void Page_Init(object sender, EventArgs e)
InitializeComponent(  );
#region Web Form Designer generated code
///        Required method for Designer support - do not modify
///        the contents of this method with the code editor.
/// </summary>
private void InitializeComponent(  )
     this.Load += new System.EventHandler(this.Page_Load);

public class CountryListArgs : EventArgs
public string countrySelected;

private void OnSelectedIndexChanged(object sender, System.EventArgs e)
CountryListArgs countrylistargs = new CountryListArgs();
countrylistargs.countrySelected= DDLCountry.SelectedItem.ToString();
Question by:detroitchassis

    Author Comment

    I resolved this problem.  The code works fine except for the protected System.Web.UI.WebControls.DropDownList DropDownList1; statement which can be removed.  I also had to wire DDLCountry to the event OnSelectedIndexChanged in the event properties for the user control dropdownlist DDLCountry.  Hope this helps :) Happy Progamming... Paul

    Accepted Solution

    Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now