Click here to Skip to main content
15,942,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
C#
protected void GridViewTrustee_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == true)
    {
        CheckBox chkisdefault = (CheckBox)GridViewTrustee.FindControl("chkisdefaultsign");
        chkisdefault.Checked = true;
    }
    else if(Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == false)
    {
        CheckBox chkisdefault = (CheckBox)GridViewTrustee.FindControl("chkisdefaultsign");
        chkisdefault.Checked = false;
    }
    else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == true)
    {
        CheckBox chkisact = (CheckBox)GridViewTrustee.FindControl("chisactive");
        chkisact.Checked = true;
    }
    else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == false)
    {
        CheckBox chkisact = (CheckBox)GridViewTrustee.FindControl("chisactive");
        chkisact.Checked = false;
    }
}

I am setting the true CheckBoxes in GridView like above code but while running having.
C#
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:


Line 335:        {
Line 336:            CheckBox chkisdefault = (CheckBox)GridViewTrustee.FindControl("chkisdefaultsign");
Line 337:            chkisdefault.Checked = true;
Line 338:        }
Line 339:        else if(Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == false)
Posted
v2

1 solution

Try like below...
C#
protected void GridViewTrustee_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == true)
        {
            CheckBox chkisdefault = (CheckBox)e.Row.FindControl("chkisdefaultsign");
            chkisdefault.Checked = true;
        }
        else if(Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == false)
        {
            CheckBox chkisdefault = (CheckBox)e.Row.FindControl("chkisdefaultsign");
            chkisdefault.Checked = false;
        }
        else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == true)
        {
            CheckBox chkisact = (CheckBox)e.Row.FindControl("chisactive");
            chkisact.Checked = true;
        }
        else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == false)
        {
            CheckBox chkisact = (CheckBox)e.Row.FindControl("chisactive");
            chkisact.Checked = false;
        }
    }
}
 
Share this answer
 
Comments
ajays3356 21-Nov-13 0:54am    
No, its not working again its showing the same error...
Okay, post the GridView mark up code here.
ajays3356 21-Nov-13 0:57am    
<asp:GridView ID="GridViewTrustee" runat="server" CellPadding="4" Width="100%" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" AutoGenerateColumns="False"
DataKeyNames="EmployeeReferenceId" OnRowCommand="GridViewTrustee_RowCommand"
OnRowDeleting="DeleteRecord" OnRowDataBound="GridViewTrustee_RowDataBound">
<columns>
<asp:TemplateField HeaderText="Actions">
<itemtemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="edit">
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this event?');">


<asp:TemplateField HeaderText="Employee Id">
<itemtemplate>
<asp:Label ID="gridEmployeeId" runat="server" Text='<%# Eval("EmployeeReferenceId") %>'>

<asp:TemplateField HeaderText="Trustee Name">
<itemtemplate>
<asp:Label ID="lblTrusteeName" runat="server" Text='<%# Eval("TrusteeName") %>'>

<edititemtemplate>
<asp:TextBox ID="txtTrusteeName" runat="server">

<asp:TemplateField HeaderText="Is Default Signatory">
<itemtemplate>
<asp:CheckBox ID="chkisdefaultsign" runat="server" Enabled="false" />


<asp:TemplateField HeaderText="Description">
<itemtemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'>
<edititemtemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("Description") %>'>

<asp:TemplateField HeaderText="Is Active">
<itemtemplate>
<asp:CheckBox ID="chisactive" runat="server" Enabled="false" />


<footerstyle backcolor="#99CCCC" forecolor="#003399">
<HeaderStyle BackColor="#64B1FF" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" />
<rowstyle backcolor="White" forecolor="#003399">
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
The CheckBoxes are Enabled="false". That might be the problem. Remove this and try once.
ajays3356 21-Nov-13 1:13am    
No.Not Working..Same Problem..

it shows on this line..
chkisdefault.Checked = true;

and while tracing it shows that chkisdefault is null

Can you tell me whether i am finding the control in gridview is correct or not..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900