Try using:
DataSet lvDS = new DataSet();
lvDS = bizobj.sqlfetchDS("select [Client Name] from Accounts");
ProvTab_Client_DropDown.DataSource = lvDS.Tables[0];
ProvTab_Client_DropDown.DataTextField = "[Client Name]";
ProvTab_Client_DropDown.DataBind();
and make sure DataSet has data inside its table 0;
The code you have above is right. You need to check whether you have set the DataTextField and DataValueField in ddl. Then make sure the lvDS has data to bind the ddl. You can set a breakpoint at the line "ProvTab_Client_DropDown.DataSource = lvDS;" to see
if "lvDS" has valid data or not.
If still not working, please describe it in detail. Where is the ddl or how does the method sqlfetchDS() works?
Upon further research, I discovered that the control itself, not the DataSet was generating the error. The DropDownList for which I am trying to define a Datasource is nested inside a TabPanel. Although I have read countless threads on the matter, I have
yet to find a way to access this control from my ActiveTabChange handler (and associated event) - AND IT IS DRIVING ME MAAAAAD, MAD I TELL YOU.
Your dropdown is nested inside a ListView control.. So you cannot access your dropdown Directly. You need to first find the control Inside your ListView and then assign your functionality to it in the ItemDataBound event of the Listview..
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("Ins_ProvTab_Client_DropDown");
if(ddl != null)
{
/// Assign the Datasource aand other info for the DropDown Here
}
}
}
disgruntledd...
Member
14 Points
9 Posts
Define DropDownList DataSource in Codebehind
Feb 21, 2012 06:48 PM|LINK
I know that it's something simple (yet, still alludes me despite my efforts and web-crawling), but what am I doing to cause a NullReferenceException?
DataSet lvDS = new DataSet(); lvDS = bizobj.sqlfetchDS("select [Client Name] from Accounts"); ProvTab_Client_DropDown.DataSource = lvDS; ProvTab_Client_DropDown.DataBind();me_ritz
Star
9337 Points
1447 Posts
Re: Define DropDownList DataSource in Codebehind
Feb 21, 2012 06:58 PM|LINK
Try using: DataSet lvDS = new DataSet(); lvDS = bizobj.sqlfetchDS("select [Client Name] from Accounts"); ProvTab_Client_DropDown.DataSource = lvDS.Tables[0]; ProvTab_Client_DropDown.DataTextField = "[Client Name]"; ProvTab_Client_DropDown.DataBind(); and make sure DataSet has data inside its table 0;rajsedhain
Contributor
4181 Points
1041 Posts
Re: Define DropDownList DataSource in Codebehind
Feb 21, 2012 07:05 PM|LINK
you have to define DataTextField and DataValueField as well. Try using SqlDataAdpter :
DataSet lvDS = new DataSet(); SqlDataAdapter myda = new SqlDataAdapter("select [Client Name] from Accounts ", conn); myda.Fill(lvDS); ProvTab_Client_DropDown.DataSource = lvDS; ProvTab_Client_DropDown.DataTextField = "ClientName"; ProvTab_Client_DropDown.DataValueField = "ClientId"; ProvTab_Client_DropDown.DataBind();more here:
http://www.progtalk.com/viewarticle.aspx?articleid=194
Raj Sedhain
disgruntledd...
Member
14 Points
9 Posts
Re: Define DropDownList DataSource in Codebehind
Feb 22, 2012 12:57 PM|LINK
Thanks, but the problem persists. :(
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Define DropDownList DataSource in Codebehind
Feb 23, 2012 07:43 AM|LINK
Hi,
The code you have above is right. You need to check whether you have set the DataTextField and DataValueField in ddl. Then make sure the lvDS has data to bind the ddl. You can set a breakpoint at the line "ProvTab_Client_DropDown.DataSource = lvDS;" to see if "lvDS" has valid data or not.
If still not working, please describe it in detail. Where is the ddl or how does the method sqlfetchDS() works?
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
disgruntledd...
Member
14 Points
9 Posts
Re: Define DropDownList DataSource in Codebehind
Feb 24, 2012 08:36 PM|LINK
Upon further research, I discovered that the control itself, not the DataSet was generating the error. The DropDownList for which I am trying to define a Datasource is nested inside a TabPanel. Although I have read countless threads on the matter, I have yet to find a way to access this control from my ActiveTabChange handler (and associated event) - AND IT IS DRIVING ME MAAAAAD, MAD I TELL YOU.
<ajaxToolkit:TabPanel ID="ServiceProvTab" runat="server" HeaderText="Services Provided" > <ContentTemplate> <div id="Div1" style="border: thin solid #C0C0C0; overflow: auto; height: 290px; width: 765px;"> <asp:ListView ID="ListView3" runat="server" InsertItemPosition="FirstItem" OnItemCommand="ListView3_OnItemCommand"> ..... <InsertItemTemplate> <tr style="background-color:#F7F6F3; text-align:center;"> <td> <asp:LinkButton ID="InsertButton" runat="server" CommandName="InsertItem" Text=" Insert" /> </td> <td> <asp:DropDownList ID="Ins_ProvTab_Client_DropDown" runat="server" Width="352.5px"></asp:DropDownList> </td> <td> <asp:TextBox ID="Ins_ProvTab_Service_Description2" runat="server" Width="352.5px" Text='<%#Bind("Field_1")%>' ToolTip="Service Description"/> </td> </tr> ......sushanth009
Contributor
6243 Points
1168 Posts
Re: Define DropDownList DataSource in Codebehind
Feb 24, 2012 11:09 PM|LINK
Your dropdown is nested inside a ListView control.. So you cannot access your dropdown Directly. You need to first find the control Inside your ListView and then assign your functionality to it in the ItemDataBound event of the Listview..
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { DropDownList ddl = (DropDownList)e.Item.FindControl("Ins_ProvTab_Client_DropDown"); if(ddl != null) { /// Assign the Datasource aand other info for the DropDown Here } } }Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Define DropDownList DataSource in Codebehind
Feb 27, 2012 02:40 AM|LINK
Hi,
You need to access ListView from TabPanel first, then access DropDownList from InsertItem in ListView and bind DropDownList:
ListView lv = (ListView)ServiceProvTab.FindControl("ListView3");
DropDownList ddl = (DropDownList)lv.InsertItem.FindControl("Ins_ProvTab_Client_DropDown");
//...
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
disgruntledd...
Member
14 Points
9 Posts
Re: Define DropDownList DataSource in Codebehind
Mar 04, 2012 01:00 AM|LINK
Ultimately, I wired a handler for the dropdownlist control itself and had to navigate the control collections from said handler.
Thanks for all of the suggestions - from all of you.
sushanth009's direction proved the most beneficial.
protected void PopulateRecvTabDDL(object sender, EventArgs e) { DropDownList ddl = (DropDownList)ListView4.Controls[0].Controls[1].Controls[1].FindControl("Ins_RecvTab_Client_DropDown"); if (ddl != null) { sqlLogic bizobj = new sqlLogic(); ddl.DataSource = bizobj.sqlfetchDS("use hcitasset select [Account Key],[client name] from Accounts"); ddl.DataTextField = "client name"; ddl.DataBind(); } }