?
Solved

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

Posted on 2007-03-24
5
Medium Priority
?
234 Views
Last Modified: 2010-04-16
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
Comment
Question by:cioservices
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 2000 total points
ID: 18785499
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
 

Author Comment

by:cioservices
ID: 18785552
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
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 18786072
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
 
LVL 10

Expert Comment

by:SystemExpert
ID: 18805711
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
 

Author Comment

by:cioservices
ID: 18810290
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question