0

i want to update the row in gridview but it give me one error
Object reference not set to an instance of an object.

how to update the current row selected in grid view.......please help!

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        
        string pname=txtprodname.Text;
        string pdesc=txtdesc.Text;
        SqlConnection conn = new SqlConnection("Data Source=ABC-59236A7CB6F;Initial Catalog=practical2;Integrated Security=True");
        conn.Open();
         SqlCommand  cmd= new SqlCommand("Insert into product(prodname,proddesc)values ('" + pname + " ' , '" + pdesc + " ')",conn );
        cmd.ExecuteNonQuery();
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        da = new SqlDataAdapter("select * from product", conn);
        da.Fill(ds, "product");
        dt = ds.Tables["product"];
        GridView1.DataSource = dt;
        GridView1.DataBind(); 
 
        
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
       
    }
    static int num;
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source=ABC-59236A7CB6F;Initial Catalog=practical2;Integrated Security=True");
        conn.Open();
       
       
        GridView1.EditIndex = e.NewEditIndex;
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        da = new SqlDataAdapter("select * from product", conn);
        da.Fill(ds, "product");
        dt = ds.Tables["product"];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox t1, t2, t3;
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        t1 = (TextBox)row.FindControl("txtprodid");
        t2 = (TextBox)row.FindControl("txtprodname");
        t3 = (TextBox)row.FindControl("txtproddesc");
        GridView1.EditIndex = -1;
        SqlConnection conn = new SqlConnection("Data Source=ABC-59236A7CB6F;Initial Catalog=practical2;Integrated Security=True");
        conn.Open();
        SqlCommand cmd = new SqlCommand("Update product set prodname= '" + t2.Text + "' , proddesc= '" + t2.Text + "' where prodid= " +t1.Text+ "", conn);
        cmd.ExecuteNonQuery();
        
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        da = new SqlDataAdapter("select * from product", conn);
        da.Fill(ds, "product");
        dt = ds.Tables["product"];
        GridView1.DataSource = dt;
        GridView1.DataBind(); 
 
    }
}
4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by dnanetwork
0

Please post the .aspx markup here. Try to run web-app in debug mode and point out at which method and line of source code, where you got an exception.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.