title: Null reference dropdown in gridview
body: <p>I have dynamically created a dropdown and everything is fine once the page loads. There is a button in each row of the gridview next to the dropdown. This button updates the table for the selected item.  I'm getting my key, but I&quot;m getting object reference not set to an instance. Is that because the id of the dropdown control is burried in ct100 stuff?</p>
  <p>Dim ddluid As String = CType(FindControl(&quot;Ddlos&quot;), DropDownList).SelectedItem.Value</p>
  <p>This here I have swiped from the source of the page:</p>
  <pre><code>&lt;select name=&quot;ctl00$ContentPlaceHolder2$GridView1$ctl02$Ddlos&quot; id=&quot;ctl00_ContentPlaceHolder2_GridView1_ctl02_Ddlos&quot;&gt;
                  &lt;option value=&quot;0&quot;&gt;-Select-&lt;/option&gt;
                  &lt;option value=&quot;503841a3-c615-4e4d-8cf5-20fbddbf7a7f&quot;&gt;John Doe&lt;/option&gt;
                  &lt;option value=&quot;9b38e9cd-f16f-4fdd-a82e-501c866f9e45&quot;&gt;Sam Beacon&lt;/option&gt;
                  &lt;option value=&quot;fe6158c5-a549-443f-b5ff-c011937db1d7&quot;&gt;John Doey&lt;/option&gt;
                  &lt;option value=&quot;295e9f85-6ea6-46ec-9c00-c38d64023517&quot;&gt;Scot King&lt;/option&gt;
  
   Protected Sub btnsaveinfo(sender As Object, e As EventArgs)
          Dim key As String = &quot;&quot;
          For Each row As GridViewRow In GridView1.Rows
              If row.RowType = DataControlRowType.DataRow Then
                  key = GridView1.DataKeys(row.RowIndex).Value.ToString()
                  Dim ddluid As String = CType(FindControl(&quot;Ddlos&quot;), DropDownList).SelectedItem.Value
                  'Dim idvalue As String = ddluid.SelectedValue
                  'Dim louid As Guid =
                  Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;sqlConnectionString&quot;).ConnectionString)
                  Dim cmdupdate As New SqlCommand(&quot;update Profiles SET [loanrepid] = @loanrepid WHERE ApplicantID=@ApplicantID&quot;, cn)
                  cmdupdate.Parameters.AddWithValue(&quot;@loanrepid&quot;, &quot;1&quot;)
                  cmdupdate.Parameters.AddWithValue(&quot;@ApplicantID&quot;, key)
                  cmdupdate.CommandType = System.Data.CommandType.Text
                  cmdupdate.Connection = cn
                  cn.Open()
                  cmdupdate.ExecuteNonQuery()
                  cn.Close()
              End If
          Next
          lblresult.Text = &quot; Details Updated Successfully&quot;
          lblresult.Visible = True
      End Sub
  
  
  
            &lt;asp:TemplateField HeaderText=&quot;Loan Officer&quot; SortExpression=&quot;Loan Officer&quot; ItemStyle-HorizontalAlign=&quot;Center&quot;&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:DropDownList ID=&quot;Ddlos&quot; runat=&quot;server&quot; Visible=&quot;false&quot;&gt;&lt;/asp:DropDownList&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  </code></pre>