Link button Delete from ListView and DB in C#

780 views Asked by At

Okay so I have ListView and I am having some issues removing an item from the listview and the database. I am trying to accomplish this using OnItemDeleting. When I run it, it returns as having been successful; however, it was not so I am guessing that I am still not retrieving my selected imageId from DataKeyNames(for some reason). Any suggestions?

Here is my List View:

    <asp:ListView ID="ListView2" runat="server" GroupItemCount="3" DataKeyNames="ImageId" OnItemDeleting="ListView2_ItemDeleting">

Here is the link button:

   <asp:LinkButton runat="server" Id="lvBtn" CommandName="Delete" Text="Remove" OnClientClick="return confirm('Remove this image?')"/>

Here is my codebehind C#:

    protected void ListView2_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        string programId = Request.QueryString["ProgramId"];

        ListView2.SelectedIndex = Convert.ToInt32(e.ItemIndex);
        string imageId = ListView2.DataKeys[e.ItemIndex].Value.ToString();

        // Execute the delete command
        bool success = CatalogAccess.DeleteProgramImageRelation(imageId, programId);

        ListView2.EditIndex = -1;

        // Display status message
        statusLabel2.Text = success ? "Image removed successfully" : "Failed to remove image";

        // Reload the grid
        DataBind();
    }

Catalog Access:

    // Delete program image relationship
public static bool DeleteProgramImageRelation(string ProgramId, string ImageId)
{
    // get a configured DbCommand object
    DbCommand comm = GenericDataAccess.CreateCommand();
    // set the stored procedure name
    comm.CommandText = "DeleteProgramImageRelation";
    // create a new parameter
    DbParameter param = comm.CreateParameter();
    param.ParameterName = "@ProgramId";
    param.Value = ProgramId;
    param.DbType = DbType.Int32;
    comm.Parameters.Add(param);
    // create a new parameter
    param = comm.CreateParameter();
    param.ParameterName = "@ImageId";
    param.Value = ImageId;
    param.DbType = DbType.Int32;
    comm.Parameters.Add(param);
    // execute the stored procedure;
    int result = -1;
    try
    {
        result = GenericDataAccess.ExecuteNonQuery(comm);
    }
    catch
    {
        // any errors are logged in DataAccess, we ignore them here
    }
    // result will be 1 in case of success
    return (result != -1);
}
2

There are 2 answers

0
Faron On BEST ANSWER

Problem solved! turns out my ProgramId was the id not being captured so I made small tweak like so:

    private string currentProgramId;

    protected void Page_Load(object sender, EventArgs e)
    {
        currentProgramId = Request.QueryString["ProgramId"];

I then just updated the rest of my references accordingly, and it works perfect now.

3
Rahul On

I am not sure what you are exactly doing but I believe you are getting some SQLException. Try debugging by putting a breakpoint in result = GenericDataAccess.ExecuteNonQuery(comm); and verify it.

From your posted code for method CatalogAccess.DeleteProgramImageRelation(imageId, programId) looks like your procedure expects a INT type parameter whereas you are passing String type parameter as can be seen below

DbParameter param = comm.CreateParameter();
param.ParameterName = "@ProgramId";
param.Value = ProgramId; -- ProgramId is String
param.DbType = DbType.Int32; -- You are specifying Int32