8
Answers

row update button coding in grid view for asp.net c#

Nitin Sharma

Nitin Sharma

10y
3.5k
1
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
<asp:GridView ID="GridView1" runat="server" Height="189px" Width="332px"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
</asp:GridView>
 
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  
 

Answers (8)
Next Recommended Forum