Mark Crompton Mark Crompton - 1 year ago 69
C# Question

Delete command not firing on post back, asp.net repeater

I have a page that contains a data repeater which in turn contains a button that is meant to delete a delete a record from an SQL database. When this button is clicked I receive the following error: Procedure or function 'usp_setKS3_DeleteAssessment' expects parameter '@intAssessmentId', which was not supplied.

If I comment out the KS3_DataSource_Assessment.Delete(); line in the code behind the page loads without error and displays the message, but does not delete the record (which I expect as the line was commented out). I have no idea what to do to make this work... any ideas?

ASP.net

<asp:SqlDataSource ID="KS3_DataSource_Assessment" runat="server" ConnectionString="<%$ ConnectionStrings:sqlWriter %>" ProviderName="System.Data.SqlClient" SelectCommandType="StoredProcedure" SelectCommand="usp_getKS3_ManageAssessments" OnSelected="KS3_DataSource_Assessment_Selected" DeleteCommand="usp_setKS3_DeleteAssessment">
<SelectParameters>
<asp:ControlParameter ControlID="KS3_DropDown_ACYear" PropertyName="SelectedValue" Name="intACYearId" Type="Int32" />
<asp:ControlParameter ControlID="KS3_DropDown_Subject" PropertyName="SelectedValue" Name="intSubjectId" Type="Int32" />
<asp:ControlParameter ControlID="KS3_DropDown_Year" PropertyName="SelectedValue" Name="intYearGroup" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="intAssessmentId" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:Repeater runat="server" ID="KS3_Repeater_Assessments" EnableViewState="true" DataSourceID="KS3_DataSource_Assessment" OnItemDataBound="KS3_Repeater_Assessments_ItemDataBound">
<ItemTemplate>
<!-- Assessments -->
<asp:Table runat="server" ID="KS3_Table_Assessments" CssClass="width100pc">
<asp:TableRow CssClass="SectionSubHeader">
<asp:TableCell><%# Eval("txtAssessmentTitle").ToString() %></asp:TableCell>
<asp:TableCell CssClass="width25px alignCenter alignVerticalMiddle" RowSpan="2"><asp:ImageButton SkinID="imgButtonDelete" runat="server" ID="KS3_Button_DeleteAssessment" OnCommand="KS3_Button_DeleteAssessment_Command" CommandArgument='<%# Eval("intAssessmentID").ToString() %>' OnClientClick="return confirm('Are you sure you want to delete this assessment?\n\nOnce deleted all data is lost.');" /></asp:TableCell>
<asp:TableCell CssClass="width25px alignCenter alignVerticalMiddle" RowSpan="2">Up</asp:TableCell>
<asp:TableCell CssClass="width25px alignCenter alignVerticalMiddle" RowSpan="2">Down</asp:TableCell>
</asp:TableRow>
<asp:TableRow CssClass="BackgroundBlack fontStyleItalic">
<asp:TableCell><%# Eval("txtAssessmentDescription").ToString() %></asp:TableCell>
</asp:TableRow>
</asp:Table>

<!-- Columns -->
<asp:SqlDataSource ID="KS3_DataSource_Columns" runat="server" ConnectionString="<%$ ConnectionStrings:sqlWriter %>" ProviderName="System.Data.SqlClient" SelectCommandType="StoredProcedure" SelectCommand="usp_getKS3_ManageAssessments">
<SelectParameters>
<asp:Parameter Name="intACYearId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

</ItemTemplate>
</asp:Repeater>


Code Behind

protected void Page_Load(object sender, EventArgs e)
{

}

protected void KS3_Button_SelectSubject_Click(object sender, EventArgs e)
{
if (KS3_DropDown_Subject.SelectedIndex == 0) { DisplayMessage("Validation Error", "Please select a subject from the list below.", "error"); KS3_Panel_DataToManage.Visible = false; }
else if (KS3_DropDown_Year.SelectedIndex == 0) { DisplayMessage("Validation Error", "Please select a year group from the list below.", "error"); KS3_Panel_DataToManage.Visible = false; }
else
{
//No errors detected, start data display
Master.systemMessageHide();
KS3_Panel_DataToManage.Visible = true;

//Update display
KS3_Label_Title.Text = KS3_DropDown_Subject.SelectedItem.Text.Trim() + " - " + KS3_DropDown_Year.SelectedItem.Text.Trim();

//Bind the new data to the repeater
KS3_Repeater_Assessments.DataBind();
}
}

public void DisplayMessage(string title, string message, string type)
{
Master.systemMessageShow(title, message, type);
}

protected void KS3_DataSource_Assessment_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
if (e.AffectedRows < 1) { KS3_Panel_NoAssessments.Visible = true; }
else { KS3_Panel_NoAssessments.Visible = false; }
}


protected void KS3_Button_DeleteAssessment_Command(object sender, CommandEventArgs e)
{
KS3_Label_Title.Text = "DataSubmitted";
KS3_DataSource_Assessment.DeleteParameters["intAssessmentId"].DefaultValue = e.CommandArgument.ToString();
Trace.Write("Item Value: " + e.CommandArgument.ToString());
KS3_DataSource_Assessment.Delete();
KS3_Repeater_Assessments.DataBind();
DisplayMessage("Assessment Delete", "The selected assessment has been deleted and all associated data has been removed.", "success");
}

Answer Source

It looks that you are missing this attribute of the data source control:

DeleteCommandType="StoredProcedure"

Default value is Text, which makes control think that the command provided is an SQL statement. Since you are using stored procedure, this is not the behavior you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download