i 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.
"SelectedRow" property doesn't work in th RowCommand event becoz it doesn't know which row in curently selected in the Row_Command event. As far as I know this property only works in SelectedIndexChanged & SelectedIndexChaning events.So
in order to get the index of the row in Row Command event,try use this...
protected void gvEmp_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "email")
{
int indRow = Convert.ToInt32(e.CommandArgument); //e.CommandArgument gives the index of the row.
GridViewRow gvr = gvEmp.Rows[indRow];
Response.Write(“Email sent to: “ + gvr.Cells[1].Text);
}
}
Marked as answer by Hunain Hafeez on Nov 12, 2012 09:29 AM
Hunain Hafee...
Member
238 Points
639 Posts
findcontrol property not working
Nov 11, 2012 07:56 PM|LINK
i 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");<%@ 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>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; } }Alsaady
Participant
1774 Points
366 Posts
Re: findcontrol property not working
Nov 11, 2012 08:24 PM|LINK
--------------
try to change this
www.rtoosh.net
Hunain Hafee...
Member
238 Points
639 Posts
Re: findcontrol property not working
Nov 11, 2012 08:27 PM|LINK
nope bro, it isn't working, same error
Alsaady
Participant
1774 Points
366 Posts
Re: findcontrol property not working
Nov 11, 2012 08:56 PM|LINK
OK
check this
http://www.codeproject.com/Articles/37207/Editable-Gridview-with-Textbox-CheckBox-Radio-Butt
http://www.sitepoint.com/forums/showthread.php?606136-gridview-with-checkbox-in-edit-template
http://stackoverflow.com/questions/11490062/how-to-modify-this-checkbox-inside-the-gridview
you will find answer
www.rtoosh.net
Hunain Hafee...
Member
238 Points
639 Posts
Re: findcontrol property not working
Nov 11, 2012 09:26 PM|LINK
ohh thanks man !
1st link worked !
just i modified:
CheckBox chk = (CheckBox)GridVIewAdminViewEmplys.SelectedRow.FindControl("chkBoxStatus");to
CheckBox chk = (CheckBox)GridVIewAdminViewEmplys.Rows[index].FindControl("chkBoxStatus");working now! :)
Alsaady
Participant
1774 Points
366 Posts
Re: findcontrol property not working
Nov 11, 2012 09:28 PM|LINK
Are you cry Now.............
www.rtoosh.net
Hunain Hafee...
Member
238 Points
639 Posts
Re: findcontrol property not working
Nov 11, 2012 09:42 PM|LINK
hahaha ! no no, not at all , i am happy now !
but i sm judt wondering that why .SelectedRow wasn't working ,
Madhu1234
Participant
1380 Points
287 Posts
Re: findcontrol property not working
Nov 12, 2012 01:28 AM|LINK
"SelectedRow" property doesn't work in th RowCommand event becoz it doesn't know which row in curently selected in the Row_Command event. As far as I know this property only works in SelectedIndexChanged & SelectedIndexChaning events.So in order to get the index of the row in Row Command event,try use this...
protected void gvEmp_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "email")
{
int indRow = Convert.ToInt32(e.CommandArgument); //e.CommandArgument gives the index of the row.
GridViewRow gvr = gvEmp.Rows[indRow];
Response.Write(“Email sent to: “ + gvr.Cells[1].Text);
}
}