update within grid view....

Posted by Ankitsrist under ASP.NET on 12/21/2012 | Points: 10 | Views : 962 | Status : [Member] | Replies : 9
hello please check the following code it is not working gives an error "Object reference not set to an instance of an object." wats wrong in this please clarify.....
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
dbc.myconnection();
GridView1.EditIndex = e.NewEditIndex;
fillgrid();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
dbc.myconnection();
int id =Convert .ToInt32 ( GridView1.DataKeys[e.RowIndex].Value.ToString());
string name = GridView1.DataKeys[e.RowIndex].Values["name"].ToString();
TextBox TextBox2=(TextBox)GridView1.Rows[e.RowIndex ].FindControl("TextBox2");
SqlCommand cmd = new SqlCommand("update dmlwithingrid set decription='"+TextBox2.Text +"' where id="+id, dbc .sqlcon );
cmd.ExecuteNonQuery();
GridView1 .EditIndex = -1;
}





Responses

Posted by: Ankitsrist on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
i have taken id, name as datakeynammes......

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
are koi to help karo......

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/21/2012 [Member] Bronze | Points: 25

Up
0
Down
by keep an break point check all the values you are assingning are fine.

by seeing the code no and having no idea of the data being assigned no one can crack it...

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
sir it is giving an error at cmd line....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
Use this
int id =Convert .ToInt32 ( GridView1.DataKeys[e.RowIndex].Value.ToString());

string name = GridView1.DataKeys[e.RowIndex].Values["name"].ToString();
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
TextBox TextBox2= row.FindControl("TextBox2") as TextBox;
SqlCommand cmd = new SqlCommand("update dmlwithingrid set decription='"+TextBox2.Text +"' where id="+id, dbc .sqlcon );
cmd.ExecuteNonQuery();
GridView1 .EditIndex = -1;


If you have taken DataKeyNames="Id ,Name ", lihe this then Change
 int id =GridView1.DataKeys[e.RowIndex].Values[0];

string name = GridView1.DataKeys[e.RowIndex].Values[1].ToString(); 


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
sandhyab still it is giving same error "Object reference not set to an instance of an object." :)

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
What U mention DatakeyNames in gridView?? Can U post Ur griview Control Code.

Thanks & Regards

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
its DataKeyNames ="id,name"

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 12/21/2012 [Member] Starter | Points: 25

Up
0
Down
Once try this,
protected void UpdateRow(object sendedr, GridViewUpdateEventArgs e)

{
var Id= GridView1.DataKeys[e.RowIndex].Values[0];
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
TextBox txt= row.FindControl("TextBox2")as TextBox;
string sql = "Update dmlwithingrid set decription= "@decription" +
" where id= @id;
using (SqlCommand cmd = new SqlCommand(sql, dbc .sqlcon))
{
cmd.Parameters.AddWithValue("@decription", txt= .Text.Trim());
cmd.Parameters.AddWithValue("@id", Id);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
GridView1.EditIndex = -1;
}


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response