Home ASP.NET Hosting Dedicated Servers Contact Us
Announcements
Virtual Tiers
.NET Applications
XML Web Services
SQL Server Database
Web Traffic Statistics
Much More...
ASP.NET Web Site Hosting
Dedicated Servers
Windows 2003 Server
2.4 GHz Pentium 4
1024 MB RAM
80 GB Hard Drive
1000 GB/month
Fully Managed
Free Setup!
$268.00/month
Windows Dedicated Servers
Specialized Plans
10 or more Domains
Windows Services
Custom Plans
Windows Services
Learning & Support
About Us
ASP.NET Tutorial Web Sites
123aspx.com
411asp.net
dotnetfreaks.com
wwwcoder.com



ASP.NET Tutorial: Delete records from SQL Server 2000 database using ADO.NET and Visual Basic.NET



Learn More about Server Intellect



Download the Visual Studio.NET 2003 VB.NET Project Here

More ASP.NET Tutorials

Deleting records from SQL Server 2000 database using ADO.NET a fairly strait forward process, much like deleting records from Access Database.

First, you will need to import the System.Data.SqlClient namespace at the top of your page.

Imports System.Data.SqlClient

Next, we write the actual code to connect to SQL Server and delete the data. We apply the below commands to the Click Event of a button control. Notice that we are getting our values from the .Text property of the txtFirstName TextBox. Because this SQL is very simple, we are not going to use stored procedures.

Dim cmd As New SqlCommand("DELETE FROM Customers WHERE FirstName = '" & txtFirstName.Text & "'", New SqlConnection(strConn))

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()

After we delete the record, we want to display the new data set without the deleted record. This is done by calling a Method that will bind the data to a repeater control.

Dim cmd As New SqlCommand("Select * From Customers", New SqlConnection(strConn))

cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()

The flow for the code behind page is as follows

Imports System.Data.SqlClient

Public Class index
Inherits System.Web.UI.Page

Public strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx; Password=xxxxxx; Persist Security Info=True;packet size=4096"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
LoadData()
End If
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim cmd As New SqlCommand("DELETE FROM Customers WHERE FirstName = '" & txtFirstName.Text & "'", New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()
Dim strSQL As String = "Select * From Customers"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub
End Class


Download the Visual Studio.NET 2003 VB.NET Project Here

Do you have an ASP.NET Tutorial you would like to see here? Contact Us!