Search code examples
asp.netobjectdatasourceconfirmation

Client Deletion Confirmation


I have the "Delete" option showing on my GridView that is populated via SQL Data Source. I am trying to implement a way where the user will be asked to verify that they wish to delete the row of information. I have been following the guide from http://msdn.microsoft.com/en-us/library/ms972940.aspx.

The issue I'm currently having is when I add my ObjectDataSource, I select the class and when I go to choose the desired method it is not listed (Fig. 37).

Addition information - I originally created the GridView to be populated via SQLDataSource, now I'm trying to transition over to ObjectDataSource and add my DeleteMethod. I am stuck at this part and do not yet have the popup window that will ask the user to continue. I will work on that after I overcome the current challenge.

Here is my aspx code:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContentAdmin" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContentAdmin" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="CourseSection_ID" DataSourceID="SqlDataSource1" 
        ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="CourseSection_ID" HeaderText="CourseSection_ID" 
                InsertVisible="False" ReadOnly="True" SortExpression="CourseSection_ID" />
            <asp:BoundField DataField="section" HeaderText="section" 
                SortExpression="section" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MGT598DBConnectionString1 %>" 
        SelectCommand="SELECT * FROM [Course_Section]" 
        DeleteCommand="DELETE FROM [Course_Section] WHERE [CourseSection_ID] = @CourseSection_ID" 
        InsertCommand="INSERT INTO [Course_Section] ([section]) VALUES (@section)" 
        UpdateCommand="UPDATE [Course_Section] SET [section] = @section WHERE [CourseSection_ID] = @CourseSection_ID">
        <DeleteParameters>
            <asp:Parameter Name="CourseSection_ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="section" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="section" Type="String" />
            <asp:Parameter Name="CourseSection_ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
    </asp:ObjectDataSource> </asp:Content>

My code behind file:

namespace MGT598GraduateProject.View
{
    public partial class ViewCourse_Section : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //http://msdn.microsoft.com/en-us/library/ms972940.aspx
        }

        public static void DeleteMethod(int CourseSection_ID)
        {
            // deletes a specified Order Details record 
            // from the Northwind Products table
            string sql = "DELETE FROM [Order Details] WHERE OrderID = " + "@OrderID";

            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MGT598DBConnectionString1"].ConnectionString))
            {
                SqlCommand myCommand = new SqlCommand(sql, myConnection);
                myCommand.Parameters.Add(new SqlParameter("@CourseSection_ID", CourseSection_ID));
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }
    }
}

Solution

  • There are two issues here.

    First, you have a few fundamental basics wrong.

    You do not need an SQLDataSource AND an ObjectDataSource.

    As you have pointed the Gridview to the SqldataSource (DataSourceID="SQLDataSource1") the object data source is infact useless.

    You can delete the following code from the aspx:

     <asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
     <asp:ObjectDataSource> </asp:Content> 
    

    And delete the entire DeleteMethod from the code behind.

    If you read the tutorial you linked us to you will see that they are two seperate sections and not to be done together.

    Second, you need to have the user 'verify' the deletion.

    To do this, modify your block to match the following:

    <Columns>
    <asp:TemplateField>
        <ItemTemplate>
             <asp:LinkButton ID="LinkButton1" Runat="server" 
             OnClientClick="return confirm('Are you sure you want to delete this record?');"
             CommandName="Delete">Delete Item
             </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
    <asp:BoundField DataField="CourseSection_ID"
        HeaderText="CourseSection_ID"
        InsertVisible="False" 
        ReadOnly="True" 
        SortExpression="CourseSection_ID" />
    <asp:BoundField DataField="section" 
        HeaderText="section"
        SortExpression="section" />
    </Columns>