Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView RowUpdating not getting new data

When trying to update GridView data, it successfully runs, but it gets the old data instead of the data you type in the textboxes.

This is what I have:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        string phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
        string email = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
        int contactId = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
        objLogic.UpdateContact(name, phone, email, contactId);  //passes values to SQL to update database
        GridView1.EditIndex = -1;
        GridView1.DataBind();
    }

and

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
    }

and

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = objLogic.LoadClient();
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "clientId";
            DropDownList1.DataBind();
        }

        GridView1.DataSource = objLogic.LoadContacts(Convert.ToInt16(DropDownList1.SelectedValue));
        GridView1.DataBind();

For example, the current data is:

name: Blake, phone: 123-234-3456, email: [email protected], contactId: 22

I type in new data:

name: John, phone: 555-555-5555, email: [email protected], contactId: 22

Data that ends up in the database:

name: Blake, phone: 123-234-3456, email: [email protected], contactId: 22

like image 810
Prokzy Avatar asked Aug 04 '14 14:08

Prokzy


3 Answers

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList1.DataSource = objLogic.LoadClient();
        DropDownList1.DataTextField = "name";
        DropDownList1.DataValueField = "clientId";
        DropDownList1.DataBind();
        BindGrid();
    }
 }

protected void BindGrid()
{
    GridView1.DataSource = objLogic.LoadContacts(Convert.ToInt16(DropDownList1.SelectedValue));
    GridView1.DataBind();
}

Every time the page posts back you are rebinding the grid. Move the binding of the grid into a separate function. After you do the row updating, rebind the grid.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
    string phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
    string email = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
    int contactId = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
    objLogic.UpdateContact(name, phone, email, contactId);  //passes values to SQL to update database
    GridView1.EditIndex = -1;
    BindGrid();
}

When you edit you must call BindGrid too.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    //GridView1.DataBind(); this is meaningless, you have not set a DataSource
    BindGrid();
}
like image 75
Martin Smellworse Avatar answered Nov 14 '22 22:11

Martin Smellworse


When you raise the RowUpdating event your basically getting the values BEFORE the GridView updates the row. This is basically so you can cancel the update operation. To get what you've type in I think you need to get the new values (the Dictionary e.NewValues() in your case).

In your case you can use it something like this (assuming name, phone and email are what they're called in your gridview):

foreach(DictionaryEntry de in e.NewValues())
{
    string name = de.FirstOrDefault(x => x.Key == "name").Value;
    string phone = de.FirstOrDefault(x => x.Key == "phone").Value;
    string email = de.FirstOrDefault(x => x.Key == "email").Value;
}

NOTE: If you're simply using this to get the values typed in to update your database then you're better of binding the datagrid and use the value after that to ensure you definitely have the same values in the datagrid as to what you put into the database. Alternatively, use the RowUpdated event instead of the RowUpdating event.

like image 43
sr28 Avatar answered Nov 14 '22 23:11

sr28


The most prevalent reason is that we usually forgot to check the postbacks in the Page Load event. check the post-back, then the problem will be removed.

if (! IsPostBack)
{
    readData();
    ...
}
like image 39
Emad Armoun Avatar answered Nov 14 '22 23:11

Emad Armoun