am near to cry bcz solving this problem took my whole day ,

simply, i am trying to get status of checkbox from grid view, so i converted it into tempaltefield and in .cs code i used findcontrol property but still getting error:

Object reference not set to an instance of an object.

Description: An unhandled 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:

***CheckBox chk = (CheckBox)GridViewAdminViewEmplys.SelectedRow.FindControl("chkBoxStatus"); 

***

.aspx code:

<%@ Page Language="C#" MasterPageFile="~/MasterPageAdministration.master" AutoEventWireup="true" CodeFile="adminViewEmployers.aspx.cs" Inherits="adminViewEmployers" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div class="superAdminContent">
  <h3>Employers</h3>
  <asp:TextBox ID="txtboxEmplyrName" runat="server"></asp:TextBox>
  <asp:Button ID="btnSrchEmplyrName" runat="server" Text="Search" 
   style="height: 26px" onclick="btnSrchEmplyrName_Click" />
   Or
  <asp:Button ID="btnViewAllEmplyrs" runat="server" Text="View All" 
            onclick="btnViewAllEmplyrs_Click" />      
  <asp:Button ID="btnClrScreen" runat="server" Text="Clear" />
 </div>
 <div class="superAdminGridViewContent" >
     <asp:GridView ID="GridViewAdminViewEmplys" runat="server" AllowPaging="True"
         AutoGenerateColumns="False" DataKeyNames="userid"
         DataSourceID="SqlDataSource1" AllowSorting="True" 
         OnRowCommand="GridViewAdminViewEmplys_RowCommand"  >
         <Columns>
             <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
             <asp:HyperLinkField DataNavigateUrlFields="userid" 
                 DataNavigateUrlFormatString="~/adminViewEmplyrFurther.aspx?userid={0}" 
                 DataTextField="userid" DataTextFormatString="{0}" HeaderText="Userid" 
                 InsertVisible="False" NavigateUrl="~/adminViewEmplyrFurther.aspx" 
                 SortExpression="userid" />
             <asp:BoundField DataField="fullname" HeaderText="fullname" 
                 SortExpression="fullname" />
             <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
             <asp:BoundField DataField="question" HeaderText="question" 
                 SortExpression="question" />
             <asp:BoundField DataField="answers" HeaderText="answers" 
                 SortExpression="answers" />
             <asp:BoundField DataField="doc" HeaderText="doc" SortExpression="doc" />
             <asp:BoundField DataField="roleid" HeaderText="roleid" 
                 SortExpression="roleid" />
             <%--<asp:CheckBoxField DataField="active" HeaderText="Status" 
                 SortExpression="active" ReadOnly="false" />--%>
             <asp:TemplateField HeaderText="Status" >
              <ItemTemplate> 
                <asp:CheckBox ID="chkBoxStatus" runat="server" Checked='<%# Bind("active") %>' />
              </ItemTemplate>
             </asp:TemplateField>

         </Columns>
     </asp:GridView>

     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
         AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
         DataKeyNames="userid" DataSourceID="SqlDataSource2" ForeColor="#333333" 
         Visible="false"
         GridLines="None">
         <RowStyle BackColor="#E3EAEB" />
         <Columns>
             <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
             <asp:HyperLinkField DataNavigateUrlFields="userid" 
                 DataNavigateUrlFormatString="~/adminViewEmplyrFurther.aspx?userid={0}" 
                 DataTextField="userid" DataTextFormatString="{0}" HeaderText="Userid" 
                 InsertVisible="False" NavigateUrl="~/adminViewEmplyrFurther.aspx" 
                 SortExpression="userid" />
             <asp:BoundField DataField="fullname" HeaderText="fullname" 
                 SortExpression="fullname" />
             <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
             <asp:BoundField DataField="question" HeaderText="question" 
                 SortExpression="question" />
             <asp:BoundField DataField="answers" HeaderText="answers" 
                 SortExpression="answers" />
             <asp:BoundField DataField="doc" HeaderText="doc" SortExpression="doc" />
             <asp:BoundField DataField="roleid" HeaderText="roleid" 
                 SortExpression="roleid" />
         </Columns>
         <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
         <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
         <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
         <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
         <EditRowStyle BackColor="#7C6F57" />
         <AlternatingRowStyle BackColor="White" />

     </asp:GridView>
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
         ConnectionString="<%$ ConnectionStrings:Waleed_orsfinalConnectionString7 %>" 
         DeleteCommand="DELETE FROM [tblUser] WHERE [userid] = @userid" 
         InsertCommand="INSERT INTO [tblUser] ([fullname], [email], [question], [answers], [doc], [roleid]) VALUES (@fullname, @email, @question, @answers, @doc, @roleid)" 
         SelectCommand="SELECT [userid], [fullname], [email], [question], [answers], [doc], [roleid] FROM [tblUser] where roleid=2" 
         UpdateCommand="UPDATE [tblUser] SET [fullname] = @fullname, [email] = @email, [question] = @question, [answers] = @answers, [doc] = @doc, [roleid] = @roleid WHERE [userid] = @userid">
         <DeleteParameters>
             <asp:Parameter Name="userid" Type="Int32" />
         </DeleteParameters>
         <UpdateParameters>
             <asp:Parameter Name="fullname" Type="String" />
             <asp:Parameter Name="email" Type="String" />

             <asp:Parameter Name="question" Type="String" />
             <asp:Parameter Name="answers" Type="String" />
             <asp:Parameter DbType="Date" Name="doc" />
             <asp:Parameter Name="roleid" Type="Int32" />
             <asp:Parameter Name="userid" Type="Int32" />
         </UpdateParameters>
         <InsertParameters>
             <asp:Parameter Name="fullname" Type="String" />
             <asp:Parameter Name="email" Type="String" />

             <asp:Parameter Name="question" Type="String" />
             <asp:Parameter Name="answers" Type="String" />
             <asp:Parameter DbType="Date" Name="doc" />
             <asp:Parameter Name="roleid" Type="Int32" />
         </InsertParameters>
     </asp:SqlDataSource>
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
         ConnectionString="<%$ ConnectionStrings:Waleed_orsfinalConnectionString5 %>" 
         DeleteCommand="DELETE FROM [tblUser] WHERE [userid] = @userid" 
         InsertCommand="INSERT INTO [tblUser] ([fullname], [email], [question], [answers], [doc], [roleid]) VALUES (@fullname, @email, @question, @answers, @doc, @roleid)" 
         SelectCommand="SELECT [userid], [fullname], [email], [question], [answers], [doc], [roleid],[active] FROM [tblUser] where fullname=@emplyrName AND roleid=2" 
         UpdateCommand="UPDATE [tblUser] SET [fullname] = @fullname, [email] = @email, [question] = @question, [answers] = @answers, [doc] = @doc, [roleid] = @roleid, [active]= @active WHERE [userid] = @userid">
         <DeleteParameters>
             <asp:Parameter Name="userid" Type="Int32" />
         </DeleteParameters>
         <UpdateParameters>
             <asp:Parameter Name="fullname" Type="String" />
             <asp:Parameter Name="email" Type="String" />
             <asp:Parameter Name="question" Type="String" />
             <asp:Parameter Name="answers" Type="String" />
             <asp:Parameter DbType="Date" Name="doc" />
             <asp:Parameter Name="roleid" Type="Int32" />
             <asp:Parameter Name="active" Type="Boolean" />
             <asp:Parameter Name="userid" Type="Int32" />
         </UpdateParameters>
         <InsertParameters>
             <asp:Parameter Name="fullname" Type="String" />
             <asp:Parameter Name="email" Type="String" />
             <asp:Parameter Name="question" Type="String" />
             <asp:Parameter Name="answers" Type="String" />
             <asp:Parameter DbType="Date" Name="doc" />
             <asp:Parameter Name="roleid" Type="Int32" />
             <asp:Parameter Name="active" Type="Boolean" />
         </InsertParameters>
         <SelectParameters>
             <asp:ControlParameter ControlID="txtboxEmplyrName" Name="emplyrName" Type="String" PropertyName="text" />
        </SelectParameters>
     </asp:SqlDataSource>
 </div>
