Rimo72
Rimo72

Reputation: 91

gridview deletion issue. Can't delete a SQL record from the gridview delete button

As much as I try to understand the delete methods of the gridview, I still can't figure how to delete a row. I'm using the same gridview to display 4 SQL tables. Depending on which table is selected, I go in a swich case to change the SELECT and DELETE command of the SQLDataSource. I'm not sure if this is the best practice but the SELECT portion works.

It is the deletion part that I need some help.

  1. Is my thought process right on how to achieve my goal?
  2. I have to have OnRowDeleting="gv1_RowDeleting" or else it gave me an error. Is this mandatory.
  3. What is it with my code that doesn't work?

.ASPX :

<form id="form1" runat="server">
            <div class="listTitle"><% Response.Write(strListTitle); %>
                <asp:GridView ID="gv1" 
                    runat="server" 
                    DataKeyNames="id" 
                    CellPadding="2" 
                    AutoGenerateDeleteButton="True"
                    OnRowDeleting="gv1_RowDeleting"
                    OnRowDeleted="gv1_deleteItem">
                </asp:GridView>
            </div>
        </form>
    </div>  <!-- close middle -->
    <div id="footer">

        <asp:SqlDataSource ID="DS" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>">
            <DeleteParameters> <asp:Parameter Name="id"/> </DeleteParameters>
        </asp:SqlDataSource>

    </div>

.CS file:

   switch (strType)
    {
        case "servers":
            DS.SelectCommand = "SELECT * FROM tbServers";
            DS.DeleteCommand = "DELETE FROM tbServers WHERE id = @id";
            strListTitle = "Server List";
            break;
        case "systems":
            DS.SelectCommand = "SELECT * FROM tbSystems";
            DS.DeleteCommand = "DELETE FROM tbSystems WHERE id = @id";
            strListTitle = "System List";
            break;
        case "itOwners":
            DS.SelectCommand = "SELECT * FROM tbITowners";
            DS.DeleteCommand = "DELETE FROM tbITowners WHERE id = @id";
            strListTitle = "IT Owner List";
            break;
        case "systemOwners":
            DS.SelectCommand = "SELECT tbSystemOwners.id, tbITowners.owner, tbSystems.Systems FROM tbITowners INNER JOIN tbSystemOwners ON tbITowners.id = tbSystemOwners.idITowners INNER JOIN tbSystems ON tbSystemOwners.idSytems = tbSystems.id";
            DS.DeleteCommand = "DELETE FROM tbSystemOwners WHERE id = @id";
            strListTitle = "System Owner List";
            break;
        default:
            break;
    }
    gv1.DataSource = DS;
    gv1.DataBind();
}

protected void gv1_deleteItem(object sender, GridViewDeletedEventArgs e)
{
    // Display whether the delete operation succeeded.
    if (e.Exception == null)
    {
       Response.Write( "Row deleted successfully.");
    }
    else
    {
       Response.Write("An error occurred while attempting to delete the row.");
       e.ExceptionHandled = true;
    }
}

 protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}

Upvotes: 0

Views: 417

Answers (1)

PRATANTIA
PRATANTIA

Reputation: 76

re: #3 I think the issue is that you are using the Parameter class as a delete parameter. The parameter class is a base class that doesn't contain the information you want - it's not going to pick up the ID from the row the delete button was clicked in, that's not going to get passed to the delete command, and I'd assume you get an SQL exception. (Or maybe you get a different error, because it's trying to pass along both id parameters, and can't find a function that accepts two. Haven't investigated)

If you remove the delete parameters, the fact that you have id set as a DataKeyName means it should get passed along to your delete command automatically.

Above information is incorrect. Using a delete parameter in addition to a DataKeyName with autogenerated delete buttons doesn't seem to cause any issue.

The actual problem was using DataSource to associate the GridView with the SQLDataSource, instead of DataSourceID. DataSource doesn't persist on postback, whereas DataSourceID does. I'm not entirely sure what's going on under the hood, but the DataSource getting thrown out during the page life cycle screws up the Delete method call, so it never happens.

Long story short, use the DataSourceID property when using a control as your data source, and the DataSource property when using an object (e.g: a DataTable) as your data ource.

Upvotes: 1

Related Questions