ARTICLE

How to bind dataset with label control in VB.NET

Posted by Rohatash Kumar Articles | ADO.NET in VB.NET February 17, 2011
Here we see how to use ado.net to connect to a database and binding DataSet with the Label control.
Download Files:
 
Reader Level:

Creating connection object

To create a connection we pass the connection string as a parameter in connection object.

Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

Dim con As New SqlConnection(str)

 

The above string defines the connection string which is used to connect the database with the application.

 

Now we create a database table and insert some values in this table. Table looks like this.

 

create table logn

(

username varchar(50),

password varchar(40)

)

go

insert into logn values('monu','mohan')

go

insert into logn values('Rohatash','rohit')

go

insert into logn values('Manoj','singh')

go

select * from logn;

The table looks like this.


d2.gif

 

Table1.gif

 

Data set Bind to Label

 

To bind DataSet with the label use DataBindings property to bind the control. The below line show the Data binding with Label control.

 

Label1.DataBindings.Add("text", dataset1, "logn.username")

 

For example

 

Drag and drop two Label control on the form. The form looks like this.


l2.gif 

Figure1.gif

 

Now double click on the form load and add the following code.

 

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

        Dim con As New SqlConnection(str)

        Dim str1 As String = "SELECT * FROM logn"

        Dim da As New SqlDataAdapter(str1, con)

        Dim dataset1 As New DataSet()

        da.Fill(dataset1, "logn")

        ' Bind Label1 to username column of the logn table

        Label1.DataBindings.Add("text", dataset1, "logn.username")

        ' Bind Label2 to password column of the logn table

        Label2.DataBindings.Add("text", dataset1, "logn.password")

    End Sub

End Class

 

Now run the application and test it.

l3.gif

 

Figure2.gif

Login to add your contents and source code to this article
share this article :
post comment
 

C# and VB.NET are popular in recent years. I was introduced to use a free component for C# and VB.NET. I hope it may help you. You can click my autograph.

Posted by tomason Feb 17, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor