ASP 101 - Active Server Pages 101 db_rec_kill.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat="server">
    Sub Page_Load(Sender As Object, E As EventArgs) 
        Dim myConnection As SqlConnection 
        Dim myCommand    As SqlCommand
        Dim myDataReader As SqlDataReader
        Dim strRecordsToDelete As String
        Dim arrRecordsToDelete As Array
        Dim I                  As Integer
        ' Create connection object using our SQL Server connection string.
		myConnection = New SqlConnection("Data Source=10.2.2.133;" _
			& "Initial Catalog=samples;User Id=samples;Password=password;" _
			& "Connect Timeout=15;Network Library=dbmssocn;")
        ' Open the connection to the database
        myConnection.Open
        ' Create our command object
        myCommand = New SqlCommand()
        myCommand.Connection = myConnection
        ' First we need to see if we need to delete any records.
        strRecordsToDelete = Request.Form("chkDelete")
        If strRecordsToDelete <> "" Then
            ' Check to be sure someone didn't pass in a
            ' SQL injection attack to mess with our table.
            arrRecordsToDelete = Split(strRecordsToDelete, ",")
            For I = LBound(arrRecordsToDelete) to UBound(arrRecordsToDelete)
                If Not IsNumeric(arrRecordsToDelete(I)) Then
                    strRecordsToDelete = "0"
                    Exit For
                End If
            Next I
            myCommand.CommandText = "DELETE FROM scratch WHERE id IN (" & strRecordsToDelete & ");"
            'Response.Write(myCommand.CommandText)
            myCommand.ExecuteNonQuery()
            lblRecordsDeleted.Text = "Records Deleted: " & strRecordsToDelete
            '******************************************************************
            ' If users are constantly deleting records, we need to make sure
            ' they don't empty out the table entirely for other users.  Add a
            ' new record just to make sure the table stays populated.
            myCommand.CommandText = "INSERT INTO [scratch] (text_field, integer_field, date_time_field) " _
                            & "VALUES ('" _
                            & CStr(WeekdayName(WeekDay(Now()))) & "', '" _
                            & CInt(Day(Now())) & "', '" _
                            & Now() & "');"
            myCommand.ExecuteNonQuery()
            '******************************************************************
        Else
            lblRecordsDeleted.Text = Nothing
        End If
        ' Now we retrieve the remaining rows in the table and display them.
        myCommand.CommandText = "SELECT * FROM scratch;"
        ' Use the ExecuteReader method to execute our query and return the results.
        myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        ' DataBind the data to our DataGrid.
        dgRecKill.DataSource = myDataReader
        dgRecKill.DataBind()
        ' Close our DataReader and Connection
        myDataReader.Close()
        myConnection.Close()
    End Sub
</script>
<html>
<head>
<title>ASP.NET Database Record Killer Sample</title>
</head>
<body>
<form runat="server"
	onsubmit="return(confirm('Do you really want to delete the selected records?'));">
<p>
<asp:Label id="lblRecordsDeleted" runat="server" />
</p>
<asp:DataGrid id="dgRecKill" runat="server"
    EnableViewState = "False"
    BorderWidth = "1"
    CellSpacing = "2"
    CellPadding = "2"
    HeaderStyle-Font-Bold = "True"
    AutoGenerateColumns = "False"
    ShowFooter = "True"
>
    <Columns>
        <asp:BoundColumn HeaderText="id"              DataField="id"              />
        <asp:BoundColumn HeaderText="text_field"      DataField="text_field"      />
        <asp:BoundColumn HeaderText="integer_field"   DataField="integer_field"   />
        <asp:BoundColumn HeaderText="date_time_field" DataField="date_time_field" />
        <asp:TemplateColumn HeaderText="Delete?" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <input type="checkbox" name="chkDelete" value="<%# DataBinder.Eval(Container.DataItem, "id") %>">
            </ItemTemplate>
            <FooterTemplate>
                <input type="submit" value="Delete!">
            </FooterTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>
</form>
<p>
If you run out of records to delete, you can add more via our
<a href="http://aspnet.asp101.com/samples/db_add.aspx">Database Insert</a> sample.
</p>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/db_rec_kill_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>