[Solved] ASP.Net GridView Row Edit Error: Object reference not set to an instance of an object.

indradeo
 
on Aug 11, 2021 12:25 AM
Sample_104642.zip
511 Views

Dear Sir,

 

Kindly check and help to remove mention error. While i have click on EDIT button , I have getting mention error. Please help me sir.

Server Error in '/' Application.


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 114:                if (empId.Trim().ToLower() == Convert.ToString(Session["EmployeeID"]).Trim().ToLower())
Line 115:                {
Line 116:                    (e.Row.FindControl("btn_Edit") as Button).Visible = true;
Line 117:                }
Line 118:                else

Source File: c:\Users\Administrator\Documents\Visual Studio 2012\Projects\Cntrct_clsr\Cntrct_clsr\WebForm1.aspx.cs    Line: 116 Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Cntrct_clsr.WebForm1.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\Cntrct_clsr\Cntrct_clsr\WebForm1.aspx.cs:116
   System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +116
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +184
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3839
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +67
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +15
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +133
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +34
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +151
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +76
   System.Web.UI.WebControls.GridView.DataBind() +10
   Cntrct_clsr.WebForm1.ShowData() in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\Cntrct_clsr\Cntrct_clsr\WebForm1.aspx.cs:61
   Cntrct_clsr.WebForm1.GridView1_RowEditing(Object sender, GridViewEditEventArgs e) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\Cntrct_clsr\Cntrct_clsr\WebForm1.aspx.cs:70
   System.Web.UI.WebControls.GridView.OnRowEditing(GridViewEditEventArgs e) +128
   System.Web.UI.WebControls.GridView.HandleEdit(Int32 rowIndex) +44
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +748
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +92
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +97
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +120
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +272
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4330.0

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" CssClass="auto-style11"
                OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" 
                OnRowUpdating="GridView1_RowUpdating"  OnRowDataBound="GridView1_RowDataBound" Width="345px"
                BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" Height="75px">
                <Columns>
                    <asp:TemplateField HeaderText="SN">
                         <ItemTemplate>
                          <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("Sr_no") %>'></asp:Label>
                             </ItemTemplate>
                        </asp:TemplateField>
                  
                      <asp:TemplateField HeaderText="empid">

                        <ItemTemplate>
                            <asp:Label ID="lbl_emp" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
                            <asp:HiddenField ID="hfEmployeeId" runat="server" Value='<%#Eval("EmployeeID") %>'></asp:HiddenField>
                        </ItemTemplate>

                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Package Name">
                        <ItemTemplate>
                            <asp:Label ID="pkg_name" Text='<%#Eval("pkg_name") %>' runat="Server" Style="width: 200px; height: 20px" ></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Agency/EIC">
                        <ItemTemplate>
                            <asp:Label ID="agncy_eic"  Text='<%#Eval("agncy_eic") %>' runat="Server" Style="width: 200px; height: 20px"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Date of Start">
                        <ItemTemplate>
                            <asp:Label ID="s_dt" Text='<%#Eval("s_dt") %>' runat="Server" Style="width: 200px; height: 20px"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Date of Completion">
                        <ItemTemplate>
                            <asp:Label ID="c_dt" Text='<%#Eval("c_dt") %>' runat="Server" Style="width: 200px; height: 20px"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Time extension upto">
                        <ItemTemplate>
                            <asp:Label ID="tm_ext_upto_dt" Text='<%#Eval("tm_ext_upto_dt") %>' runat="Server" Style="width: 200px; height: 20px"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
 
                            <asp:TextBox ID="txt_tm_ext_upto_dt" Text='<%# Eval("tm_ext_upto_dt") %>' runat="Server" Style="width: 200px; height: 20px" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Contract Closing/Target date">
                        <ItemTemplate>
                        <asp:Label ID="cntrt_cls_trgt_dt" Text='<%#Eval("cntrt_cls_trgt_dt") %>' runat="Server" Style="width: 200px; height: 20px"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txt_cntrt_cls_trgt_dt" Text='<%# Eval("cntrt_cls_trgt_dt") %>' runat="Server" Style="width: 200px; height: 20px"/>
 
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Contract Closing date">
                        <ItemTemplate>
                            <asp:Label ID="cntrt_cls_dt" Text='<%#Eval("cntrt_cls_dt") %>' runat="Server" Style="width: 200px; height: 20px"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txt_cntrt_cls_dt" Text='<%# Eval("cntrt_cls_dt") %>' runat="Server" Style="width: 200px; height: 20px" />
 
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-1">
                        <ItemTemplate>
                            <asp:Label ID="ccp_one" runat="server" Text='<%#Eval("ccp_one") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-2">
                        <ItemTemplate>
                            <asp:Label ID="ccp_two" runat="server" Text='<%#Eval("ccp_two") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                         
                            <asp:DropDownList ID="DropDownList2" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate> 
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-3">
                        <ItemTemplate>
                            <asp:Label ID="ccp_three" runat="server" Text='<%#Eval("ccp_three") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList3" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-4">
                        <ItemTemplate>
                            <asp:Label ID="ccp_four" runat="server" Text='<%#Eval("ccp_four") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList4" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-5">
                        <ItemTemplate>
                            <asp:Label ID="ccp_five" runat="server" Text='<%#Eval("ccp_five") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList5" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-6">
                        <ItemTemplate>
                            <asp:Label ID="ccp_six" runat="server" Text='<%#Eval("ccp_six") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList6" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-7">
                        <ItemTemplate>
                            <asp:Label ID="ccp_seve" runat="server" Text='<%#Eval("ccp_seve") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList7" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-8">
                        <ItemTemplate>
                            <asp:Label ID="ccp_eight" runat="server" Text='<%#Eval("ccp_eight") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList8" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-9">
                        <ItemTemplate>
                            <asp:Label ID="ccp_nine" runat="server" Text='<%#Eval("ccp_nine") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList9" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-10">
                        <ItemTemplate>
                            <asp:Label ID="ccp_ten" runat="server" Text='<%#Eval("ccp_ten") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList10" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-11">
                        <ItemTemplate>
                            <asp:Label ID="ccp_ele" runat="server" Text='<%#Eval("ccp_ele") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList11" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-12">
                        <ItemTemplate>
                            <asp:Label ID="ccp_tw" runat="server" Text='<%#Eval("ccp_tw") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList12" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="CCP-13">
                        <ItemTemplate>
                            <asp:Label ID="ccp_thre" runat="server" Text='<%#Eval("ccp_thre") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList13" runat="server">
                                <asp:ListItem>Y</asp:ListItem>
                                <asp:ListItem>N</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Remark">
                        <ItemTemplate>
                            <asp:Label ID="rmrk" runat="server" Text='<%#Eval("rmrk") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="rmrk" Text='<%# Eval("rmrk") %>' runat="Server" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btn_Edit" runat="server" CommandName="Edit" Text="Edit" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Button ID="btn_Update" runat="server" CommandName="Update" Text="Update" />
                            <asp:Button ID="btn_Cancel" runat="server" CommandName="Cancel" Text="Cancel" />
                        </EditItemTemplate>
 
                    </asp:TemplateField>
                    <asp:TemplateField>
                         <ItemTemplate>
                <asp:Button ID="btn_Closed" runat="server" Enabled="true" Text="Closed" CssClass="btn btn-primary" OnClientClick="return confirm('Are you sure you want to Closed this Contract?');" Visible="false"/>
            </ItemTemplate>
        </asp:TemplateField>

                   
                </Columns>
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <HeaderStyle BackColor="#A55129" ForeColor="#ffffff" Font-Bold="True" />
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <RowStyle ForeColor="#8C4510" BackColor="#FFF7E7" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FFF1D4" />
                <SortedAscendingHeaderStyle BackColor="#B95C30" />
                <SortedDescendingCellStyle BackColor="#F1E5CE" />
                <SortedDescendingHeaderStyle BackColor="#93451F" />
            </asp:GridView>

        
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string empId = (e.Row.FindControl("hfEmployeeId") as HiddenField).Value;
                if (empId.Trim().ToLower() == Convert.ToString(Session["EmployeeID"]).Trim().ToLower())
                {
                    (e.Row.FindControl("btn_Edit") as Button).Visible = true;
                }
                else
                {
                    (e.Row.FindControl("btn_Edit") as Button).Visible = false;
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string empId = (e.Row.FindControl("hfEmployeeId") as HiddenField).Value;
                if (empId.Trim().ToLower() == Convert.ToString(Session["EmployeeID"]).Trim().ToLower())
                {
                    (e.Row.FindControl("btn_Closed") as Button).Visible = true;
                }
                else
                {
                    (e.Row.FindControl("btn_Closed") as Button).Visible = false;
                }
            }
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 11, 2021 06:47 AM
indradeo says:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string empId = (e.Row.FindControl("hfEmployeeId") as HiddenField).Value;
                if (empId.Trim().ToLower() == Convert.ToString(Session["EmployeeID"]).Trim().ToLower())
                {
                    (e.Row.FindControl("btn_Edit") as Button).Visible = true;
                }
                else
                {
                    (e.Row.FindControl("btn_Edit") as Button).Visible = false;
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string empId = (e.Row.FindControl("hfEmployeeId") as HiddenField).Value;
                if (empId.Trim().ToLower() == Convert.ToString(Session["EmployeeID"]).Trim().ToLower())
                {
                    (e.Row.FindControl("btn_Closed") as Button).Visible = true;
                }
                else
                {
                    (e.Row.FindControl("btn_Closed") as Button).Visible = false;
                }
            }
 
        }

Replace above with below code.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) != DataControlRowState.Edit)
    {
        string empId = (e.Row.FindControl("hfEmployeeId") as HiddenField).Value;
        if (empId.Trim().ToLower() == Convert.ToString(Session["EmployeeID"]).Trim().ToLower())
        {
            (e.Row.FindControl("btn_Closed") as Button).Visible = true;
            (e.Row.FindControl("btn_Edit") as Button).Visible = true;
        }
        else
        {
            (e.Row.FindControl("btn_Closed") as Button).Visible = false;
            (e.Row.FindControl("btn_Edit") as Button).Visible = false;
        }
    }
}