Display data using DataAdapter of disconnected model in VB.NET
DataAdapter enable data transfer from physical database to in memory database and back again.
In my previous articles I have used connected model. In that I have used SqlConnection, SqlCommand and SqlDataReader object to connect to database and retrieve data. in this article I am going to use disconnected model. In disconnected model, you don't need to keep a connection to the database open. In disconnected model. You need to use DataAdapter, DataTable, DataView and DataSet.
DataAdapter: DataAdapter enable data transfer from physical database to in memory database and back again.
In this article I am using DataAdapter to show you how its work.
Web.config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="Employee" connectionString="Data Source=Sapna-PC\Sapna;Initial Catalog=master;User ID=sa;Password=SapnaBeniwal" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
</configuration>
App_Code/Employee.vb
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Public Class Employee
Public Shared ReadOnly Connection_String As String
Public Function All_Detail() As DataTable
Dim sda As New SqlDataAdapter("Select * from Employee", Connection_String)
'creata a DataTable
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Function
Shared Sub New()
Connection_String = WebConfigurationManager.ConnectionStrings("Employee").ConnectionString
End Sub
End Class
Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1">
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" TypeName="Employee" SelectMethod="All_Detail" runat="server"></asp:ObjectDataSource>
</div>
</form>
</body>
</html>
Output: