ADO.NET DataAdapter in VB.NET

Here we see how to use to connect to a database, retrieve records, place them into a dataset using dataadapter and display the records on the web form.
  • 2687

Data adapter object

DataAdapter provides the communication between the Dataset and the Datasource. The DataAdapter object is used to retrieve the data from the database and place that data into a DataSet. The DataSet was them bound to a control such as GridView and displayed in a web form.

Creating a SqlDataAdapter Object

To create a SqldataAdapter we call connection object .

adapt As SqlDataAdapter = New SqlDataAdapter(com)

Now creating a table in Database and insert the value. like this

table emp3


firstname varchar(20),

lastname varchar(30)


Now using select statement.

select * from emp3




For example

The below example will show the employee firstname and last name from the table emp3 from MS SQL server database on the form. Now taking a Button control and GridView control on the form. The form looks like this.



Now double click on the Button control and add the following code.

Imports System.Data.SqlClient

Public Class WebForm1

       Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

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

        Dim con As New SqlConnection(str)

        Dim com As New SqlCommand("select * from emp3", con)

        Dim adapt As SqlDataAdapter = New SqlDataAdapter(com)

        Dim dt As New DataSet()


        adapt.Fill(dt, "emp3")


        GridView1.DataSource = dt


    End Sub

End Class

Now run the application and click on the button. It shows the database table on the GridView.




More Articles

© 2020 DotNetHeaven. All rights reserved.