Solved

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

Posted on 2007-03-24
5
226 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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