Thursday, 21 July 2011

Using the ObjectDataSource Control with VB


ObjectDataSource Controls can be used to represent any object. This tutorial will show you how we can use the ObjectDataSource control with collections and a DataGrid. VB version.

The Object Data Source Control can be used to represent any object. This tutorial will show how we can get started using the ObjectDataSource.
In this example, we will look at how we can create a list in a separate class and then use the ObjectDataSource to use this method in the class as the data source.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The first thing we will do is create our class and method. We will create a class called people, and a method that will create a collection of names. The method will make use of IEnumerable and look something like this:

Public Function GetPeople() As List(Of String)
Dim people As List(Of String) = New List(Of String)()
people.Add("Ryu")
people.Add("Alain")
people.Add("Katrina")
people.Add("Ali Babba")
people.Add("Jessica")
Return people
End Function

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

In a real-world situation, we probably wouldn't use a data source like this, but for this example, we will demonstrate how the ObjectDataSource can be used.
Because we are using a collection, we need to import the namespace. The entire class looks like this:

Imports Microsoft.VisualBasic Public Class People
Public Function GetPeople() As List(Of String)
Dim people As List(Of String) = New List(Of String)()
people.Add("Ryu")
people.Add("Alain")
people.Add("Katrina")
people.Add("Ali Babba")
people.Add("Jessica")
Return people
End Function
End Class

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

We are hard-coding some names to create a collection in the back-end. Now to use, this we will add an ObjectDataSource control to our ASPX page, as well as a DataGrid to read it:

<form id="form1" runat="server">
<asp:DataGrid ID="DataGrid1" runat="server" /><br />
<asp:Button ID="butGet" runat="server" Text="Get" onclick="butGet_Click" />

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="People"
SelectMethod="GetPeople" />
</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.

Notice that our DataGrid's DataSourceID is assigned to that of the ObjectDataSource, which has a TypeName of People - this refers to our class, and the SelectMethod refers to our method within the class. We have also added a button with a click event to assign the ObjectDataSource to the DataGrid. This way we can see what happens. We will add the following code to the button:

Protected Sub butGet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butGet.Click
DataGrid1.DataSourceID = "ObjectDataSource1"
DataGrid1.DataBind()
End Sub

Now when we run this web application, we will be presented with a button, which when clicked, should display the DataGrid of the collection we created in the class.

Looking for the C#.NET 2008 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment