locked
LINQ GridView Row_Updating SubmitChanges() not updating database RRS feed

  • Question

  • Wow, this is just killing me.  In debug it appears that all of the values are correct, however the database doesn't get updated.  Any help would probably add years back to my life.  :)

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
        {        
            Guid key = (Guid)(GridView1.DataKeys[e.RowIndex].Value);
    
    
    
           
            
            
            TextBox CompanyAddress = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]);
            TextBox CompanyCity = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]);
            TextBox CompanyName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]);
          
                ermContextDataContext ctx = new ermContextDataContext();
                
    
            CompanyProfile prof = ctx.CompanyProfiles.Single(p => p.CompanyProfileID == key);
            
           
            prof.CompanyAddress = CompanyAddress.Text;
            prof.CompanyCity = CompanyCity.Text;
            prof.CompanyName = CompanyName.Text;
    
            
            ctx.SubmitChanges();
                
    
    
    
            GridView1.EditIndex = -1;
    
    
    // fetch and rebind the data.
            GridView1.DataBind();
        }
    Monday, May 18, 2009 6:12 PM

Answers

  •  

    Hi Mike,

     

    From your another thread about a similar question as the issue in this thread, I finally find the root cause.  That thread is http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/bc9ceabf-af81-4da5-9240-5181678b7b8b.    

     

    In that thread, I noticed that in Page_Load method, there is a LINQ to SQL query and it is also the data source of the GridView.  From the MSDN documentation, Page_Load method will be fired when the page is loaded or accessed for the first time or the page is in response to a client postback. Each time we click the Update button to fire the GridView_RowUpdating method, the page will receive a client postback request, so actually the Page_Load method will be fired again before GridView_RowUpdating is fired, which means the GridView is bound to the original data source again before we try to update the database. 

     

    Based on these information, I debug into the GridView_RowUpdating method and find that the corresponding TextBox.Text value is the original value instead of what we just input.  Therefore, the DataContext.SubmitChanges method won’t update the database to the new values.  

     

    To resolve this problem, we can first detect whether the page is loaded for the first time in the Page_Load method.  For detail, please see the following code snippet:

    ===============================================

       protected void Page_Load(object sender, EventArgs e)

    {

           // LINQ to SQL query to get the data source

    ·                    GridView1.DataSource = query;

    ·                    

              if (!IsPostBack)

              {                         

                  GridView1.DataBind();

              }

        }

    ===============================================

     

    If you want to show the newly updated data on the page after the GridView_RowUpdating, you can retrieve the data again after calling the DataContext.SubmitChanges.  For detail, please see:

    ===============================================

    ·            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    ·             {

    ·                 // Update the data

    ·                 dc.SubmitChanges();

    ·          

    ·                 GridView1.EditIndex = -1;

    ·          

            // Retrieve the data from the database again and bind the

           //  query result to the GridView1 here

     

    ·                 GridView1.DataBind();

    ·             }

    ===============================================

     

     

    Have a nice day, Mike!

     

     

    Best Regards,
    Lingzhi

     

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    Tuesday, May 19, 2009 10:26 AM
    Moderator

All replies

  •  

    Hi Mike,

     

    From your another thread about a similar question as the issue in this thread, I finally find the root cause.  That thread is http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/bc9ceabf-af81-4da5-9240-5181678b7b8b.    

     

    In that thread, I noticed that in Page_Load method, there is a LINQ to SQL query and it is also the data source of the GridView.  From the MSDN documentation, Page_Load method will be fired when the page is loaded or accessed for the first time or the page is in response to a client postback. Each time we click the Update button to fire the GridView_RowUpdating method, the page will receive a client postback request, so actually the Page_Load method will be fired again before GridView_RowUpdating is fired, which means the GridView is bound to the original data source again before we try to update the database. 

     

    Based on these information, I debug into the GridView_RowUpdating method and find that the corresponding TextBox.Text value is the original value instead of what we just input.  Therefore, the DataContext.SubmitChanges method won’t update the database to the new values.  

     

    To resolve this problem, we can first detect whether the page is loaded for the first time in the Page_Load method.  For detail, please see the following code snippet:

    ===============================================

       protected void Page_Load(object sender, EventArgs e)

    {

           // LINQ to SQL query to get the data source

    ·                    GridView1.DataSource = query;

    ·                    

              if (!IsPostBack)

              {                         

                  GridView1.DataBind();

              }

        }

    ===============================================

     

    If you want to show the newly updated data on the page after the GridView_RowUpdating, you can retrieve the data again after calling the DataContext.SubmitChanges.  For detail, please see:

    ===============================================

    ·            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    ·             {

    ·                 // Update the data

    ·                 dc.SubmitChanges();

    ·          

    ·                 GridView1.EditIndex = -1;

    ·          

            // Retrieve the data from the database again and bind the

           //  query result to the GridView1 here

     

    ·                 GridView1.DataBind();

    ·             }

    ===============================================

     

     

    Have a nice day, Mike!

     

     

    Best Regards,
    Lingzhi

     

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    Tuesday, May 19, 2009 10:26 AM
    Moderator
  • Hi Mike,


    Would you mind letting me know the results of my suggestions?

     

    If you have any further questions, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    Tuesday, May 26, 2009 9:17 AM
    Moderator
  • Thank you so much.  I didn't see the answer the first time.  I appreciate your help.  Also, thanks for the link to IsPostBack documentation.  I think that will help me resolve many issues.
    Wednesday, May 27, 2009 5:15 PM
  • Hi Mike,

     

    You are welcome!  

     

    By the way, for ASP.NET problems, you can consider posting in ASP.NET forums.  I think you will receive more assistance from the ASP.NET experts living there.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    Thursday, May 28, 2009 2:18 AM
    Moderator