Nothing is added to the dropdown list- even though when I debug, measureDropdown has the items listed (Via non-public members--> base --> list --> non- public members --> listItem)
U r creating an instance of DropDown in 'loadDropdown' method and binding data to it, can u give it a try by binding it to instance 'measureDropdown' directly?
donpisci
Member
74 Points
178 Posts
Databind Dropdown Lists
May 28, 2012 03:07 PM|LINK
Hi Guys,
Having a bit of trouble with databinding my dropdown list and was hoping you could help.
Initially, I was using the following code, which works:
private void loadMeasuringType() { table = new DataTable(); cmd = new SqlCommand(); cmd.CommandText = "GetMeasuringTypes"; cmd.CommandType = CommandType.StoredProcedure; table = dbConn.ExecuteQuery(cmd); measureDropdown.DataSource = table; measureDropdown.DataValueField = table.Columns[0].ToString(); measureDropdown.DataTextField = table.Columns[1].ToString(); measureDropdown.DataBind(); }However, I've got a couple of dropdown lists, so thought I'd stick the following in a base page:
protected DropDownList loadDropdown(string storedProc) { DropDownList d = new DropDownList(); table = new DataTable(); cmd = new SqlCommand(); cmd.CommandText = storedProc; cmd.CommandType = CommandType.StoredProcedure; table = dbConn.ExecuteQuery(cmd); d.DataSource = table; d.DataValueField = table.Columns[0].ToString(); d.DataTextField = table.Columns[1].ToString(); d.DataBind(); return d; }This returns the required data, but when I call the method like so:
measureDropdown = loadDropdown("GetMeasuringTypes"); measureDropdown.DataBind();Nothing is added to the dropdown list- even though when I debug, measureDropdown has the items listed (Via non-public members--> base --> list --> non- public members --> listItem)
Is very confusing! Any ideas? I'm using .Net 4.0.
Ramesh T
Contributor
5101 Points
827 Posts
Re: Databind Dropdown Lists
May 28, 2012 03:19 PM|LINK
U r creating an instance of DropDown in 'loadDropdown' method and binding data to it, can u give it a try by binding it to instance 'measureDropdown' directly?
donpisci
Member
74 Points
178 Posts
Re: Databind Dropdown Lists
May 28, 2012 03:23 PM|LINK
Yes that worked a treat :-)
Here's the updated code:
protected DropDownList loadDropdown(string storedProc, DropDownList d) { table = new DataTable(); cmd = new SqlCommand(); cmd.CommandText = storedProc; cmd.CommandType = CommandType.StoredProcedure; table = dbConn.ExecuteQuery(cmd); d.DataSource = table; d.DataValueField = table.Columns[0].ToString(); d.DataTextField = table.Columns[1].ToString(); d.DataBind(); return d; }And I call it like so:
measureDropdown = loadDropdown("GetMeasuringTypes", measureDropdown); measureDropdown.DataBind();