User control dataset object not set to an instance

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();
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

detroitchassisAuthor Commented:
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
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.