Deleting SQL Server Data

If you have code on your ASP.NET pages that allows visitors to enter a record, or where you add records through code without their input, you will likely need to delete records from your pages. This technique shows you how to use a SQL Delete query to delete records from an SQL Server table.

When the page first loads, the following code will populate the DropDownList control:

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)If Not IsPostBack ThenDim DBConn as SQLConnectionDim DBCommand As SQLDataAdapter Dim DSPageData as New DataSet
DBConn = New SQLConnection("server=localhost;" _& "Initial Catalog=TT;" _& "User Id=sa;" _"Password=yourpassword;") DBCommand = New SQLDataAdapter _ ("Select LastName + ‘ + FirstName "
& "as EmpName, EmpID " _& "From Employees " _& "Order By LastName, FirstName", DBConn)
DBCommand.Fill(DSPageData, _ "Employees")ddlEmps.DataSource = _DSPageData.Tables("Employees").DefaultViewddlEmps.DataBind()End IfEnd Sub

and this procedure fires when the Button control is clicked:

Sub SubmitBtn_Click(Sender As Object, E As EventArgs)


Iterating Through Records in an SQL Server Table

In many situations you no longer need to iterate through records in an SQL Server table in ASP.NET pages as you did with ASP. This is because you can now bind DataSet Tables directly to controls.

But on occasion you may still need to iterate through all the records in a DataSet Table so that they can be processed in some special way.

The DataSet Table object contains a Rows collection that allows you to iterate through each record it contains. This technique shows you how to create a loop so that you can process each record from an SQL Server table through a DataSet Table object.