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.
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.
The secret of creativity is knowing how to hide your sources...
Is ListBox1 an object on your page?? U cud infact send it directly without the ref keyword at all. U would still see what u are expecting to see happen to the ListBox1 control since ListBox1 control is available for anymodule in the code behind. Try that and
see if it works. hth
I'm asking because I think the issue is that ListBox1 is null at the time you are passing it. And you DO need to use the ref keword if the receiving method used a ref parameter ..., so that's good.
Christian Calderon
Principal, Software Architec and Delivery
http://www.eki-consulting.com
http://www.linkedin.com/in/chriscalderon
usamaalam
Member
389 Points
148 Posts
Bind ListBox with DataTable
Mar 17, 2005 12:19 PM|LINK
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.ccalderon
Contributor
4732 Points
955 Posts
Re: Bind ListBox with DataTable
Mar 17, 2005 03:18 PM|LINK
Principal, Software Architec and Delivery
http://www.eki-consulting.com
http://www.linkedin.com/in/chriscalderon
usamaalam
Member
389 Points
148 Posts
Re: Bind ListBox with DataTable
Mar 18, 2005 03:16 AM|LINK
shravan79
Contributor
4734 Points
947 Posts
Re: Bind ListBox with DataTable
Mar 18, 2005 04:24 AM|LINK
ccalderon
Contributor
4732 Points
955 Posts
Re: Bind ListBox with DataTable
Mar 18, 2005 01:36 PM|LINK
Principal, Software Architec and Delivery
http://www.eki-consulting.com
http://www.linkedin.com/in/chriscalderon
usamaalam
Member
389 Points
148 Posts
Re: Bind ListBox with DataTable
Mar 19, 2005 03:05 AM|LINK