1
Answer

problem with gridview

archana a

archana a

16y
4.3k
1
Hi Frineds,

This is my code for a simple gridview with edit,update,delete methods..
private void Page_Load(object sender, System.EventArgs e)
       {
           if (!IsPostBack)
           {
               BindData();
           }
       }

      public void BindData()
      {
          string connectionString = "server=system;Database=Sample;uid=sa;pwd=veena123";
          SqlConnection con = new SqlConnection(connectionString);
          SqlDataAdapter da = new SqlDataAdapter("select * from Emp", con);
          DataSet ds = new DataSet();
          da.Fill(ds);
          GridView1.DataSource = ds;
          GridView1.DataBind();
      }

       protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
       {
           GridView1.EditIndex = e.NewEditIndex; BindData();
       }

       protected void GridView1_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
       {
           GridView1.EditIndex = -1; BindData();
       }

       protected void GridView1_RowUpdating(object sender, GridViewUpdatedEventArgs e)
       {
                      Label lblEmpNo = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEmpNo") as Label;
           TextBox txtName1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
           TextBox txtSalary1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
           TextBox txtDOB1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox;
           TextBox txtDOTrans1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox;
           TextBox txtDesignation1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5") as TextBox;
           TextBox txtDepartment1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6") as TextBox;

           String connectionString = "server=system;database=sample;uid=sa;pwd=veena123";
           SqlConnection con = new SqlConnection(connectionString);
           SqlCommand cmd = new SqlCommand("Update emp set Emp_Name='" + txtName1.Text + "','" + txtSalary1.Text + "','" + txtDOB1.Text + "','" + txtDOTrans1.Text + "','" + txtDesignation1.Text + "','" + txtDepartment1.Text + "' where Emp_Number= '" + lblEmpNo.Text + "'", con);
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();
           }
       protected void GridView1_RowDeleting(object sender, GridViewDeleteEventHandler e)
       {
           Label lblEmpNo = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEmpNo");
           TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
           TextBox txtSalary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
           TextBox txtDOB = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
           TextBox txtDOTrans = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
           TextBox txtDesignation = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
           TextBox txtDepartment = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");

           string str ="delete from emp where Emp_Number='"+ lblEmpNo.Text +"'";
           String connectionString = "server=system;database=sample;uid=sa;pwd=veena123";
           SqlConnection con = new SqlConnection(connectionString);
           con.Open();
           SqlCommand cmd = new SqlCommand(str,con);
           cmd.ExecuteNonQuery();
           GridView1.EditIndex = -1;
           BindData();
        }
   }
But when i debug it i am getting an error saying that
System.Web.UI.WebControls.GridViewUpdatedEventArgs' doesnot contain a definition for 'RowIndex'
Can anyone plz let me know the problem...???

Bye,
Thanks in advance

Answers (1)