Updating SQL Server Data by ASP.Net

Another type of query that you will often need to run on your ASP.NET pages that include database connectivity are Update queries. Update queries allow you to modify existing records in an SQL Server database table. This technique shows you how to run an Update query from your ASP.NET code.The page presented with this technique allows the visitor to give all the employees in the Employees table a raise. The visitor enters the factor for the raise and every employee has their salary increased by that factor.Defined on the ASP.NET page is this TextBox control for entering the raise amount.

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.