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.NullReferenceExcept
ion: 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.
DropDownLi
st DropDownList1;
protected System.Web.UI.WebControls.
DropDownLi
st DDLCountry;
public delegate void ListChangedHandler(object sender, CountryListArgs e);
public event ListChangedHandler ListChanged;
protected virtual void OnListChanged(CountryListA
rgs e)
{
if (ListChanged != null)
ListChanged(this, e);
}
public CountryDropDown()
{
this.Init += new System.EventHandler(Page_I
nit);
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DataSet DS1 = new DataSet();
DS1 = DCPIntranet_ClassLibrary.
DCPIntranet_Admin_code_to_
execute_st
ored_proce
dures.
Get_all_countries();
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.P
age_Load);
}
#endregion
public class CountryListArgs : EventArgs
{
public string countrySelected;
}
private void OnSelectedIndexChanged(obj
ect sender, System.EventArgs e)
{
CountryListArgs countrylistargs = new CountryListArgs();
countrylistargs.countrySel
ected= DDLCountry.SelectedItem.To
String();
OnListChanged(countrylista
rgs);
}
}
}