Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML:-
XML
<asp:GridView runat="server" ID="gv2" AutoGenerateColumns="false" AutoGenerateEditButton="true"
            AutoGenerateDeleteButton="true" OnRowEditing="edit" OnRowUpdating="update" OnRowDeleting="delete"
            OnRowCancelingEdit="cancel">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblId" Text='<%#Eval("s_id") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:Label runat=server ID="lblId1" Text='<%#Eval("s_id") %>'></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblName" Text='<%#Eval("s_name") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtName" Text='<%#Eval("s_name") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblPhone" Text='<%#Eval("s_phone") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtPhone" Text='<%#Eval("s_phone") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Degree">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblDegree" Text='<%#Eval("s_degree") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtDegree" Text='<%#Eval("s_degree") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


Code(.cs):-
public void update(object sender, GridViewUpdateEventArgs e)
        {
            con.Open();
            Label lbl = gv2.Rows[e.RowIndex].FindControl("lblId1") as Label;
            TextBox txtName = gv2.Rows[e.RowIndex].FindControl("txtName") as TextBox;
            TextBox txtPhone = gv2.Rows[e.RowIndex].FindControl("txtPhone") as TextBox;
            TextBox txtDegree = gv2.Rows[e.RowIndex].FindControl("txtDegree") as TextBox;
            SqlCommand cmd2 = new SqlCommand();
            cmd2.Connection = con;
            cmd2.CommandText = "UpdateInfo";
            cmd2.CommandType = CommandType.StoredProcedure;
            cmd2.Parameters.Add("@s_name", SqlDbType.VarChar).Value = txtName.Text.ToString();
            cmd2.Parameters.Add("@s_phone", SqlDbType.VarChar).Value = txtPhone.Text.ToString();
            cmd2.Parameters.Add("@s_degree", SqlDbType.VarChar).Value = txtDegree.Text.ToString();
            cmd2.Parameters.Add("@s_id", SqlDbType.Int).Value = Convert.ToInt32(lbl.Text);
            cmd2.ExecuteNonQuery();
            lblTest.Text = txtName.Text;//was checking value but previous value printing here
            cmd2.Dispose();
            gv2.EditIndex = -1;
            con.Close();
            BindData();
        }

Procedure:-
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[UpdateInfo]
(
	@s_name varchar(300),
	@s_phone varchar(50),
	@s_degree varchar(300),
	@s_id int
)
as
begin
update info set s_name=@s_name, s_phone=@s_phone, s_degree=@s_degree where s_id=@s_id
end


Quote:
Guys, the problem here is that I need to delete the old row and insert a new row in Gridview in EDIT mode. So help me with some code which would Insert/Update new values given in Edit mode and also clearing previous stored/bind value.
When I am updating, the Gridview holds the bind value in it thus not letting Update operation to cause effects.
Posted
Updated 3-May-17 15:34pm
v3
Comments
sriman.ch 9-Dec-11 0:44am    
is it throwing any error ? check whether you are getting values to be updated ...
Karthik Harve 9-Dec-11 0:45am    
Show the function "BindData();"...!!!!
07navneet 9-Dec-11 1:15am    
public void BindData()
{
con.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
cmd1.CommandText = "GetInfo";
cmd1.CommandType = CommandType.StoredProcedure;
//SqlDataReader rdr = cmd1.ExecuteReader();
//gv2.DataSource = rdr;
SqlDataAdapter da = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
da.Fill(ds);
gv2.DataSource = ds;
gv2.DataBind();
cmd1.Dispose();
con.Close();
}
07navneet 9-Dec-11 1:15am    
no error thrown!

At Page Load I was simply calling BindData function but the technique was to call it when its not Postback!

HTML
if(!Page.IsPostBack)
                BindData();
 
Share this answer
 
v2
Hi,

If your data updated successfully on database table then you call the function which is bind the data on gridview, after the update function.
 
Share this answer
 
Comments
07navneet 9-Dec-11 1:19am    
no, its not updating! After entering new(edit) value I am fetching the text of 'txtname.text' into dummy textbox but new value is not been fetched into dummy!
I dnt think your table is updating with this query so your data is not updating in the grid.Try this :
SQL
update info set s_name='''+@s_name+''', s_phone='''+@s_phone+''', s_degree='''+@s_degree+''' where s_id=@s_id


Hope it'll help you.
Don't forget to mark as answer if it helps. :)
 
Share this answer
 
Comments
07navneet 9-Dec-11 1:25am    
Mine is a stored procedure(MS-SQL) and you are giving me syntax for writing code in .cs file!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900