chenschel 0 Newbie Poster

Hi and thanks ahead of time:
I'm new to vb.net - not sure if I'm doing this right.
I need an editable gridview with an Access database as it's source.
The findcontrol is not finding control in gridview (dropdownlist).
I have the following gridview:

<asp:GridView ID="gvRescue" 
                  runat="server" 
                  AutoGenerateColumns="False" 
                  onrowcancelingedit="gvRescue_RowCancelingEdit" 
                  onrowediting="gvRescue_RowEditing" 
                  onrowupdating="gvRescue_RowUpdating" 
                  onrowdatabound="gvRescue_RowDataBound">
                  <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate >
                            <asp:Label ID="lblID" runat="server" Text='<%# Eval("PersonPlaceID") %>' > </asp:Label>
                      </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="FirstName">
                    <ItemTemplate >
                        <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>' > </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate >
                        <asp:TextBox ID="txtFirstName" runat="server" Width="50px" Text='<%# Eval("FirstName")%>'  ></asp:TextBox>
                    </EditItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Last&lt;br&gt;Name" SortExpression="LastName">
                      <ItemTemplate> 
                          <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label> 
                      </ItemTemplate>
                      <EditItemTemplate>
                         <asp:TextBox ID="txtLastName" runat="server" Width="50px" Text='<%# Eval("LastName") %>'></asp:TextBox>
                      </EditItemTemplate>
                    </asp:TemplateField>
                    
                      <asp:TemplateField HeaderText="Role ID" >
                         <ItemTemplate>
                            <asp:Label ID="lblRoleID" runat="server" Text='<%# Eval("RoleID") %>'></asp:Label>
                         </ItemTemplate>
                         <EditItemTemplate>
                            <asp:TextBox ID="txtRoleID" runat="server" Width="50px" Text='<%# Eval("RoleID") %>'></asp:TextBox>
                         </EditItemTemplate>
                     </asp:TemplateField>
                            
                  <asp:TemplateField HeaderText="Role&lt;br&gt;(Rescue&lt;br&gt;Foster&lt;br&gt;Adopter">
                     <ItemTemplate >
                        <asp:Label ID="lblRescueRole" runat="server" Text='<%# Eval("RescueRole") %>'></asp:Label>
                     </ItemTemplate>
                    <EditItemTemplate >
                         <asp:DropDownList ID="ddRoles" runat="server" > </asp:DropDownList>
                    </EditItemTemplate>
                   </asp:TemplateField>
                              
                     <asp:TemplateField HeaderText="Address" >
                            <ItemTemplate>
                              <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
                           </ItemTemplate>
                           <EditItemTemplate>
                              <asp:TextBox ID="txtAddress" runat="server" Width="150px" Text='<%# Eval("Address") %>'></asp:TextBox>
                           </EditItemTemplate>
                       </asp:TemplateField>
                            
                       <asp:TemplateField HeaderText="City" SortExpression="City">
                           <ItemTemplate>
                               <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
                           </ItemTemplate>
                           <EditItemTemplate>
                                <asp:TextBox ID="txtCity" runat="server" Width="150px" Text='<%# Eval("City") %>'></asp:TextBox>
                           </EditItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="State ID" >
                           <EditItemTemplate>
                               <asp:TextBox ID="txtStateID" runat="server" Width="20px" Text='<%# Eval("StateID") %>'></asp:TextBox>
                           </EditItemTemplate>
                           <ItemTemplate>
                              <asp:Label ID="lblStateID" runat="server" Text='<%# Eval("StateID") %>'></asp:Label>
                           </ItemTemplate>
                         </asp:TemplateField>
                            
                       <asp:TemplateField HeaderText="State">
                            <ItemTemplate >
                                <asp:Label ID="lblState" runat="server" Text='<%# Eval("RescueState") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate >
                                 <asp:DropDownList ID="ddStates" runat="server" DataTextField="State" DataValueField="StateID"> </asp:DropDownList>
                          </EditItemTemplate>
                        </asp:TemplateField>
                                                              
                         <asp:TemplateField HeaderText="Zip" SortExpression="Zip">
                             <ItemTemplate>
                                <asp:Label ID="lblZip" runat="server" Text='<%# Eval("Zip") %>'></asp:Label>
                             </ItemTemplate>
                             <EditItemTemplate>
                                 <asp:TextBox ID="txtZip" runat="server" Width="50px" Text='<%# Eval("Zip") %>'></asp:TextBox>
                             </EditItemTemplate>
                         </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="1st Phone" >
                                <ItemTemplate>
                                    <asp:Label ID="lblPhone1" runat="server" Text='<%# Eval("Phone1") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPhone1" runat="server" Width="60px" Text='<%# Eval("Phone1") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="2nd Phone" >
                                 <ItemTemplate>
                                    <asp:Label ID="lblPhone2" runat="server" Text='<%# Eval("Phone2") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPhone2" runat="server" Width="60px" Text='<%# Eval("Phone2") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="3rd Phone" >
                                <ItemTemplate>
                                    <asp:Label ID="lblPhone3" runat="server" Text='<%# Eval("Phone3") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="Phone3" runat="server" Width="60px" Text='<%# Eval("Phone3") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Email" >
                                <ItemTemplate>
                                    <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEmail" runat="server" Width="150px" Text='<%# Eval("Email") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Website" >
                                <ItemTemplate>
                                    <asp:Label ID="lblWebsite" runat="server" Text='<%# Eval("Website") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtWebsite" runat="server" Width="150px" Text='<%# Eval("Website") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="UserName" >
                                <ItemTemplate>
                                    <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtUserName" runat="server" Width="50px" Text='<%# Eval("UserName") %>'></asp:TextBox>
                                </EditItemTemplate>
                             </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="PassWord" >
                                 <ItemTemplate>
                                    <asp:Label ID="lblPassWord" runat="server" Text='<%# Eval("UserPassWord") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPassWord" runat="server" Width="50px" Text='<%# Eval("UserPassWord") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Rescue&lt;br&gt;Contact" >
                               <ItemTemplate>
                                    <asp:Label ID="lblContact" runat="server" Text='<%# Eval("RescueContactName") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtContact" runat="server" Width="75px" Text='<%# Eval("RescueContactName") %>'></asp:TextBox>
                                </EditItemTemplate>
                             </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Rescue&lt;br&gt;Criteria" >
                                <ItemTemplate>
                                    <asp:Label ID="lblCriteria" runat="server" Text='<%# Eval("RescueCriteria") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtCriteria" runat="server" Width="200px" Text='<%# Eval("RescueCriteria") %>'></asp:TextBox>
                                </EditItemTemplate>
                             </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Comment" >
                                <ItemTemplate>
                                    <asp:Label ID="lblComment" runat="server" Text='<%# Eval("PersonPlaceComment") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtComment" runat="server" TextMode="MultiLine" Width="200px" Text='<%# Eval("PersonPlaceComment") %>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Approved&lt;br&gt;For&lt;br&gt;Website">
                                <ItemTemplate >
                                    <asp:Label ID="lblApproved" runat="server" Text='<%# Eval("Approved") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate >
                                    <asp:CheckBoxList ID="CheckBoxApproved" runat="server" >  
                                            <asp:ListItem>Approved</asp:ListItem>
                                    </asp:CheckBoxList>
                                </EditItemTemplate>
                            </asp:TemplateField>
                    
                            <asp:TemplateField HeaderText="Edit" ShowHeader="false">
                                <ItemTemplate>
                                       <asp:Button ID="btnedit" runat="server" CommandName="Edit" Text="Edit" ></asp:Button>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Button ID="btnupdate" runat="server" CommandName="Update" Text="Update" ></asp:Button>
                                    <asp:Button ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:Button>
                                 </EditItemTemplate>
                             </asp:TemplateField>
                        </Columns>
                     </asp:GridView>

