deleted item still shows up in datagrid after it's been deleted

Bear with me, I'm new to C# and troubleshooting someone else's code...

I have a simple datagrid. It stores links to photos, so pretty much all you can do is delete a line item (e.g. delete one of the photos) or use the upload feature at the bottom to add a new photo.

When you add a new photo, you hit the upload button and the datagrid is updated right away. When I hit delete for a line item, it still shows up. If I hit delete a second time, it disappears; also, if I go back to the previous page and return to this page, it no longer shows up. The database also reflects the deletion. So everything is correct EXCEPT the datagrid.

My guess is that maybe it's rebinding before the deletion is complete??? Is that possible? Is there a fix?

Here's the datagrid code:
        <asp:Panel ID="pnlPhotos" runat="server" Visible="false">
        <asp:Label Rows="5" Text='House Photos' runat="server" ID="houseAddress" />
        <asp:GridView ID="grdPhotos" runat="server" AutoGenerateColumns="false">
                <asp:BoundField DataField="ImagePath" HeaderText="Image Path" />
                <asp:TemplateField HeaderText="Sort Order">
                        <div align="center">
                     <Faster:DropDown ID="lstSort" DataSource='<%# getSortCategories() %>'
                                                   OnSelectedIndexChanged="sort_change" AutoPostBack="true" runat="server"></Faster:DropDown>
                <asp:CommandField ShowCancelButton="false" ShowDeleteButton="true" />
        <AlternatingRowStyle BackColor="#ECECEC" />
        <HeaderStyle BackColor="LightYellow" />
        <br />
         Upload photos:
        <asp:FileUpload ID="fileUpload" runat="server" />
        <asp:Button ID="btnUpload" OnClick="upload" runat="server" Text="Upload" />
        <br />
        <a href="Houses.aspx">Back</a>

And here's the code from the back end:

    void grdPhotos_RowDeleting(object sender, GridViewDeleteEventArgs e)
        GridView lnk = (GridView)sender;
        OleDbConnection conn = new OleDbConnection(ConfigurationManager.AppSettings["connString"]);
        OleDbCommand sortCommand = new OleDbCommand();
        sortCommand.CommandText = "DELETE FROM tblPhotos WHERE ImagePath='" + lnk.Rows[e.RowIndex].Cells[0].Text + "'";
        sortCommand.Connection = conn;

        da2.Update(ds, "tblPhotos");
        photoDv = ds.Tables["tblPhotos"].DefaultView;
        photoDv.RowFilter = "HouseID=" + currentDbId.ToString();
        Session["photoView"] = photoDv;
        grdPhotos.DataSource = photoDv;
        grdPhotos.DataMember = "tblPhotos";

        foreach (GridViewRow row in grdPhotos.Rows)
                DropDown lst = ((DropDown)row.FindControl("lstSort"));
                lst.SelectedValue = Convert.ToInt16(photoDv[row.RowIndex]["SortOrder1"]).ToString();
            catch { }

    void grdPhotos_RowDeleted(object sender, GridViewDeletedEventArgs e)

Bonus question: is there an easy way to add a thumbnail of the photo itself to the datagrid?

Thanks so much! I've been working on this for hours and I just can't figure out how to fix it.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
Check and see if postback is enabled for the delete. Also check to see which method is being called during the delete postback. It appears that the event to delete exists as you have shown it to us above.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cioservicesAuthor Commented:
Dumb do I find out if postback is enabled? I think it's something from page load so here's that code. The one thing I notice that maybe is part of the problem: on that postback section, it's rebinding the house list but not the photo list...

    protected void Page_Load(object sender, EventArgs e)
        gridHouses.RowEditing += new GridViewEditEventHandler(gridHouses_RowEditing);
        dlHouseEdit.CancelCommand += new DataListCommandEventHandler(dlHouseEdit_CancelCommand);
        dlHouseEdit.UpdateCommand += new DataListCommandEventHandler(dlHouseEdit_UpdateCommand);
        gridHouses.RowCommand += new GridViewCommandEventHandler(gridHouses_RowCommand);
        grdPhotos.RowDeleting += new GridViewDeleteEventHandler(grdPhotos_RowDeleting);
        grdPhotos.RowCommand += new GridViewCommandEventHandler(grdPhotos_RowCommand);
        grdPhotos.RowDeleted += new GridViewDeletedEventHandler(grdPhotos_RowDeleted);

        //connection dbConnnect = new connection();
        OleDbConnection conn = new OleDbConnection(ConfigurationManager.AppSettings["connString"]);
        try { conn.Open(); }
        catch { }
        string strSQL1 = "SELECT * FROM tblHouses ORDER BY ID DESC";
        string strSQL2 = "SELECT * FROM tblPhotos";
       // string strSQL2 = "SELECT * FROM tblUserTypes";
        da = new OleDbDataAdapter(new OleDbCommand(strSQL1, conn));
        da2 = new OleDbDataAdapter(new OleDbCommand(strSQL2, conn));
        ds = new DataSet();
        OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(da);
        OleDbCommandBuilder cmdBuilder2 = new OleDbCommandBuilder(da2);
        da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        da2.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        da.Fill(ds, "tblHouses");
        da2.Fill(ds, "tblPhotos");
        da.UpdateCommand = cmdBuilder.GetUpdateCommand();
        da.InsertCommand = cmdBuilder.GetInsertCommand();
        da2.UpdateCommand = cmdBuilder2.GetUpdateCommand();
        da2.InsertCommand = cmdBuilder2.GetInsertCommand();
        gridHouses.DataSource = ds;
        if (!IsPostBack)
            Session.Timeout = 30;
            Session["CurrentItem"] = -1;
            Session["CurrentDbId"] = 0;
                currentId = Int32.Parse(Session["CurrentItem"].ToString());
                currentDbId = Int32.Parse(Session["CurrentDbId"].ToString());
            catch (Exception timeoutEx)
                //Timeout has occurred
                Response.Write(@"<script language=javascript>" +
                                @"alert('Your session has timed out, please try again');" +
                                @"window.location='/admin/Houses.aspx'; </script>");
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
Select the control from the designer view and view the properties.
Click the little lightning bolt to see the events and what methods are hooked to each event.

It does appear that rebinding isn't happening for you, so you may be on to something there.

After Deleteing row from your grid

you need to featch record from DB and Bind it to your datagrid

menas you require fresh status from DB

Thanks & Regards
cioservicesAuthor Commented:
Isn't that what the da2.update command does?

Anyway, I came up with a hack--it's cludgy but it works--I added a filter to exclude the item just deleted from the grid.

xDJR1875, you helped me get there with your feedback, so I'm going to award you the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.