Bind ListBox with DataTable
Hello everybody,
I have a method in my business class and I send reference of a listbox to that method to fill it. Method first fills fetches the records in a DataTable and then binds the DataTable with ListBox. Here is the code.
Code:
public void Fill(ref System.Web.UI.WebControls.ListBox thisList)
{
// Object of Data Access Layer - Related Class
DataAccessLayer.Group groupDAL = new DataAccessLayer.Group();
groupDAL.GetAll();
int count = 0 ;
DataTable dtTemporary = new DataTable();
dtTemporary.Clear();
DataColumn dc1 = new DataColumn("ID",Type.GetType("System.Int32"));
DataColumn dc2 = new DataColumn("Name",Type.GetType("System.String") );
dtTemporary.Columns.Add(dc1);
dtTemporary.Columns.Add(dc2);
while ( groupDAL.Read() )
{
count++;
DataRow rowgroup = dtTemporary.NewRow();
rowgroup["ID"] = groupDAL.ID;
rowgroup["Name"] = groupDAL.Name;
dtTemporary.Rows.Add(rowgroup);
}
groupDAL.Dispose();
thisList.DataSource = dtTemporary;
thisList.DataTextField = "Name";
thisList.DataValueField = "ID";
}
I am getting the error messages, "Object reference not set to an instance of an object" on this line.
thisList.DataSource = dtTemporary;
Thanks.