Tutorial RSS
 
Navigator: Home - Advanced - Deleting with Data Access Components in VB.NET

Deleting with Data Access Components in VB.NET

This tutorial will show you how to create a Data Access Component that will both read and delete data from a SQL database. VB Version.

Click Here for this project written in Visual Studio C#.NET 2008!

Looking for more .NET Database Tutorials? Click Here!

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.


In addition to the built-in controls ASP.NET provides us in managing data sources and interacting with data, we are able to also create our own Data Access Components, for further customization for manipulation of data. In this tutorial, we will be looking at how we go about building a Data Access Component (DAC) with the ability to retrieve and delete database data.

Before anything else, we need to create a database. In this example, we will be working with a SQL database with one table, which has three columns - id, name and age. Once we have set up our database, we will add some sample records to work with. If you already have a database you wish to work with, add it to your project in Solution Explorer.

The first thing we want to do is to create our class. This will handle all the interaction with our database - reading and deleting. We will need to write a method for reading the data and a method for deleting data. We will start off with the reading method:

Private Shared ReadOnly _connectionString As String

Private _id As Integer
Private _name As String
Private _age As String

Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public Property Age() As String
Get
Return _age
End Get
Set(ByVal value As String)
_age = value
End Set
End Property

Public Function GetAll() As List(Of People)
Dim results As List(Of People) = New List(Of People)()
Dim con As New SqlConnection(_connectionString)
Dim cmd As New SqlCommand("SELECT id,name,age FROM tblPeople", con)
Using con
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
Do While dr.Read()
Dim newPerson As New People()
newPerson.Id = CInt(Fix(dr("Id")))
newPerson.Name = CStr(dr("Name"))
newPerson.Age = CStr(dr("Age"))
results.Add(newPerson)
Loop
End Using
Return results
End Function

Shared Sub New()
_connectionString = WebConfigurationManager.ConnectionStrings("connectionString").ConnectionString
End Sub


At present, the above code is just for retrieving the data from the database. We use a List collection to gather all records from the database, and then return to the object that calls the method. In this case, we will use the ObjectDataSource to call the method, but that's a little later in the ASPX code. Because we want to add the functionality of deleting data from the database, we will need to add another method, which will look something like this:

Public Sub Delete(ByVal id As Integer)
Dim con As New SqlConnection(_connectionString)
Dim cmd As New SqlCommand("DELETE tblPeople WHERE Id=@Id", con)
cmd.Parameters.AddWithValue("@Id", id)
Using con
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!


This method uses SQL statements to delete database records with the id variable passed to it from whatever calls it. In this example, the ObjectDataSource will be calling it, which we will get to a little later.
So the entire code-behind for the class looks something like this:

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Web.Configuration

Public Class People
Private Shared ReadOnly _connectionString As String

Private _id As Integer
Private _name As String
Private _age As String

Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public Property Age() As String
Get
Return _age
End Get
Set(ByVal value As String)
_age = value
End Set
End Property

Public Sub Delete(ByVal id As Integer)
Dim con As New SqlConnection(_connectionString)
Dim cmd As New SqlCommand("DELETE tblPeople WHERE Id=@Id", con)
cmd.Parameters.AddWithValue("@Id", id)
Using con
con.Open()
cmd.ExecuteNonQuery()
End Using
End Sub

Public Function GetAll() As List(Of People)
Dim results As List(Of People) = New List(Of People)()
Dim con As New SqlConnection(_connectionString)
Dim cmd As New SqlCommand("SELECT id,name,age FROM tblPeople", con)
Using con
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
Do While dr.Read()
Dim newPerson As New People()
newPerson.Id = CInt(Fix(dr("Id")))
newPerson.Name = CStr(dr("Name"))
newPerson.Age = CStr(dr("Age"))
results.Add(newPerson)
Loop
End Using
Return results
End Function

Shared Sub New()
_connectionString = WebConfigurationManager.ConnectionStrings("connectionString").ConnectionString
End Sub
End Class


The class is now fully-functional. Now we just need to construct our ASPX page to use the class.
To do this, we are going to include two controls: GridView and ObjectDataSource. By specifying a few attributes of these controls, we will unlock the functionality of the class we just created. The ASPX page will look something like this:

<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
DataKeyNames="id" AutoGenerateDeleteButton="true" Width="370px" />

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="People"
SelectMethod="GetAll" DeleteMethod="Delete" />
</form>


We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.




Click Here for this project written in Visual Studio C#.NET 2008!

Looking for more .NET Database Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

411asp.net123aspxDotNetFreaksServer Intellect