• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

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">
        <Columns>
       
                <asp:BoundField DataField="ImagePath" HeaderText="Image Path" />
                <asp:TemplateField HeaderText="Sort Order">
                    <ItemTemplate>
                        <div align="center">
                     <Faster:DropDown ID="lstSort" DataSource='<%# getSortCategories() %>'
                                                   OnSelectedIndexChanged="sort_change" AutoPostBack="true" runat="server"></Faster:DropDown>
                        </div>
                    </ItemTemplate>
                   
                </asp:TemplateField>
               
                <asp:CommandField ShowCancelButton="false" ShowDeleteButton="true" />
               
            </Columns>
        <AlternatingRowStyle BackColor="#ECECEC" />
        <HeaderStyle BackColor="LightYellow" />
        </asp:GridView>
        <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>
        </asp:Panel>

-----------------------------------------------------------------
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"]);
        conn.Open();
        OleDbCommand sortCommand = new OleDbCommand();
        sortCommand.CommandText = "DELETE FROM tblPhotos WHERE ImagePath='" + lnk.Rows[e.RowIndex].Cells[0].Text + "'";
        sortCommand.Connection = conn;
        sortCommand.ExecuteNonQuery();
        conn.Close();

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

        foreach (GridViewRow row in grdPhotos.Rows)
        {
            try
            {
                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.

Wendy
0
cioservices
Asked:
cioservices
  • 2
  • 2
1 Solution
 
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.
0
 
cioservicesAuthor Commented:
Dumb question...how 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);

        //BUILD DATAADAPTERS, DATASETS, AND COMMANDS FOR GETTING INFORMATION
        //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");
       conn.Close();
        da.UpdateCommand = cmdBuilder.GetUpdateCommand();
        da.InsertCommand = cmdBuilder.GetInsertCommand();
       
        da2.UpdateCommand = cmdBuilder2.GetUpdateCommand();
        da2.InsertCommand = cmdBuilder2.GetInsertCommand();
       
        gridHouses.DataSource = ds;
       
        //IF THE PAGE IS POSTED, DON'T REBIND THE CONTROL
        if (!IsPostBack)
        {
            gridHouses.DataBind();
            Session.Timeout = 30;
            Session["CurrentItem"] = -1;
            Session["CurrentDbId"] = 0;
        }
        else
        {
            //SET A SESSION VARIABLE TO HOLD THE CURRENT ITEM
            try
            {
                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>");
            }
        }
    }
0
 
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.
0
 
SystemExpertCommented:
Hi,

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
0
 
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now