Question : Formview - System.NullReference Exception when using findcontrol

I have a form view with an edit item, insert item and item templates.  I have coded a object datasource inserting event to set the parameters from the formview.  I get the null reference exception with the find control method of the formview.  I have never used all three formview templates before.  In the past, I have used a formview with a default mode of insert to find controls without a problem.  I really do not understand why it cannot find the control.  I am using the Ajax control toolket to render a table.  Below is the inserting event code and the markup for the page.  Any suggestions are greatly appreciated.

public void SI_Inserting(object source, ObjectDataSourceMethodEventArgs e)
    {
        //use formview instead
       
        TextBox txtIncidentDatei = (TextBox)FormView1.FindControl("txtIncidentDatei");
        DropDownList ddlSecurityi = (DropDownList)FormView1.FindControl("ddlSecurityi");
        DropDownList ddlFacilityi = (DropDownList)FormView1.FindControl("ddlFacilityi");
        DropDownList ddlRatingi = (DropDownList)FormView1.FindControl("ddlRatingi");
        DropDownList ddlProgrami = (DropDownList)FormView1.FindControl("ddlProgrami");
        DropDownList ddlCategoryi = (DropDownList)FormView1.FindControl("ddlCategoryii");
        DropDownList ddlAreai = (DropDownList)FormView1.FindControl("ddlAreai");

        e.InputParameters["socialSecurityNumber"] = "xxxxxx";
        e.InputParameters["incidentDate"] = txtIncidentDatei.Text.Trim();
        e.InputParameters["facilityWhereOccurred"] = ddlFacilityi.SelectedItem.Text;
        e.InputParameters["programWhereOccurred"] = ddlProgrami.SelectedValue;
        e.InputParameters["securityLevel"] = ddlSecurityi.SelectedItem.Text;
        e.InputParameters["riskRating"] = ddlRating.SelectedValue;
        e.InputParameters["categoryOfIncident"] = ddlCategoryi.SelectedValue;
        e.InputParameters["narrative"] = txtNarrative1.Text;
        e.InputParameters["incidentYear"] = txtYeari.Text;
        e.InputParameters["dmhId"] = "xxxxxx";
    }

 <asp:FormView ID="FormView1" runat="server" Height="230px" AllowPaging="True"
        DataSourceID="SIDetailsDataSource" OnItemInserted="FormView1_ItemInserted" OnItemUpdated="FormView_ItemUpdated" OnItemDeleted="FormView_ItemDeleted" DataKeyNames="RowNum"
        >
        <ItemTemplate>
                <asp:Label ID="lblRowNumRead" runat="server" Text='<%# Bind("RowNum") %>' Visible=false></asp:Label>
            <cc1:TabContainer ID="TabContainer1" runat="server"
    Height="350px" Width="700px" ActiveTabIndex="0"  >
                <cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="Main">
                    <HeaderTemplate>
                        Main
                    </HeaderTemplate>
                    <ContentTemplate><table><tr><td><asp:Label ID="Label1Read" runat="server" Text="Incident Date:"></asp:Label></td><td>
                        <asp:Label ID="lblIncidentDate" runat="server" Text='<%# Bind("IncidentDate", "{0:d}") %>'></asp:Label></td></tr><tr><td><asp:Label ID="Label3Read" runat="server" Text="Security Level:"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlSecurityRead" Enabled=false runat="server" Width=300px
                        DataSourceID="SqlDataSource6" DataTextField="SecurityLevel"
                        DataValueField="SecurityLevel" SelectedValue='<%# Bind("SecurityLevel") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label2Read" runat="server" Text="Facility Where Occurred"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlFacilityRead" Enabled=false runat="server" Width=300px
                        DataSourceID="SqlDataSource1" DataTextField="FacilityDescriptions"
                        DataValueField="FacilityDescriptions" SelectedValue='<%# Bind("FacilityWhereOccured") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label4Read" runat="server" Text="Rating of Recent Predictive Behavior"></asp:Label></td><td>
                        <asp:DropDownList ID="ddlRatingRead" Enabled=false runat="server" Width=150px SelectedValue='<%# Bind("RiskRating") %>'
                            DataSourceID="SqlDataSource7" DataTextField="RiskRating"
                            DataValueField="RiskRating" ></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label5Read" runat="server" Text="Program Where Occurred"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlProgramRead" Enabled=false runat="server" Width=300px
                        DataSourceID="SqlDataSource2" DataTextField="ProgramDescription"
                        DataValueField="ProgramDescription" SelectedValue='<%# Bind("ProgramWhereOccurred") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label6Read" runat="server" Text="Category Of Incident:"></asp:Label></td><td>
                        <asp:DropDownList ID="ddlCategoryRead" Enabled=false runat="server" Width=400px
                            DataSourceID="SqlDataSource8" DataTextField="SeriousIncident"
                            DataValueField="SeriousIncident" SelectedValue='<%# Bind("CategoryOfIncident") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label7Read" runat="server" Text="Residence Area"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlAreaRead" Enabled=false runat="server" Width=300px
                        DataSourceID="SqlDataSource5" DataTextField="WardDescriptions"
                        DataValueField="WardDescriptions" SelectedValue='<%# Bind("ResidenceArea") %>'></asp:DropDownList></td></tr><tr><td colspan=2><asp:CheckBox ID="ckHistoricalRead" Enabled=false runat="server" Text="Entering Historical Data" /></td></tr></table></ContentTemplate></cc1:TabPanel>
                <cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="Details"><ContentTemplate><table><tr valign=top><td><asp:Label ID="Label8Read" runat="server" Text="Incident Year:"></asp:Label></td><td><asp:TextBox ID="txtYearRead" ReadOnly=true runat="server" Text='<%# Bind("IncidentYear") %>'></asp:TextBox></td></tr><tr><td valign=top><asp:Label ID="Label9Read" runat="server" Text="Narrative:"></asp:Label></td><td><asp:TextBox ID="txtNarrative1Read" ReadOnly=true runat="server" TextMode=MultiLine Width=500px Height=195px MaxLength=1073741823 Text='<%# Bind("Narrative") %>'></asp:TextBox></td></tr></table></ContentTemplate></cc1:TabPanel></cc1:TabContainer><asp:Button ID="btnNew" runat="server" Text="New" CommandName="New" />
                                                <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" /><asp:Button
                                                    ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" /></ItemTemplate><EditItemTemplate><asp:Label ID="lblRowNum" runat="server" Text='<%# Bind("RowNum") %>' Visible=false></asp:Label><cc1:TabContainer ID="TabContainer1" runat="server"
    Height="350px" Width="700px" ActiveTabIndex="0"  >
                <cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="Main">
                    <HeaderTemplate>
                        Main
                    </HeaderTemplate>
                    <ContentTemplate><table><tr><td><asp:Label ID="Label1" runat="server" Text="Incident Date:"></asp:Label></td><td><asp:TextBox ID="txtIncidentDate" runat="server" Text='<%# Bind("IncidentDate", "{0:d}") %>'>></asp:TextBox><asp:Image ID="Image10" runat="server" ImageUrl="Calendar_scheduleHS.png" />
                            <cc1:CalendarExtender ID="CalendarExtender4" runat="server"
                        TargetControlID="txtIncidentDate" Enabled="True">
                          </cc1:CalendarExtender></td></tr><tr><td><asp:Label ID="Label3" runat="server" Text="Security Level:"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlSecurity" runat="server" Width=300px
                        DataSourceID="SqlDataSource6" DataTextField="SecurityLevel"
                        DataValueField="SecurityLevel" SelectedValue='<%# Bind("SecurityLevel") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label2" runat="server" Text="Facility Where Occurred"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlFacility" runat="server" Width=300px
                        DataSourceID="SqlDataSource1" DataTextField="FacilityDescriptions"
                        DataValueField="FacilityDescriptions" SelectedValue='<%# Bind("FacilityWhereOccured") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label4" runat="server" Text="Rating of Recent Predictive Behavior"></asp:Label></td><td>
                        <asp:DropDownList ID="ddlRating" runat="server" Width=150px SelectedValue='<%# Bind("RiskRating") %>'
                            DataSourceID="SqlDataSource7" DataTextField="RiskRating"
                            DataValueField="RiskRating" ></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label5" runat="server" Text="Program Where Occurred"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlProgram" runat="server" Width=300px
                        DataSourceID="SqlDataSource2" DataTextField="ProgramDescription"
                        DataValueField="ProgramDescription" SelectedValue='<%# Bind("ProgramWhereOccurred") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label6" runat="server" Text="Category Of Incident:"></asp:Label></td><td>
                        <asp:DropDownList ID="ddlCategory" runat="server" Width=400px
                            DataSourceID="SqlDataSource8" DataTextField="SeriousIncident"
                            DataValueField="SeriousIncident" SelectedValue='<%# Bind("CategoryOfIncident") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label7" runat="server" Text="Residence Area"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlArea" runat="server" Width=300px
                        DataSourceID="SqlDataSource5" DataTextField="WardDescriptions"
                        DataValueField="WardDescriptions" SelectedValue='<%# Bind("ResidenceArea") %>'></asp:DropDownList></td></tr><tr><td colspan=2><asp:CheckBox ID="ckHistorical" runat="server" Text="Entering Historical Data" /></td></tr></table></ContentTemplate></cc1:TabPanel>
                <cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="Details"><ContentTemplate><table><tr valign=top><td><asp:Label ID="Label8" runat="server" Text="Incident Year:"></asp:Label></td><td><asp:TextBox ID="txtYear" runat="server" Text='<%# Bind("IncidentYear") %>'></asp:TextBox></td></tr><tr><td valign=top><asp:Label ID="Label9" runat="server" Text="Narrative:"></asp:Label></td><td><asp:TextBox ID="txtNarrative1" runat="server" TextMode=MultiLine Width=500px Height=195px MaxLength=1073741823 Text='<%# Bind("Narrative") %>'></asp:TextBox></td></tr></table></ContentTemplate></cc1:TabPanel></cc1:TabContainer><asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
                                                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" /></EditItemTemplate><InsertItemTemplate><cc1:TabContainer ID="TabContainer1" runat="server"
    Height="350px" Width="700px" ActiveTabIndex="0"  >
                <cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="Main">
                    <HeaderTemplate>
                        Main
                    </HeaderTemplate>
                    <ContentTemplate><table><tr><td><asp:Label ID="Label1i" runat="server" Text="Incident Date:"></asp:Label></td><td><asp:TextBox ID="txtIncidentDatei" runat="server" Text='<%# Bind("IncidentDate", "{0:d}") %>'></asp:TextBox><asp:Image ID="Image10i" runat="server" ImageUrl="Calendar_scheduleHS.png" />
                            <cc1:CalendarExtender ID="CalendarExtender4i" runat="server"
                        TargetControlID="txtIncidentDatei" Enabled="True">
                          </cc1:CalendarExtender></td></tr><tr><td><asp:Label ID="Label3i" runat="server" Text="Security Level:"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlSecurityi" runat="server" Width=300px
                        DataSourceID="SqlDataSource6" DataTextField="SecurityLevel"
                        DataValueField="SecurityLevel" SelectedValue='<%# Bind("SecurityLevel") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label2i" runat="server" Text="Facility Where Occurred"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlFacilityi" runat="server" Width=300px
                        DataSourceID="SqlDataSource1" DataTextField="FacilityDescriptions"
                        DataValueField="FacilityDescriptions" SelectedValue='<%# Bind("FacilityWhereOccured") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label4i" runat="server" Text="Rating of Recent Predictive Behavior"></asp:Label></td><td>
                        <asp:DropDownList ID="ddlRatingi" runat="server" Width=150px SelectedValue='<%# Bind("RiskRating") %>'
                            DataSourceID="SqlDataSource7" DataTextField="RiskRating"
                            DataValueField="RiskRating" ></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label5i" runat="server" Text="Program Where Occurred"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlProgrami" runat="server" Width=300px
                        DataSourceID="SqlDataSource2" DataTextField="ProgramDescription"
                        DataValueField="ProgramDescription" SelectedValue='<%# Bind("ProgramWhereOccurred") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label6i" runat="server" Text="Category Of Incident:"></asp:Label></td><td>
                        <asp:DropDownList ID="ddlCategoryi" runat="server" Width=400px
                            DataSourceID="SqlDataSource8" DataTextField="SeriousIncident"
                            DataValueField="SeriousIncident" SelectedValue='<%# Bind("CategoryOfIncident") %>'></asp:DropDownList></td></tr><tr><td><asp:Label ID="Label7i" runat="server" Text="Residence Area"></asp:Label></td><td>
                    <asp:DropDownList ID="ddlAreai" runat="server" Width=300px
                        DataSourceID="SqlDataSource5" DataTextField="WardDescriptions"
                        DataValueField="WardDescriptions" SelectedValue='<%# Bind("ResidenceArea") %>'></asp:DropDownList></td></tr><tr><td colspan=2><asp:CheckBox ID="ckHistoricali" runat="server" Text="Entering Historical Data" /></td></tr></table></ContentTemplate></cc1:TabPanel>
                <cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="Details"><ContentTemplate><table><tr valign=top><td><asp:Label ID="Label8i" runat="server" Text="Incident Year:"></asp:Label></td><td><asp:TextBox ID="txtYeari" runat="server" Text='<%# Bind("IncidentYear") %>'></asp:TextBox></td></tr><tr><td valign=top><asp:Label ID="Label9i" runat="server" Text="Narrative:"></asp:Label></td><td><asp:TextBox ID="txtNarrative1i" runat="server" TextMode=MultiLine Width=500px Height=195px MaxLength=1073741823 Text='<%# Bind("Narrative") %>'></asp:TextBox></td></tr></table></ContentTemplate></cc1:TabPanel></cc1:TabContainer><asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Insert" />
                                                    <asp:Button ID="btnCanceli" runat="server" Text="Cancel" CommandName="Cancel" /></InsertItemTemplate></asp:FormView>

Answer : Formview - System.NullReference Exception when using findcontrol

Hi,

The controls are probably not direct childcontrols to formview. Try this

 TextBox txtIncidentDatei = FindControlRecursive(FormView1, "txtIncidentDatei") as TextBox;

.
.
.
            public static Control FindControlRecursive(Control root, string id) {
                  if ((root.ID != null) && (root.ID == id)) return root;

                  foreach (Control ctrl in root.Controls) {
                        Control found = FindControlRecursive(ctrl, id);
                        if (found != null) return found;
                  }

                  return null;
            }


/peter
Random Solutions  
 
programming4us programming4us