User control dataset object not set to an instance

Posted on 2006-04-11
Medium Priority
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

ID: 16436838
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

EE_AutoDeleter earned 0 total points
ID: 16599889
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.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 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