none
Delete Button within Repeater does not exist within current context RRS feed

  • Question

  • I am trying to get the delete button within my repeater control to function as intended. The aim is to get the button to "fire" the stored procedure within my MSSQL database.

    Code within my *.*aspx page:

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ 
    ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM 
    [Comments] WHERE ([Ad_ID] = @Ad_ID) ORDER BY [CommentCreationDateTime] ASC">
    And further down the *.*aspx page:
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2" 
    Visible="True" OnItemCommand="Repeater1_ItemCommand">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
    <table id="displayCommentsTable" class="displayCommentsTable">
    <tr class="displayCommentsTable"><td class="displayCommentsTable">
    <asp:ImageButton ID="deleteCommentImageButtonReal" runat="server" 
    class="rightCross" ImageUrl="images/Red-Cross-Mark-PNG.png" 
    OnClientClick="return confirm('Are you sure you wish to delete this 
    comment?');" Height="11" Width="11" CommandName="Delete" 
    CommandArgument='<%# Eval("Comment_ID") %>' /><%# Eval("CommenterName") %> 
    commented on <%# Eval("CommentCreationDateTime", "{0:d/M/yyyy <i> hh:mm:ss 
    tt}") %>
    </td></tr>
    <tr class="displayCommentsTable"><td class="displayCommentsTable"><%# 
    Eval("CommentText") %><br /></td></tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    And finally, my code behind where the magic should be happening but isn't:
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    if (e.CommandName == "Delete")
    {
    DeleteCommentById(Convert.ToInt32(e.CommandArgument))
    }
    }
    private void DeleteCommentById(int Comment_ID)
    {
    SqlConnection conn;
    SqlCommand deleteCommentById;
    
    string connectionString = 
    ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    
    conn = new SqlConnection(connectionString);
    
    deleteCommentById = new SqlCommand("usp_deleteCommentById", conn);
    
    deleteCommentById.CommandType = System.Data.CommandType.StoredProcedure;
    
    deleteCommentById.Parameters.Add("@Comment_ID", System.Data.SqlDbType.Int);
    deleteCommentById.Parameters["@Comment_ID"].Value = Comment_ID;
    
    conn.Open();
    
    deleteCommentById.ExecuteNonQuery();
    
    conn.Close();
    }
    It is perhaps worth mentioning that if I "hard code" the line I am attempting to delete then it works. For example, if I used the following within my delete button:
    CommandArgument='44'
    then the stored procedure would fire and affect line 44 as intended.



    • Moved by CoolDadTx Friday, June 30, 2017 1:59 PM ASP.NET related
    Friday, June 30, 2017 8:48 AM

All replies