siri

siri

  • NA
  • 45
  • 28.2k

working with Gridview

Nov 18 2012 11:06 AM
hi experts can any one of you tell me waht is wrong in my coding so that iam getting an error


like this when iam clicking on EDit button and after that trying to update the value the am getting this error


bject 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 59:         //string newename= txt.Text ; Line 60:  Line 61:         string EName = txt.Text; Line 62:  Line 63: 

Source File: d:\gridviewedit\Default.aspx.cs    Line: 61

Stack Trace:











my code :using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings ["constr"].ToString ()); 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            griddata();
        }
    }
    private  void griddata()
       {
    SqlDataAdapter da = new SqlDataAdapter("select *from EmployeeDetails",con );
    DataSet ds = new DataSet();
    da.Fill(ds, "data");
    GridView1.DataSource = ds;
    GridView1.DataBind();

          }
   
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        con.Open();
     int id = Convert.ToInt32 (GridView1.DataKeys[e.RowIndex].Value);
        string deletequery= "delete from EmployeeDetails where id="+id;
        SqlCommand cmd = new SqlCommand(deletequery, con);
        cmd.ExecuteNonQuery();
        griddata();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        griddata();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        GridViewRow row = GridView1.Rows[e.RowIndex];

        TextBox txt = (TextBox)row.FindControl("txtEName");
        //string newename= txt.Text ;

        string EName = txt.Text;


        txt = (TextBox)row.FindControl("txtDesignation");
        //string newdesignation = txt.Text;

        string Designation = txt.Text;


        txt = (TextBox)row.FindControl("txtAddress");
        //string newaddress = txt.Text;


        string Address = txt.Text;



        txt = (TextBox)row.FindControl("txtAge");
        //string newage = txt.Text;

        string Age = txt.Text;

        string updatequry = "update EmployeeDetails set EName='" + EName + "',Designation='" +Designation + "', Address='" + Address + "',Age='" + Age + "' where id=" + id;
        SqlCommand cmd = new SqlCommand(updatequry, con);
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        griddata();
        con.Close();
    }
}
 


Answers (1)