Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi all
source of grid view is
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat=server; AutoGenerateDeleteButton="True"
               AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
               OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating">
                                                   <Columns>
<asp:TemplateField Visible="false">
                                                         <ItemTemplate>
                                                               <asp:Label ID="eid" Text='<%# Eval("eid") %>' runat="server"></asp:Label>
                                                           </ItemTemplate>
                                                       </asp:TemplateField>
                                                       <asp:TemplateField HeaderText="EXAM NAME">
                                                           <ItemTemplate>
                                                                <%#Eval("Examname") %>
                                                           </ItemTemplate>
                                                           <EditItemTemplate>
                                                               <asp:TextBox ID="textBox1" Text='<%#Eval("Examname") %>' runat="server"></asp:TextBox>
                                                           </EditItemTemplate>
                                                       </asp:TemplateField>
                                                       <asp:TemplateField HeaderText="MAX.MARKS">
                                                           <ItemTemplate>
                                                               <%#Eval("Maxmarks") %></ItemTemplate>
                                                           <EditItemTemplate>
                                                               <asp:TextBox ID="textBox2" Text='<%#Eval("Maxmarks") %>' runat="server"></asp:TextBox>
                                                           </EditItemTemplate>
                                                       </asp:TemplateField>
                                                   </Columns>
                                               </asp:GridView>

and my code-behind code is
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
       Label lbl = (Label)row.FindControl("eid");
       TextBox textname = (TextBox)row.FindControl("textBox1");
       TextBox textmax = (TextBox)row.FindControl("textBox2");
       GridView1.EditIndex = -1;
       try
       {
           MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
           con.Open();
           MySqlCommand cmd = new MySqlCommand("update examcreation set Examname='" + textname.Text + "' , Maxmarks='" + textmax.Text + "' where eid='" + lbl.Text + "'", con);
           cmd.ExecuteNonQuery();
           con.Close();
       }
       catch (Exception ex)
       {
           Response.Write("<script type='text/javascript' >window.alert('Please Start Wamp Server " + ex.Message + " ')</script>");
       }
   Binding();
   }

after debugging if i press edit button and give some values here i am not getting newly entered values i am getting previous values only
how can i get newly entered values can anyone help me please?
thanks in advance
Posted
Updated 22-Dec-10 20:11pm
v2
Comments
BhavinBhatt 12-Jan-12 2:24am    
I have all of the solutions... But the problem remains same...
Then I've used RowCommand event of GridView...and its solved!!!


Most likely reason is, you are always binding the data to the GridView without checking the IsPostBack property.

When you are clicking the "Update" button, the Page_Load() event gets invoked before the RowUpdating() event method is invoked. So, the GridView is getting bound again with the old value (Because you might not checked the IsPostBack flag) and hence, inside the RowUpdating() event method you are getting the old value.

If my assumption is correct, to solve this, change the Data binding method of the GridView as follows:

C#
if (!IsPostBack) //Bind GridView only when IsPostBack is false
{
           GridView1.DataSource = //Assign data here;
           GridView1.DataBind();
}
 
Share this answer
 
Comments
tulasiram3975 23-Dec-10 3:44am    
Good Call
thank you i get you thank you very much
AnirbanM 2 28-Aug-12 1:57am    
Though i'm not using any controls inside gridview, still i'm facing same problem, not getting updated value once i hit update button. after doing database update, binding again with databound function inside pageload if(!ispostback) function. still no use, even database's not updating anything.
just set Enableviewstate property to false of gridview you can get new value
 
Share this answer
 
Comments
.NET_development 6-Feb-14 9:29am    
thank you very much
Member 10426605 3-Sep-14 23:16pm    
Thank you very much!
seddig 5-Apr-15 19:14pm    
Thanks it is solution
Mandeep Negi 15-Feb-17 3:16am    
Thanks, it works for me
Sanjana31893 17-Aug-17 7:04am    
Thanks a ton! It saved my life
what is the uses of datakey property of grid view?
 
Share this answer
 
what is the uses of datakey property of grid view ?
can any body tell why this code is not giving me the updated value
C#
protected void grdvw1_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           //int sid = int.Parse(grdvw1.Rows[e.RowIndex].Cells[0].Text);
           //TextBox sname = (TextBox)grdvw1.Rows[e.RowIndex].Cells[1].Controls[0];
           //TextBox clas = (TextBox)grdvw1.Rows[e.RowIndex].Cells[3].Controls[0];
           //int sfee = int.Parse(grdvw1.Rows[e.RowIndex].Cells[4].Text);
           //r.Editinfo(sid,sname.Text,clas.Text,sfee);

           //TextBox sid = (TextBox)grdvw1.FooterRow.FindControl("txtbxeditstudentid");
           //TextBox sname = (TextBox)grdvw1.FooterRow.FindControl("txtbxeditname");
           //TextBox sclass = (TextBox)grdvw1.FooterRow.FindControl("txtbxeditclass");
           //TextBox sfee = (TextBox)grdvw1.FooterRow.FindControl("txtbxeditfee");

           //string empid = ((TextBox)grdvw1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

          //GridViewRow row=grdvw1.Rows[e.RowIndex];
          //string columnvalue = ((TextBox)row.Cells[0].Controls[0]).Text;
           int empsid = Convert.ToInt32(grdvw1.DataKeys[e.RowIndex].Values[0]);//Convert.ToInt32(sid);
           string empsname = Convert.ToString(grdvw1.DataKeys[e.RowIndex].Values[1]);
           string empclass = Convert.ToString(grdvw1.DataKeys[e.RowIndex].Values[2]);
           int empfee = Convert.ToInt32(grdvw1.DataKeys[e.RowIndex].Values[3]);


          ////DateTime dob= ((TextBox)grdvw1.Rows[e.RowIndex].Cells[1].FindControl("")).Text; //Position
          //string empclass = Convert.ToString(sclass);
          //int empfee = Convert.ToInt32(sfee);
           //r.Editinfo(empsid, empsname, empclass, empfee);

           grdvw1.EditIndex = -1;
              BindData();

              Response.Write("<script>alert ('Your Record Has Been updated Successfully') </script>");
 
Share this answer
 
It's nothing but the binding of gridview in page load event without Ispostback property. Every time when u r firing any event first that page load event will fire just bind ur gridview using
if(!IsPostBack)
{
//bind grid
}
that's solve
 
Share this answer
 
Comments
istiyakbca 26-Dec-10 11:34am    
just set Enableviewstate property to false of gridview you can get new value

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



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