Gridview showing error while trying to edit records
error:
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 134: GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
Line 135: //string Invoice_No = (row.Cells[0].Controls[0] as TextBox).Text;
Line 136: string orderdate = (row.Cells[1].Controls[0] as TextBox).Text;
Line 137: string Posted_By = (row.Cells[2].Controls[0] as TextBox).Text;
Line 138:
markup:
<asp:GridView ID="GridViewCustomerORDER" runat="server" AutoGenerateColumns="False" class="table table-striped table-bordered table-hover" DataKeyNames="Invoice_No" OnRowEditing="GridViewCustomerORDER_RowEditing1" >
<AlternatingRowStyle CssClass="warning" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" CssClass="btn btn-success" BorderColor="#CCFF33" BorderStyle="Ridge" BorderWidth="2px" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CssClass="btn btn-warning" BorderColor="#CCFF33" BorderStyle="Ridge" BorderWidth="2px" OnClick="btnUpdate" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="btn btn-danger" BorderColor="#CCFF33" BorderStyle="Ridge" BorderWidth="2px" OnClick="btnCancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Invoice_No" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Invoice_No") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtInvoice_No" runat="server" Text='<%# Eval("Invoice_No") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Posted_By" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Posted_By") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPostedby" runat="server" Text='<%# Eval("Posted_By") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="OrderDate" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("OrderDate") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPosted_By" runat="server" Text='<%# Eval("OrderDate") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Branch") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBranch" runat="server" Text='<%# Eval("Branch") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerID" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("CustomerID") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCustomerID" runat="server" Text='<%# Eval("CustomerID") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Payment_Methode" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Payment_Methode") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPayment_Methode" runat="server" Text='<%# Eval("Payment_Methode") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product_Code" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Product_Code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCategory" runat="server" Text='<%# Eval("Category") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Category") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Category" runat="server" Text='<%# Eval("Category") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Brand") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Brand" runat="server" Text='<%# Eval("Brand") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery_Manager" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Delivery_Manager") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Delivery_Manager" runat="server" Text='<%# Eval("Delivery_Manager") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity_By_Customer" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Quantity_By_Customer") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Quantity_By_Customer" runat="server" Text='<%# Eval("Quantity_By_Customer") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Discount") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Discount" runat="server" Text='<%# Eval("Discount") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit_Price" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Unit_Price") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Unit_Price" runat="server" Text='<%# Eval("Unit_Price") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Bank_Name" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Bank_Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Bank_Name" runat="server" Text='<%# Eval("Bank_Name") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery_Location" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Delivery_Location") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Delivery_Location" runat="server" Text='<%# Eval("Delivery_Location") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery_Date" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Delivery_Date") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Delivery_Date" runat="server" Text='<%# Eval("Delivery_Date") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Ground_Total" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Ground_Total") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Ground_Total" runat="server" Text='<%# Eval("Ground_Total") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total_Cost" ItemStyle-Width="150">
<ItemTemplate>
<%# Eval("Total_Cost") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Total_Cost" runat="server" Text='<%# Eval("Total_Cost") %>' />
</EditItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="info" />
<RowStyle CssClass="success" />
<SelectedRowStyle CssClass="info" />
</asp:GridView>
code:
protected void GridViewCustomerORDER_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridViewCustomerORDER.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void btnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
//string Invoice_No = (row.Cells[0].Controls[0] as TextBox).Text;
string orderdate = (row.Cells[1].Controls[0] as TextBox).Text;
string Posted_By = (row.Cells[2].Controls[0] as TextBox).Text;
string branch = (row.Cells[3].Controls[0] as TextBox).Text;
string customerID = (row.Cells[4].Controls[0] as TextBox).Text;
string payment_Methode = (row.Cells[5].Controls[0] as TextBox).Text;
string product_Code = (row.Cells[6].Controls[0] as TextBox).Text;
string category = (row.Cells[7].Controls[0] as TextBox).Text;
string brand = (row.Cells[8].Controls[0] as TextBox).Text;
string delivery_Manager = (row.Cells[9].Controls[0] as TextBox).Text;
DataTable dt = ViewState["dt"] as DataTable;
//dt.Rows[row.RowIndex]["Invoice_No"] = Invoice_No;
dt.Rows[row.RowIndex]["OrderDate"] = orderdate;
dt.Rows[row.RowIndex]["Posted_By"] = Posted_By;
ViewState["dt"] = dt;
GridViewCustomerORDER.EditIndex = -1;
this.BindGrid();
}
protected void btnCancel(object sender, EventArgs e)
{
GridViewCustomerORDER.EditIndex = -1;
this.BindGrid();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[18] { new DataColumn("Invoice_No"), new DataColumn("Posted_By"), new DataColumn("OrderDate"), new DataColumn("Branch"), new DataColumn("CustomerID"), new DataColumn("Payment_Methode"), new DataColumn("Product_Code"), new DataColumn("Category"), new DataColumn("Brand"), new DataColumn("Delivery_Manager"), new DataColumn("Quantity_By_Customer"), new DataColumn("Discount"), new DataColumn("Unit_Price"), new DataColumn("Bank_Name"), new DataColumn("Delivery_Location"), new DataColumn("Delivery_Date"), new DataColumn("Total_Cost"), new DataColumn("Ground_Total") });
ViewState["dt"] = dt;
this.BindGrid();
}
}
protected void BindGrid()
{
GridViewCustomerORDER.DataSource = ViewState["dt"] as DataTable;
GridViewCustomerORDER.DataBind();
}
Igot this example code on this site
page link:
http://www.aspsnippets.com/Articles/Using-DataTable-as-Temporary-storage-table-in-ASPNet.aspx
http://www.aspsnippets.com/Articles/Find-and-validate-TextBox-and-DropDownList-controls-in-GridView-row-using-jQuery-in-ASPNet.aspx