i have use this part of code for update a row in gridview in c# asp.net
in my past project it was worked but now it is not working can anybody help me in this i have pase my sql query, asp gridview code and my logical code too
please advise me how to update a row in gridview
SQL Query
create database test
use test
create table gridview(id int identity(1,1),ename varchar(50),city varchar(50))
insert into gridview values('nitin','hisar')
insert into gridview values('deepak','gwalior')
insert into gridview values('raman','delhi')
select*from gridview
update gridview set ename='nitin sharma', city='haryana' where id=1
default.aspx
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
default.aspx.cs
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
string name=GridView1.Rows[e.RowIndex].Cells[0].Text;
string city=GridView1.Rows[e.RowIndex].Cells[1].Text;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("update gridview set ename='"+name+"', city='"+city+"' where id='"+id+"'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
display();
}
please help me 8 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nitin SharmaPosted Mar 27, 2014, 5:59 AM
"Object reference not set to an instance of an object."
but my used code work but output is wrong can you please modify my code all details i have posted , its very urgent for me
any body please help me
Munesh SharmaPosted Mar 27, 2014, 5:07 AM
Nitin SharmaPosted Mar 27, 2014, 3:29 AM
Nitin SharmaPosted Mar 26, 2014, 3:14 AM
"Object reference not set to an instance of an object."
Munesh SharmaPosted Mar 26, 2014, 2:56 AM
Nitin SharmaPosted Mar 26, 2014, 2:24 AM
Dear Abhey
given link coding was not working
Abhay ShankerPosted Mar 26, 2014, 2:18 AM
http://devilswork.wordpress.com/2009/03/27/gridview-row-edit-delete-and-update/
Lakshmanan Sethu SankaranarayanPosted Mar 26, 2014, 2:12 AM