Define DropDownList DataSource in Codebehindhttp://forums.asp.net/t/1772188.aspx/1?Define+DropDownList+DataSource+in+CodebehindSun, 04 Mar 2012 01:00:50 -050017721884843713http://forums.asp.net/p/1772188/4843713.aspx/1?Define+DropDownList+DataSource+in+CodebehindDefine DropDownList DataSource in Codebehind <p>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?</p> <pre class="prettyprint">DataSet lvDS = new DataSet(); lvDS = bizobj.sqlfetchDS(&quot;select [Client Name] from Accounts&quot;); ProvTab_Client_DropDown.DataSource = lvDS; ProvTab_Client_DropDown.DataBind();</pre> <pre class="prettyprint">Thanks,</pre> 2012-02-21T18:48:07-05:004843720http://forums.asp.net/p/1772188/4843720.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <pre class="prettyprint">Try using: DataSet lvDS = new DataSet(); lvDS = bizobj.sqlfetchDS(&quot;select [Client Name] from Accounts&quot;); ProvTab_Client_DropDown.DataSource = lvDS.Tables[0]; ProvTab_Client_DropDown.DataTextField = &quot;[Client Name]&quot;; ProvTab_Client_DropDown.DataBind(); and make sure DataSet has data inside its table 0;</pre> 2012-02-21T18:58:30-05:004843725http://forums.asp.net/p/1772188/4843725.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <p>you have to define DataTextField and DataValueField as well. Try using SqlDataAdpter :</p> <pre class="prettyprint">DataSet lvDS = new DataSet(); SqlDataAdapter myda = new SqlDataAdapter(&quot;select [Client Name] from Accounts &quot;, conn); myda.Fill(lvDS); ProvTab_Client_DropDown.DataSource = lvDS; ProvTab_Client_DropDown.DataTextField = &quot;ClientName&quot;; ProvTab_Client_DropDown.DataValueField = &quot;ClientId&quot;; ProvTab_Client_DropDown.DataBind();</pre> <p>more here:</p> <p><a href="http://www.progtalk.com/viewarticle.aspx?articleid=194">http://www.progtalk.com/viewarticle.aspx?articleid=194</a></p> 2012-02-21T19:05:01-05:004845247http://forums.asp.net/p/1772188/4845247.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <p>Thanks, but the problem persists. :(</p> 2012-02-22T12:57:34-05:004846624http://forums.asp.net/p/1772188/4846624.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <p>Hi,</p> <p>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 &quot;ProvTab_Client_DropDown.DataSource = lvDS;&quot; to see if &quot;lvDS&quot; has valid data or not.</p> <p>If still not working, please describe it in detail. Where is the ddl or how does the method sqlfetchDS() works?</p> <p>Thanks,</p> 2012-02-23T07:43:26-05:004850035http://forums.asp.net/p/1772188/4850035.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <p>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.</p> <pre class="prettyprint">&lt;ajaxToolkit:TabPanel ID=&quot;ServiceProvTab&quot; runat=&quot;server&quot; HeaderText=&quot;Services Provided&quot; &gt; &lt;ContentTemplate&gt; &lt;div id=&quot;Div1&quot; style=&quot;border: thin solid #C0C0C0; overflow: auto; height: 290px; width: 765px;&quot;&gt; &lt;asp:ListView ID=&quot;ListView3&quot; runat=&quot;server&quot; InsertItemPosition=&quot;FirstItem&quot; OnItemCommand=&quot;ListView3_OnItemCommand&quot;&gt; ..... &lt;InsertItemTemplate&gt; &lt;tr style=&quot;background-color:#F7F6F3; text-align:center;&quot;&gt; &lt;td&gt; &lt;asp:LinkButton ID=&quot;InsertButton&quot; runat=&quot;server&quot; CommandName=&quot;InsertItem&quot; Text=&quot; Insert&quot; /&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:DropDownList ID=&quot;Ins_ProvTab_Client_DropDown&quot; runat=&quot;server&quot; Width=&quot;352.5px&quot;&gt;&lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&quot;Ins_ProvTab_Service_Description2&quot; runat=&quot;server&quot; Width=&quot;352.5px&quot; Text='&lt;%#Bind(&quot;Field_1&quot;)%&gt;' ToolTip=&quot;Service Description&quot;/&gt; &lt;/td&gt; &lt;/tr&gt; ......</pre> <pre class="prettyprint">Any and All Assistance concerning this matter is greatly appreciated.</pre> 2012-02-24T20:36:36-05:004850135http://forums.asp.net/p/1772188/4850135.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <p>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..</p> <p></p> <pre class="prettyprint">protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { DropDownList ddl = (DropDownList)e.Item.FindControl(&quot;Ins_ProvTab_Client_DropDown&quot;); if(ddl != null) { /// Assign the Datasource aand other info for the DropDown Here } } }</pre> 2012-02-24T23:09:48-05:004851809http://forums.asp.net/p/1772188/4851809.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <p>Hi,</p> <p>You need to access ListView from TabPanel first, then access DropDownList from InsertItem in ListView and bind DropDownList:</p> <p>ListView lv = (ListView)ServiceProvTab.FindControl(&quot;ListView3&quot;);<br> DropDownList ddl = (DropDownList)lv.InsertItem.FindControl(&quot;Ins_ProvTab_Client_DropDown&quot;);<br> //...</p> <p>Thanks,</p> 2012-02-27T02:40:17-05:004862427http://forums.asp.net/p/1772188/4862427.aspx/1?Re+Define+DropDownList+DataSource+in+CodebehindRe: Define DropDownList DataSource in Codebehind <p>Ultimately, I wired a handler for the dropdownlist control itself and had to navigate the control collections from said handler.&nbsp;</p> <p>Thanks for all of the suggestions - from all of you.&nbsp;</p> <p>sushanth009's direction proved the most beneficial.</p> <p></p> <pre class="prettyprint">protected void PopulateRecvTabDDL(object sender, EventArgs e) { DropDownList ddl = (DropDownList)ListView4.Controls[0].Controls[1].Controls[1].FindControl(&quot;Ins_RecvTab_Client_DropDown&quot;); if (ddl != null) { sqlLogic bizobj = new sqlLogic(); ddl.DataSource = bizobj.sqlfetchDS(&quot;use hcitasset select [Account Key],[client name] from Accounts&quot;); ddl.DataTextField = &quot;client name&quot;; ddl.DataBind(); } }</pre> 2012-03-04T01:00:50-05:00