</asp:Content>

.cs code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class adminViewEmployers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

// GridViewAdminViewEmplys.Visible = false;

}

protected void GridViewAdminViewEmplys_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
//Response.Write("Updated");
GridViewRow gvr = GridViewAdminViewEmplys.SelectedRow;
int index = Convert.ToInt32(e.CommandArgument);
int pk = Convert.ToInt32(GridViewAdminViewEmplys.DataKeys[index].Value);
Response.Write(pk);
//bool isChecked = (GridViewAdminViewEmplys.SelectedRow.FindControl("chkBoxStatus") as CheckBox).Checked;
//Response.Write(isChecked);
CheckBox chk = (CheckBox)GridViewAdminViewEmplys.SelectedRow.FindControl("chkBoxStatus");
if (chk.Checked)
{
Response.Write("True");
}
else
{
Response.Write("False");
}
}

}
protected void btnSrchEmplyrName_Click(object sender, EventArgs e)
{
GridViewAdminViewEmplys.Visible = true;
GridView1.Visible = false;
}
protected void btnViewAllEmplyrs_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
GridViewAdminViewEmplys.Visible = false;

}
}

Recommended Answers

All 3 Replies

I think I may have a similar example from an older project. I'll take a look.

I see you're using a Master.Page. If you run your app and view the source before the error I believe that you'll find that the master.page has renamed your control from
"chkBoxStatus" to "ctl00$chkBoxStatus".

In that case the code in your cs should read:

(CheckBox)GridViewAdminViewEmplys.SelectedRow.FindControl("ctl00$chkBoxStatus");

done
actually problem was using .SelectedRow, for which i hadn't any select button, so i used GV.Rows[index].cells[8].control[0] and it worked

Be a part of the DaniWeb community

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