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(); 
 
    }
}

Recommended Answers

All 3 Replies

There's an ASP.NET Forum, for ASP .NET questions. You have more chances to get the answer, when posting on a proper forum.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.