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
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();
}
{
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();
}
Rajkiran SwainPosted Jan 8, 2018, 7:48 AM
Srikant MaruwadaPosted Jan 8, 2018, 7:14 AM