Search code examples
asp.netsql-insertsqldatasourcecontrolparameter

Cannot Find Control ID in ControlParameter


I am trying to insert values from a textbox but I get an error that it cannot find the controlid in the controlparameter. The TextBox is inside a formview, which is inside a listview. The SqlDataSource is outside the ListView.

My InsertButton_Click Method

protected void InsertButton_Click(object sender, EventArgs e)
    {

        var ctrl = (Control)sender;
        var formview = (FormView)ctrl.NamingContainer;
        formview.ChangeMode(FormViewMode.Insert);
        SectionListView.DataSource = SectionDataSource;
        SectionListView.DataBind();

    }

The InsertItem Template

<InsertItemTemplate>
    <tr style="">
        <td>
           <div style="font-size: .8em;">
               <asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
                   <ItemTemplate>
                       <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
                       <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
                       <asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
                       <asp:TextBox ID="SectionItemTextBox" runat="server" />
                       <asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
                       <asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>

                   </ItemTemplate>
               </asp:FormView>
           </div>
      </td>
  </tr>
</InsertItemTemplate>

My SqlDataSource

<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
                    InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (@SectionItem, @SectionItemLabel)">
        <InsertParameters>
                <asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
                <asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
        </InsertParameters>
 </asp:SqlDataSource>

Solution

  • When you want to use the child controls as control parameters, you need to use the qualified id as control id. Try changing the SQL data source as below

    <asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
                        InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (@SectionItem, @SectionItemLabel)">
            <InsertParameters>
                    <asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
                    <asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
            </InsertParameters>
     </asp:SqlDataSource>