简体   繁体   中英

RadioButton in Footer Row of Grid View

error- Object reference not set to an instance of an object. 

The error is pointing towards RadioButtonList which is present inside Footer Row of GridView . Object reference not set to an instance of an object. Description: An unhanded exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 69: 
Line 70:         //for RadioButtonLiist Control
Line 71:         gender = ((RadioButtonList)  (this.GridView1.FooterRow.FindControl("RadioButtonList2"))).SelectedValue;
Line 72: 
Line 73:         //for DropDownList                                                                                                                      

Code-

    string name, city, gender;
    //id = int.Parse(((TextBox)this.GridView1.FooterRow.FindControl("TextBox1"))).Text);
    name = ((TextBox)(this.GridView1.FooterRow.FindControl("TextBox5"))).Text;

    //for RadioButtonLiist Control
    gender = ((RadioButtonList)(this.GridView1.FooterRow.FindControl("RadioButtonList2"))).SelectedValue;



    city = ((TextBox)(this.GridView1.FooterRow.FindControl("TextBox4"))).Text;
    cmd = new SqlCommand("insert into emp values('" + name + "','" + gender + "','" + city + "')", cn);
    cmd.ExecuteNonQuery();
    disp();

Code Behind-

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
        onrowupdating="GridView1_RowUpdating" ShowFooter="True" 
        onrowcancelingedit="GridView1_RowCancelingEdit">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:TemplateField HeaderText="Id">
                <EditItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


            <asp:TemplateField HeaderText="name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# bind("name") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# bind("name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


            <asp:TemplateField HeaderText="gender">
                <EditItemTemplate>
                    <br />
                    <asp:DropDownList ID="DropDownList1" SelectedValue='<%# bind("gender") %>' runat="server">
                        <asp:ListItem>male</asp:ListItem>
                        <asp:ListItem>female</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:RadioButtonList ID="RadiaButtonList2" runat="server" Height="18px" 
                        Width="84px">
                        <asp:ListItem>male</asp:ListItem>
                        <asp:ListItem>female</asp:ListItem>
                    </asp:RadioButtonList>

                </FooterTemplate>
                <ItemTemplate>
                    <br />
                    <asp:Label ID="Label4" runat="server" Text='<%# bind("gender") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


            <asp:TemplateField HeaderText="city">
                <EditItemTemplate>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem>lucknow</asp:ListItem>
                        <asp:ListItem>kanpur</asp:ListItem>
                    </asp:RadioButtonList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# bind("city") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Add new item">
                <FooterTemplate>
                    <asp:LinkButton ID="lbInsert" runat="server" onclick="LinkButton1_Click">Insert</asp:LinkButton>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

This is because in the following code, you have mis-spelled RadioButtonList2 :-

<asp:RadioButtonList ID="RadiaButtonList2" runat="server" Height="18px" 
                        Width="84px">
                        <asp:ListItem>male</asp:ListItem>
                        <asp:ListItem>female</asp:ListItem>
                    </asp:RadioButtonList>

Thus, it is unable to find that control and you are trying to invoke a property on a Null reference!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM