none
edit gridView with intity frameWork RRS feed

  • Question

  • Hello to everyone

    I'm using EntityFrameWork to edit a GridView . my problem is it that when I want to edit a row (input value of username & phone & email in the gridView and click edit) , nothing happens? !    Is my code have problem ?




     protected void gv_PanelUsers_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv_PanelUsers.EditIndex = e.NewEditIndex;
            gv_PanelUsers.DataBind();
                    
        }
    
    protected void gv_PanelUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            MShopEntities db = new MShopEntities();
    
            int id = (int)gv_PanelUsers.DataKeys[e.RowIndex].Value;
           
    
            TextBox txt_username = (TextBox)gv_PanelUsers.Rows[e.RowIndex].Cells[1].Controls[0];
            TextBox txt_phone = (TextBox)gv_PanelUsers.Rows[e.RowIndex].Cells[2].Controls[0];
            TextBox txt_email = (TextBox)gv_PanelUsers.Rows[e.RowIndex].Cells[3].Controls[0];
            PanelUsers query = db.PanelUsers.FirstOrDefault(i => i.PanelUserID == id);
            if (query != null)
            {
                
                query.UserName = txt_username.Text;
                query.Phone = txt_phone.Text;
                query.Email = txt_email.Text;
                db.SaveChanges();
                gv_PanelUsers.DataBind();
    
                gv_PanelUsers.EditIndex = -1;
                gv_PanelUsers.DataBind();
                fill_gv_users();
            }
    
            else
            {
                show_message("error", "Edit faild!");
            }
    
           
           


    Monday, December 1, 2014 9:00 PM

Answers

  • Hello martin,

    >>, nothing happens? !    Is my code have problem ?

    I do not quite understand the meaning of “nothing happens”, if it is that the database is not changed or the GirdView is not changed

    If it is former, I am confused since you said nothing happens, so it should does not throw any error which means it calls SaveChanges() method successfully.

    For solveing/narrowing down this issue, have you added breakpoint to rowupdating method to see if these input values of username & phone & email in the gridView are different from values in database? if they are actually same, nothing would happen.

    If they are different, there is one possible is that these changes are made into the copied database file in bin folder while you are watching the original one. For ensuring if these values are modified, to define a variable as below when calling the SaveChanges():

    var affectrows = db.SaveChanges();

    It will return effected rows in database which means if your code updates the row successfully, it should return 1.

    If you mean the gridview does not change, i suggest you could ask it to the web control forum:

    http://forums.asp.net/

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 2, 2014 7:50 AM
    Moderator