' Here is where I'm trying to load the dropdownlist:

Protected Sub gvRescue_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRescue.RowDataBound

        Dim drv As DataRowView = e.Row.DataItem
        'if its a row in the datagrid
        If e.Row.RowType = DataControlRowType.DataRow Then
            If (e.Row.RowState & DataControlRowState.Edit) > 0 Then

                Dim dp As DropDownList = CType(e.Row.Cells(4).FindControl("ddRoles"), DropDownList)
             
                Dim dt As DataTable = LoadRoles()
                Dim i As Integer = 0
                For z As Integer = 0 To dt.Rows.Count - 1
                    Dim lt As New ListItem
                    lt.Text = dt.Rows(i)(0).ToString
                    dp.Items.Add(lt)
                Next
                dp.SelectedValue = drv(3).ToString
            End If
        End If
End Sub

The code is bombing out on the following line:

dp.Items.Add(lt)

Error is: object not set to an instance of an object
When I step through the code the following line has value of "nothing":

Dim dp As DropDownList = CType(e.Row.Cells(4).FindControl("ddRoles"), DropDownList)

So I think error is because dp (dropdownlist) isn't found in the gridview.
This is the code where the dropdownlist is getting it's source:

Public Function LoadRoles()
        Dim dt As New DataTable
        Dim DBConnection As OleDbConnection = Dogs.MyConnection
        Dim DBAdapter As New OleDbDataAdapter
        Try
            DBConnection.Open()

            Dim sSelect As String = "SELECT Role, RoleID from Roles ORDER BY Role;"
            Dim cmd As New OleDbCommand(sSelect)
            cmd.CommandType = CommandType.Text
            cmd.Connection = DBConnection
            DBAdapter.SelectCommand = cmd
            DBAdapter.Fill(dt)
        Catch ex As Exception
            lblMessage.Visible = True
            lblMessage.Text = ex.Message

            ' lblMessage.Text = "<font color=red>Problem accessing Role records." _
            '  & " Please contact Programmer or CaliDogs.org.</font>"
            Me.SetFocus(lblMessage)
        Finally
            DBConnection.Close()
            DBConnection.Dispose()

        End Try

