Hi smile,
When you are on edit mode i.e. OnRowEditing you are binding the GridView in which trying to find control from cell 5 that does not exist as GridView in edit mode. So you are getting null reference error.So you need to check the rowstate condition to handle the error.
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" Class="table table-striped table-bordered table-hover"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Invoice No.">
<ItemTemplate>
<asp:Label ID="lbl_AdNo" runat="server" Text='<%# Eval("InvoiceNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Invoice Date">
<ItemTemplate>
<asp:Label ID="lbl_Date" runat="server" Text='<%# Eval("InvoiceDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Grand Total">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("GrandTotal") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("CName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone No.">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("CPhone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("IStatus") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="chkStatus" runat="server">
<asp:ListItem>Paid</asp:ListItem>
<asp:ListItem>Unpaid</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:Label ID="lblPaid" runat="server"></asp:Label>
<asp:Label ID="lblUnpaid" runat="server"></asp:Label>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
C#
string str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select InvoiceNo,InvoiceDate,GrandTotal,CName,CPhone,IStatus from tblSales order by InvoiceNo Desc;", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
lblTotal.Text = GridView1.Rows.Count.ToString();
int Paid = 0;
int Unpaid = 0;
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowState != DataControlRowState.Edit && row.RowState.ToString() != "Alternate, Edit")
{
Label Label6 = (Label)row.FindControl("Label6");
if (Label6.Text == "Paid")
{
Paid++;
}
if (Label6.Text == "Unpaid")
{
Unpaid++;
}
}
}
this.lblPaid.Text = Paid.ToString();
this.lblUnpaid.Text = Unpaid.ToString();
}
else
{
Response.Write("No Record Found");
return;
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}