Asp.net error Object reference not set to an instance of an object for a DDL were data source is a datatable
<p>Hi has anyone else come across a similar issue as described below</p>

<p>I have a web form with several DDL and text boxes some of them have post backs some dont </p>

<p>the DDLs have 3 or 4 Values like Yes , No or Often, Regularly, Never </p>

<p>a user is able to select the values and save his selection results on a sql server and a record number is generated on the save.</p>

<p>he is also able to retrieve his results via the same record number Via a subroutine called RecordSrchId all the text boxes get populated most of the DDLs get populated and results are selected but some of the DDLs dont get selected.</p>

<p>The DDLs for example will have Yes, No results as values in the list of items. The retrieved data from the sql is presented in a datatable and the value is cascaded down via the correct ID of the items for example "mydatatable.Tables(0).Rows(0)(2).ToString" where my datatable is the datatable and (2) is the item number i get a value from mydatatable as i have tested this with a Label control so its not returning null and the DDl have the correct list of values also in the correct case formats but for some reason it will give me an error below</p>

<p>Object reference not set to an instance of an object.</p>

<p><strong>Description:</strong></p>

<pre><code>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
</code></pre>

<p><strong>Source Error:</strong></p>

<pre><code>   Line 1148:
Line 1149: crane_requirement_Frequency_40T0.Items.FindByText(mydatatable.Tables(0).Rows(0)(32).ToString).Selected = True Line 1150: Line 1151: 

Source File: C:\inetpub\wwwroot\aspx\factfind\Default.aspx.vb Line: 1149 

Stack Trace:



[NullReferenceException: Object reference not set to an instance of an object.]
   FactFindMenu.recordidsrch() in C:\inetpub\wwwroot\aspx\factfind\Default.aspx.vb:1149
   FactFindMenu.Srch_Record_Click(Object sender, EventArgs e) in C:\inetpub\wwwroot\aspx\factfind\Default.aspx.vb:1090
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 
</code></pre>

<p>the subroutine to retrive the date is below</p>

<pre><code>Private Sub recordidsrch()

Dim searchFactfnd As New SearchFactFnd ' custom class used to retrive data From sql server

Dim mydatatable As New Data.DataSet ' Dataset/table used

        mydatatable = searchFactfnd.FactFNDTableID(Rcrd_num_Txt.Text) '  populates the dataset with the function FactFNDTableID via a record Id gained from a textbox and function returns a dataset/table

       contact_name.Text = mydatatable.Tables(0).Rows(0)(1).ToString

        crane_hireCranesFrequency.Items.FindByText(mydatatable.Tables(0).Rows(0)(30).ToString)

        crane_spendPA.Text = mydatatable.Tables(0).Rows(0)(31).ToString

        crane_requirement_Frequency_40T.Items.FindByText(mydatatable.Tables(0).Rows(0)(32).ToString).Selected =True ' Line give error if i include .Selected =True 

        Label4.Text = mydatatable.Tables(0).Rows(0)(32) ' gives me a result that can be found in the DDL

end sub





The HTML code dor the DDL is 

&lt;asp:DropDownList ID="crane_requirement_Frequency_40T" runat="server" CssClass="largeinput" AutoPostBack="True"&gt;

&lt;asp:ListItem&gt;Crane Requirements Frequency&lt;/asp:ListItem&gt;

&lt;asp:ListItem&gt;Regularly&lt;/asp:ListItem&gt;

&lt;asp:ListItem&gt;Often&lt;/asp:ListItem&gt;

&lt;asp:ListItem&gt;Seldom&lt;/asp:ListItem&gt;

&lt;asp:ListItem&gt;Never&lt;/asp:ListItem&gt;

&lt;/asp:DropDownList&gt;



I have tried several variations of the below

Items.FindByText(mydatatable.Tables(0).Rows(0)(32).ToString).Selected =True 

Items.FindByvalue(mydatatable.Tables(0).Rows(0)(32).ToString).Selected =True 

ddl.selectedvalue

ddl.selecteditem

if not ispostback then

'populate

end if

ddl.clearcontents
</code></pre>

<p>tried rewriting the html ddl to below and try variations above did not work </p>

<pre><code>&lt;asp:DropDownList ID="crane_requirement_Frequency_40T" runat="server" CssClass="largeinputd"&gt;

&lt;asp:ListItem Text="Crane Requirements Frequency"Value="" /&gt;

&lt;asp:ListItem Text="Regularly" Value="Regularly" /&gt;

&lt;asp:ListItem Text="Often" Value="Often" /&gt;

&lt;asp:ListItem Text="Seldom" Value="Seldom" /&gt;

&lt;/asp:DropDownList&gt;
</code></pre>

<p>seriously stuck now any help whould be appreciated i know the values fro the data set are correct and not null and the values exist in the DDL could post back be causing this</p>
