TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
john kanyora
NA
242
46.3k
Object reference not set to an instance of an object
Jan 8 2018 6:47 AM
Hi, am having trouble performing CRUD operation from my gridview.am getting the error"
Object reference not set to an instance of an object"..
the error is at this line of code
"
(e.Row.Cells[2].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
below i have the codes am using..this is the aspx code for populating the gridview
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TransactionId"
OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
<Columns>
<asp:TemplateField HeaderText="CompanyID" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCompanyID" runat="server" Text='<%# Eval("CompanyID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCompanyID" runat="server" Text='<%# Eval("CompanyID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BranchID" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblBranchID" runat="server" Text='<%# Eval("BranchID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBranchID" runat="server" Text='<%# Eval("BranchID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DepartmentID" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblDepartmentID" runat="server" Text='<%# Eval("DepartmentID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDepartmentID" runat="server" Text='<%# Eval("DepartmentID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GLaccountNumber" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblGLaccountNumber" runat="server" Text='<%# Eval("GLaccountNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGLaccountNumber" runat="server" Text='<%# Eval("GLaccountNumber") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CostCenterID" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCostCenterID" runat="server" Text='<%# Eval("CostCenterID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCostCenterID" runat="server" Text='<%# Eval("CostCenterID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CostPercentage" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCostPercentage" runat="server" Text='<%# Eval("CostPercentage") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCostPercentage" runat="server" Text='<%# Eval("CostPercentage") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true"
ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="width: 150px">
CompanyID<br />
<asp:TextBox ID="txtCompanyID" runat="server" Width="140" />
</td>
<td style="width: 150px">
BranchID<br />
<asp:TextBox ID="txtBranchID" runat="server" Width="140" />
</td>
<td style="width: 150px">
DepartmentID<br />
<asp:TextBox ID="txtDepartmentID" runat="server" Width="140" />
</td>
<td style="width: 150px">
GLaccountNumber<br />
<asp:TextBox ID="txtGLaccountNumber" runat="server" Width="140" />
</td>
<td style="width: 150px">
CostCenterID<br />
<asp:TextBox ID="txtCostCenterID" runat="server" Width="140" />
</td>
<td style="width: 150px">
CostPercentage<br />
<asp:TextBox ID="txtCostPercentage" runat="server" Width="140" />
</td>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
</td>
</tr>
</table>
</form>
This is the code behind
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
{
cmd.Parameters.AddWithValue("@Action", "SELECT");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void Insert(object sender, EventArgs e)
{
string CompanyID = txtCompanyID.Text;
string BranchID = txtBranchID.Text;
string DepartmentID = txtDepartmentID.Text;
string GLaccountNumber = txtGLaccountNumber.Text;
string CostCenterID = txtCostCenterID.Text;
string CostPercentage = txtCostPercentage.Text;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "INSERT");
cmd.Parameters.AddWithValue("@CompanyID", CompanyID);
cmd.Parameters.AddWithValue("@BranchID", BranchID);
cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
cmd.Parameters.AddWithValue("@GLaccountNumber", GLaccountNumber);
cmd.Parameters.AddWithValue("@CostCenterID", CostCenterID);
cmd.Parameters.AddWithValue("@CostPercentage", CostPercentage);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnRowCancelingEdit(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int TransactionId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string CompanyID = (row.FindControl("txtCompanyID") as TextBox).Text;
string BranchID = (row.FindControl("txtBranchID") as TextBox).Text;
string DepartmentID = (row.FindControl("txtDepartmentID ") as TextBox).Text;
string GLaccountNumber = (row.FindControl("txtGLaccountNumber") as TextBox).Text;
string CostCenterID = (row.FindControl("txtCostCenterID") as TextBox).Text;
string CostPercentage = (row.FindControl("txtCostPercentage") as TextBox).Text;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "UPDATE");
cmd.Parameters.AddWithValue("@CompanyID", CompanyID);
cmd.Parameters.AddWithValue("@BranchID", BranchID);
cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
cmd.Parameters.AddWithValue("@GLaccountNumber", GLaccountNumber);
cmd.Parameters.AddWithValue("@CostCenterID", CostCenterID);
cmd.Parameters.AddWithValue("@CostPercentage", CostPercentage);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
{
(e.Row.Cells[2].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
}
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int TransactionId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "DELETE");
cmd.Parameters.AddWithValue("@TransactionId", TransactionId);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
}
Reply
Answers (
2
)
controller not found my action method name when add httpPost
QR Code Scanner from Camera