title: GridView Error: Object reference not set to an instance of an object
body: <p>have a gridview with template fields, one column contains a dropdownlist that should be populated with a sql statement. I have created the grid dynamically and called the rowdatabound in order to access the dropdownlist but I keep recieving the error: Object reference not set to an instance of an object. Anyone have any ideas?</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"&gt;
      &lt;Columns&gt;
          &lt;asp:CommandField EditText="Add" ShowEditButton="True" /&gt;
          &lt;asp:TemplateField&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox1" runat="server" /&gt;                          
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label1" runat="server" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:DropDownList ID="DropDownList1" runat="server" /&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label2" runat="server"&gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox2" runat="server" /&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label3" runat="server" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField&gt;        
              &lt;EditItemTemplate&gt;
                  &lt;asp:DropDownList ID="DropDownList2" runat="server" /&gt;        
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label4" runat="server" /&gt;
              &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField&gt;        
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="TextBox3" runat="server" /&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label5" runat="server" /&gt;        
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:Label ID="Label6" runat="server" /&gt;
              &lt;/EditItemTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="Label7" runat="server" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>This is the code for the creation of the GridView Dynamically, this is done in the page_load event:</p>
  
  <pre><code>If Not Page.IsPostBack Then
          Dim CommittedTable As New DataTable("Committed")
  
          CommittedTable.Columns.Add("Date Posted", GetType(Date))
          CommittedTable.Columns.Add("Vendor", GetType(String))
          CommittedTable.Columns.Add("Expense Description", GetType(String))
          CommittedTable.Columns.Add("Ledger", GetType(String))
          CommittedTable.Columns.Add("Amount", GetType(String))
          CommittedTable.Columns.Add("Initials", GetType(String))
  
          For i As Integer = 0 To GridView1.Rows.Count
              Dim tableRow As DataRow = CommittedTable.NewRow()
  
              tableRow("Date Posted") = Date.Today
              tableRow("Vendor") = ""
              tableRow("Expense Description") = ""
              tableRow("Ledger") = ""
              tableRow("Amount") = ""
              tableRow("Initials") = ""
              CommittedTable.Rows.Add(tableRow)
          Next
  
          Session("CommsTable") = CommittedTable
          BindDataComm()
  
      End If
  </code></pre>
  
  <p>Lastly this is the RowDataBound event handler code: </p>
  
  <pre><code>Dim ddl As DropDownList = DirectCast(e.Row.FindControl("DropDownList1"), DropDownList)
      If ddl Is Nothing Then
          result = dbConnect(dbType.SqlServer, ConfigurationManager.AppSettings("SQLServerConnection"))
          If result = "Successful" Then
              dt = FillDataTable(dbType.SqlServer, "SELECT V_VendorNo + ' | ' + V_VendorName FROM VendorTbl")
              ddl.DataSource = dt 'it errors out here'
              ddl.DataTextField = "V_VendorNo"
              ddl.DataValueField = "V_VendorName"
              ddl.DataBind()
          End If
      End If
  </code></pre>