This is the code for datasource of gridview - called in page_load event:

Protected Sub ShowGrid(ByVal vRescueID As Integer)
        Dim dtRescue As New DataTable
        Dim DBConnection As OleDbConnection = Dogs.MyConnection
        Dim DBAdapter As New OleDbDataAdapter
        Try
            DBConnection.Open()

            Dim sSelect As String = "SELECT PersonPlace.PersonPlaceID, State.StateID, PersonPlace_Roles.FKRoleID AS RoleID, PersonPlace.FirstName, PersonPlace.LastName, Roles.Role AS RescueRole, PersonPlace.Address, PersonPlace.City, State.StateAbbv AS RescueState, PersonPlace.Zip, PersonPlace.Phone1, PersonPlace.Phone2, PersonPlace.Phone3, PersonPlace.Email, PersonPlace.Website, PersonPlace.UserName, PersonPlace.UserPassWord, PersonPlace.RescueContactName, PersonPlace.RescueCriteria, PersonPlace.PersonPlaceComment, PersonPlace.Approved, PersonPlace_Roles.FKRoleID FROM ((PersonPlace INNER JOIN PersonPlace_Roles ON PersonPlace.PersonPlaceID=PersonPlace_Roles.FKPersonPlaceID) INNER JOIN State ON PersonPlace.StateID=State.StateID) LEFT JOIN Roles ON PersonPlace_Roles.FKRoleID=Roles.RoleID WHERE (PersonPlace_Roles.FKRoleID=3 Or PersonPlace_Roles.FKRoleID=4 Or PersonPlace_Roles.FKRoleID=5) And (PersonPlace.PersonPlaceID = 11) ORDER BY PersonPlace.LastName;"
            Dim cmd As New OleDbCommand(sSelect)
            cmd.CommandType = CommandType.Text
            cmd.Connection = DBConnection
            DBAdapter.SelectCommand = cmd
            DBAdapter.Fill(dtRescue)

            gvRescue.DataSource = dtRescue
            gvRescue.DataBind()
        Catch ex As Exception
            lblMessage.Visible = True
            lblMessage.Text = ex.Message

            ' lblMessage.Text = "<font color=red>Problem accessing Rescue/Foster/Adopter records." _
            '   & " Please contact Programmer or CaliDogs.org.</font>"
            Me.SetFocus(lblMessage)
        Finally
            DBConnection.Close()
            DBConnection.Dispose()
        End Try
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.