SlideShare a Scribd company logo
A Programme Under the Compumitra Series
Data binding is a functionality that allows us to connect data
from a source to a target.
GridView Control–VB
LAB WORK GUIDE
Data Binding in GridView using wizard
 Example Template Creation.
 Database file addition in App_data
 Data Binding with GridView using wizard
 Output
 Code explanation
 Modification Trials
Data Binding in GridView using VB Code
 Example Template Creation.
 Data Binding with GridView using vb code
 Output
 Code explanation
 Modification Trials
 Error Trials
 Practice Exercise
 Learning Summary Review
 References.
OUTLINE
Data Binding
Using GridView control through wizard
Data binding is a general technique that binds two
data/information sources together and maintains
them in sync.
The GridView control enables you to connect to a data
source and display data in tabular format.
"VB"
GridViewUsingWizardVB: Creating And Renaming Webpage
2. Now Rename this page with "Rename"
option by Right Clicking on "Default.aspx"
1. Select the "Default.aspx" page
in the "Solution Explorer"
3. Rename "Default.aspx"
page to "GridView.aspx" page.
• Follow Standard Website Creation Steps and set your path to
C:Learner<student-id>DataBindingGridViewUsingWizardVB
• Add New Default.aspx page in your Website
1. Select the "Root Path" in the
"Solution Explorer" and Right
Click on it
2. Now select "Add ASP.NET
Folder" option
3. And then select "App_Data"
GridViewUsingWizardVB: Adding App_Data Folder
Adding Database To App_Data
"VB"
GridViewUsingWizardVB: Adding Database-1
Right Click on "App_Data" and select "Add
Existing Item.." option in Solution Explorer
to attach the database to "App_Data"
For attaching database to your current website follow the
instruction.
GridViewUsingWizardVB: Adding Database-2
1. Select the database you
created
2. Attach the database by
clicking the ‘Add’ Button
Your attached database will
appear in 'App_Data' folder.
To attach the database follow the path "C:Learner<Student-
Id>DatabaseSale".
GridViewUsingWizardVB: Adding Gridview Control
The GridView control enables you to connect to a data source and
display data in tabular format
Drag and Drop "GridView" control
in div From 'Data' Toolbox
Select '<New data source…>' option
from 'Choose Data Source:'
dropdown list of 'GridView Tasks' to
attach a data source in GridView
control.
GridViewUsingWizardVB: Adding AccessDataSource in GridView-1
1. Select "Access
Database" and then
click "OK" button.
2. Click on "Browse" button to
browse for database file
1. Double click on App_Data and select
the database and click "OK" button
Now follow the instructions to select the database
GridViewUsingWizardVB: Adding AccessDataSource in GridView-2
2. Click "Next"
GridViewUsingWizardVB: Adding AccessDataSource in GridView-3
1. Select the table which you
want to access from the
database from DropDown list
If you have applied any query on the tables you can also select
that query from the DropDownList.
Select the fields which You
want to display and click
"Next" button
GridViewUsingWizardVB: Adding AccessDataSource in GridView-4
1. First Click "Test Query" to
test the query
2. Then Click "Finish" button
Test Query is used to test that whether the data is accessed
according to our need or not.
Your tested query will
show here.
GridViewUsingWizardVB: View Of AccessDataSource
'AccessDataSource' control
will automatically appear
here.
Now Run Code By
pressing 'F5'
GridViewUsingWizardVB: Output
Output on browser
Content of the ItemMaster table
will display in GridView
GridViewUsingWizardVB: Source Code Explanation -1
"AutoGenerateColumn"
indicating whether bound
fields are automatically
created for each field
This source code describes your attached database
and select query
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ItemId" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False"
ReadOnly="True" SortExpression="ItemId" />
<asp:BoundField DataField="Item_name" HeaderText="Item_name"
SortExpression="Item_name" />
<asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" />
<asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate"
SortExpression="Product_Rate" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]">
</asp:AccessDataSource>
"DataKeyNames" defines
key in the table
"DataSourceID" defines
data source as
"AccessdataSource1"
GridViewUsingWizardVB: Source Code Explanation -2
"DataField" Displays name
of the field
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ItemId" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False"
ReadOnly="True" SortExpression="ItemId" />
<asp:BoundField DataField="Item_name" HeaderText="Item_name"
SortExpression="Item_name" />
<asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" />
<asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate"
SortExpression="Product_Rate" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]">
</asp:AccessDataSource>
"BoundField" displays the value
of specified DataSource field
as text
"SortExpression" Defines
name on which sorting can
be performed
"HeaderText" shows Header
name of the column
GridViewUsingWizardVB: Modification trials
 Select "All Fields" query instead of "ItemMaster" and observe
the difference.
You will see that you are getting the data from all the
tables.
 Instead of selecting ‘*’ while selecting columns select any three
column and see the difference.
Now you can observe from the result that only selected fields
are showing.
 Change the Header Text of column "Item_name" from
"Item_name" to "Product Name" and see the difference after
running the page.
You will see that Header of column is changed.
Data Binding
Using GridView control through VB Code
Data binding provides a simple and consistent
way for applications to present and interact with
data.
The GridView control enables you to connect to
a data source and display data in tabular
format.
GridViewUsingCSCode: .aspx File Creation
1. Drag and Drop a 'Button' control
in div from Standard Toolbox
• Follow Standard Website Creation Steps and set your path to
"C:Learner<student-id>DataBindingGridViewUsingVBCodeVB"
• Add New Default.aspx page in your Website
2. Introduce a new line and Drag and Drop
a 'GridView' control from Data Toolbox
•Add the 'App_Data' folder
•To add the database, follow the path "C:Learner<Student-
Id>DatabaseSale".
GridViewUsingCSCode: Adding Database
1. Add 'App_Data' folder
2. And Database 'Sale.mdb' in
App_Data like previous exercise.
GridViewUsingVBCode: Copy/Paste Code
Dim dbconn As OleDbConnection
Dim sql As String
Dim dbcomm As OleDbCommand
Dim dbread As OleDbDataReader
Copy/Paste or type this code under
the "Partial Class Default" as shown
in the template
Imports System.Data
Imports System.Data.OleDb Type this code above "Partial
Class Default"
Go to 'default.aspx.vb' by double clicking on blank space
of 'default.aspx'
GridViewUsingVBCode: Button1_Click Event handler Code
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" &
Server.MapPath("~/App_Data/sale.mdb"))
dbconn.Open()
sql = "SELECT * FROM ItemMaster"
dbcomm = New OleDbCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader()
GridView1.DataSource = dbread
GridView1.DataBind()
dbread.Close()
dbconn.Close()
Copy/Paste or type the code under
"Button1_Click Event" and run this code with "F5"
• Generate the Button1_Click Event in the VB code by double-clicking
on the Button control of .aspx file.
GridViewUsingVBCode: Run GridView Page
First click the
button
Result will show here.
When you will run your page then after clicking button you will get the output
Imports System.Data
Imports System.Data.OleDb
Partial Class _Default Inherits System.Web.UI.Page
Dim dbconn As OleDbConnection
Dim sql As String
Dim dbcomm As OleDbCommand
Dim dbread As OleDbDataReader
GridViewUsingVBCode: Code Explanation-1
Imports System.Data.OleDb provide
OleDb.Net provider for MS Access
database connectivity
This line declare
OleDbConnecton class type
variable which is used to
make a connection with MS
Access Database
Imports System.Data Allows to use the
DataSet Class Methods.
This line declare
OleDbDataReader class type
variable which is used to read
the Data.
This line declare OleDbCommand
class type variable which is used to
run SQL queries.
GridViewUsingVBCode: Code Explanation-2
Protected Sub Button1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Button1.Click
dbconn = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;dat
a source=" & Server.MapPath("~/App_Data/sale.mdb"))
dbconn.Open()
sql = "SELECT * FROM ItemMaster"
dbcomm = New OleDbCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader()
GridView1.DataSource = dbread
GridView1.DataBind()
dbconn.Close()
End Sub
This statement creates an object
of OleDbConnection class.
This is a path of Database.
This statement initialize the SQl
query in sql String type variable,
which is used in next line
Define the DataSource ID of
GridView equal to DataReader
This string type argument is passed, when
we create a connection with MS Access
This statement open the
OleDbConnection
This statement creates an object
of OleDbConnection class. This
need two arguments, 1- SQL
Query, 2- OleDbConnection class
object
This statement execute the SQl
query and store its result in
OleDbDataReader class object
This statement Close the
OleDbConnection
GridViewUsingVBCode: Modification Trials-1
 In SELECT statement, replace '*' sign with 'ItemId,
Item_name'
Run and watch the effect
Now you can observe that the result displays
only the selected fields.
 Add 'As Product_Name' just after the 'Item_name'
SELECT statement.
Syntax: <Field Name> As <New Name>
You will see that Header of column will change
when you will run the page.
GridViewUsingVBCode: Modification Trials-2
 Click on 'Auto Format…' link in 'GridView
Task' and select the 'scheme' equal to 'Black
& Blue 2'
Run and watch the effect
GridViewUsingVBCode: Error Trials
 Remove the Namespace files which we have inserted in the beginning.
You will get the error as given below
BC30002: Type 'OleDbConnection' is not defined.
 Instead of 'Dim dbconn As OleDbConnection' line type Dim dbconn and
observe the error showing at ToolTip
So it is necessary to Define the variable with As clause.
 Remove the line dbconn.open() and see the result after running the
page.
The error will be shown that is given below.
ExecuteReader requires an open and available Connection. The
connection's current state is closed.
To remove this error, connection should be open.
GridViewUsingVBCode: Practice Exercise
Create a website named Expenses
Add the Database 'Monthly Expenses'
Show column "CatId" and "Description"
from table "Expense Category" and "Date"
and "Detail" from table "Expenses" in a
Grid view using wizard option.
Show column "CatId" and "Description"
from the "Expense Category" table and
"Date" and "Detail" from the "Expenses"
table in a Grid view using code.
GridViewUsingVBCode: Learning Summary Review
Use of GridView control
Adding database to App_Data.
Linking of MS Access database with Grid View
through wizard.
Linking of MS Access database with Grid View
through Code.
Use of OleDb classes.
Bibliography
http://msdn.microsoft.com/en-
us/library/fbk67b6z.aspx
http://www.w3Schools.com
http://www.switchonthecode.com/tutorials/csharp-
tutorial-binding-a-datagridview-to-a-database
http://www.codeproject.com/KB/aspnet/DataGridVi
ew__GridView.aspx
http://www.codersource.net/asp-net/asp-net-2-
0/grid-view-control-in-asp-net-2-0.aspx
Ask and guide me at
sunmitraeducation@gmail.com
Share this information with as many people as
possible.
Keep visiting www.sunmitra.com for
programme updates.

More Related Content

PPTX
Grid View Control CS
PPT
VB6 Using ADO Data Control
PPTX
Data base connectivity and flex grid in vb
PDF
Step by Step Asp.Net GridView Tutorials
PPT
Database Connection
PDF
Data Binding and Data Grid View Classes
PPT
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
Grid View Control CS
VB6 Using ADO Data Control
Data base connectivity and flex grid in vb
Step by Step Asp.Net GridView Tutorials
Database Connection
Data Binding and Data Grid View Classes
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book

What's hot (20)

PPTX
Databind in asp.net
PPT
ADO CONTROLS - Database usage
PPT
Database programming in vb net
PPTX
Web based database application design using vb.net and sql server
PPT
PPT
ASP.NET 10 - Data Controls
PPT
Ado.net
PPT
Introduction to ADO.NET
PPTX
Ado .net
PPTX
For Beginners - Ado.net
PPSX
ADO.NET
PPT
Chap14 ado.net
PDF
Visual basic databases
PPT
ASP.NET 09 - ADO.NET
PDF
Visual Basic.Net & Ado.Net
PPT
Data Connection using ADO DC
PPTX
asp.net data controls
PPTX
Database application and design
PPS
VISUAL BASIC .net data accesss vii
PPTX
Database connectivity to sql server asp.net
Databind in asp.net
ADO CONTROLS - Database usage
Database programming in vb net
Web based database application design using vb.net and sql server
ASP.NET 10 - Data Controls
Ado.net
Introduction to ADO.NET
Ado .net
For Beginners - Ado.net
ADO.NET
Chap14 ado.net
Visual basic databases
ASP.NET 09 - ADO.NET
Visual Basic.Net & Ado.Net
Data Connection using ADO DC
asp.net data controls
Database application and design
VISUAL BASIC .net data accesss vii
Database connectivity to sql server asp.net
Ad

Similar to Grid Vew Control VB (20)

DOCX
Grid view control
PPT
ASP.NET Session 13 14
PPTX
Application of Insert and select notes.ppt x
PDF
Part 3 binding navigator vb.net
PPTX
PPTX
76.pptx ajx ppt file for univercity of granted
PPT
Chapter12 (1)
PPTX
Priyank Goel PPT.pptx
PPTX
2.3.1 creating database, table and relationship on Access 2003
DOCX
How to work a database
PPT
HARJOT.ppt gggggggggggggggggggggggggggggggggggggggg
PPTX
Operate Spreadsheet applications ppt.pptx
PPTX
Work with data in ASP.NET
PPTX
Access ppt
PPTX
Datasource in asp.net
PPTX
DB.pptx data base HNS level III 2017 yearx
PPTX
Module 08 Access & Use Database Application.pptx
PPTX
dbms ms access basics and introduction to ms access
DOCX
INTRODUCTION TO ACCESSOBJECTIVESDefine th.docx
PPTX
Ms access
Grid view control
ASP.NET Session 13 14
Application of Insert and select notes.ppt x
Part 3 binding navigator vb.net
76.pptx ajx ppt file for univercity of granted
Chapter12 (1)
Priyank Goel PPT.pptx
2.3.1 creating database, table and relationship on Access 2003
How to work a database
HARJOT.ppt gggggggggggggggggggggggggggggggggggggggg
Operate Spreadsheet applications ppt.pptx
Work with data in ASP.NET
Access ppt
Datasource in asp.net
DB.pptx data base HNS level III 2017 yearx
Module 08 Access & Use Database Application.pptx
dbms ms access basics and introduction to ms access
INTRODUCTION TO ACCESSOBJECTIVESDefine th.docx
Ms access
Ad

More from sunmitraeducation (20)

PPTX
Java Introduction
PPTX
Installing JDK and first java program
PPTX
Project1 VB
PPTX
Project1 CS
PPTX
PPTX
Database Basics Theory
PPTX
Visual Web Developer and Web Controls VB set 3
PPTX
Visual Web Developer and Web Controls CS set 3
PPTX
Progamming Primer Polymorphism (Method Overloading) VB
PPTX
Programming Primer EncapsulationVB
PPTX
Programming Primer Encapsulation CS
PPTX
Programming Primer Inheritance VB
PPTX
Programming Primer Inheritance CS
PPTX
ProgrammingPrimerAndOOPS
PPTX
Web Server Controls VB Set 1
PPTX
Web Server Controls CS Set
PPTX
Web Controls Set-1
PPTX
Understanding IDEs
PPTX
Html Server Image Control VB
PPTX
Html Server Image Control CS
Java Introduction
Installing JDK and first java program
Project1 VB
Project1 CS
Database Basics Theory
Visual Web Developer and Web Controls VB set 3
Visual Web Developer and Web Controls CS set 3
Progamming Primer Polymorphism (Method Overloading) VB
Programming Primer EncapsulationVB
Programming Primer Encapsulation CS
Programming Primer Inheritance VB
Programming Primer Inheritance CS
ProgrammingPrimerAndOOPS
Web Server Controls VB Set 1
Web Server Controls CS Set
Web Controls Set-1
Understanding IDEs
Html Server Image Control VB
Html Server Image Control CS

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Empathic Computing: Creating Shared Understanding
PDF
Approach and Philosophy of On baking technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Mobile App Security Testing_ A Comprehensive Guide.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Empathic Computing: Creating Shared Understanding
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
A comparative analysis of optical character recognition models for extracting...
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
cloud_computing_Infrastucture_as_cloud_p
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
OMC Textile Division Presentation 2021.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Machine Learning_overview_presentation.pptx
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.

Grid Vew Control VB

  • 1. A Programme Under the Compumitra Series Data binding is a functionality that allows us to connect data from a source to a target. GridView Control–VB LAB WORK GUIDE
  • 2. Data Binding in GridView using wizard  Example Template Creation.  Database file addition in App_data  Data Binding with GridView using wizard  Output  Code explanation  Modification Trials Data Binding in GridView using VB Code  Example Template Creation.  Data Binding with GridView using vb code  Output  Code explanation  Modification Trials  Error Trials  Practice Exercise  Learning Summary Review  References. OUTLINE
  • 3. Data Binding Using GridView control through wizard Data binding is a general technique that binds two data/information sources together and maintains them in sync. The GridView control enables you to connect to a data source and display data in tabular format.
  • 4. "VB" GridViewUsingWizardVB: Creating And Renaming Webpage 2. Now Rename this page with "Rename" option by Right Clicking on "Default.aspx" 1. Select the "Default.aspx" page in the "Solution Explorer" 3. Rename "Default.aspx" page to "GridView.aspx" page. • Follow Standard Website Creation Steps and set your path to C:Learner<student-id>DataBindingGridViewUsingWizardVB • Add New Default.aspx page in your Website
  • 5. 1. Select the "Root Path" in the "Solution Explorer" and Right Click on it 2. Now select "Add ASP.NET Folder" option 3. And then select "App_Data" GridViewUsingWizardVB: Adding App_Data Folder
  • 7. "VB" GridViewUsingWizardVB: Adding Database-1 Right Click on "App_Data" and select "Add Existing Item.." option in Solution Explorer to attach the database to "App_Data" For attaching database to your current website follow the instruction.
  • 8. GridViewUsingWizardVB: Adding Database-2 1. Select the database you created 2. Attach the database by clicking the ‘Add’ Button Your attached database will appear in 'App_Data' folder. To attach the database follow the path "C:Learner<Student- Id>DatabaseSale".
  • 9. GridViewUsingWizardVB: Adding Gridview Control The GridView control enables you to connect to a data source and display data in tabular format Drag and Drop "GridView" control in div From 'Data' Toolbox Select '<New data source…>' option from 'Choose Data Source:' dropdown list of 'GridView Tasks' to attach a data source in GridView control.
  • 10. GridViewUsingWizardVB: Adding AccessDataSource in GridView-1 1. Select "Access Database" and then click "OK" button. 2. Click on "Browse" button to browse for database file
  • 11. 1. Double click on App_Data and select the database and click "OK" button Now follow the instructions to select the database GridViewUsingWizardVB: Adding AccessDataSource in GridView-2 2. Click "Next"
  • 12. GridViewUsingWizardVB: Adding AccessDataSource in GridView-3 1. Select the table which you want to access from the database from DropDown list If you have applied any query on the tables you can also select that query from the DropDownList. Select the fields which You want to display and click "Next" button
  • 13. GridViewUsingWizardVB: Adding AccessDataSource in GridView-4 1. First Click "Test Query" to test the query 2. Then Click "Finish" button Test Query is used to test that whether the data is accessed according to our need or not. Your tested query will show here.
  • 14. GridViewUsingWizardVB: View Of AccessDataSource 'AccessDataSource' control will automatically appear here. Now Run Code By pressing 'F5'
  • 15. GridViewUsingWizardVB: Output Output on browser Content of the ItemMaster table will display in GridView
  • 16. GridViewUsingWizardVB: Source Code Explanation -1 "AutoGenerateColumn" indicating whether bound fields are automatically created for each field This source code describes your attached database and select query <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemId" DataSourceID="AccessDataSource1"> <Columns> <asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False" ReadOnly="True" SortExpression="ItemId" /> <asp:BoundField DataField="Item_name" HeaderText="Item_name" SortExpression="Item_name" /> <asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" /> <asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate" SortExpression="Product_Rate" /> </Columns> </asp:GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]"> </asp:AccessDataSource> "DataKeyNames" defines key in the table "DataSourceID" defines data source as "AccessdataSource1"
  • 17. GridViewUsingWizardVB: Source Code Explanation -2 "DataField" Displays name of the field <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemId" DataSourceID="AccessDataSource1"> <Columns> <asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False" ReadOnly="True" SortExpression="ItemId" /> <asp:BoundField DataField="Item_name" HeaderText="Item_name" SortExpression="Item_name" /> <asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" /> <asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate" SortExpression="Product_Rate" /> </Columns> </asp:GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]"> </asp:AccessDataSource> "BoundField" displays the value of specified DataSource field as text "SortExpression" Defines name on which sorting can be performed "HeaderText" shows Header name of the column
  • 18. GridViewUsingWizardVB: Modification trials  Select "All Fields" query instead of "ItemMaster" and observe the difference. You will see that you are getting the data from all the tables.  Instead of selecting ‘*’ while selecting columns select any three column and see the difference. Now you can observe from the result that only selected fields are showing.  Change the Header Text of column "Item_name" from "Item_name" to "Product Name" and see the difference after running the page. You will see that Header of column is changed.
  • 19. Data Binding Using GridView control through VB Code Data binding provides a simple and consistent way for applications to present and interact with data. The GridView control enables you to connect to a data source and display data in tabular format.
  • 20. GridViewUsingCSCode: .aspx File Creation 1. Drag and Drop a 'Button' control in div from Standard Toolbox • Follow Standard Website Creation Steps and set your path to "C:Learner<student-id>DataBindingGridViewUsingVBCodeVB" • Add New Default.aspx page in your Website 2. Introduce a new line and Drag and Drop a 'GridView' control from Data Toolbox
  • 21. •Add the 'App_Data' folder •To add the database, follow the path "C:Learner<Student- Id>DatabaseSale". GridViewUsingCSCode: Adding Database 1. Add 'App_Data' folder 2. And Database 'Sale.mdb' in App_Data like previous exercise.
  • 22. GridViewUsingVBCode: Copy/Paste Code Dim dbconn As OleDbConnection Dim sql As String Dim dbcomm As OleDbCommand Dim dbread As OleDbDataReader Copy/Paste or type this code under the "Partial Class Default" as shown in the template Imports System.Data Imports System.Data.OleDb Type this code above "Partial Class Default" Go to 'default.aspx.vb' by double clicking on blank space of 'default.aspx'
  • 23. GridViewUsingVBCode: Button1_Click Event handler Code dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("~/App_Data/sale.mdb")) dbconn.Open() sql = "SELECT * FROM ItemMaster" dbcomm = New OleDbCommand(sql, dbconn) dbread = dbcomm.ExecuteReader() GridView1.DataSource = dbread GridView1.DataBind() dbread.Close() dbconn.Close() Copy/Paste or type the code under "Button1_Click Event" and run this code with "F5" • Generate the Button1_Click Event in the VB code by double-clicking on the Button control of .aspx file.
  • 24. GridViewUsingVBCode: Run GridView Page First click the button Result will show here. When you will run your page then after clicking button you will get the output
  • 25. Imports System.Data Imports System.Data.OleDb Partial Class _Default Inherits System.Web.UI.Page Dim dbconn As OleDbConnection Dim sql As String Dim dbcomm As OleDbCommand Dim dbread As OleDbDataReader GridViewUsingVBCode: Code Explanation-1 Imports System.Data.OleDb provide OleDb.Net provider for MS Access database connectivity This line declare OleDbConnecton class type variable which is used to make a connection with MS Access Database Imports System.Data Allows to use the DataSet Class Methods. This line declare OleDbDataReader class type variable which is used to read the Data. This line declare OleDbCommand class type variable which is used to run SQL queries.
  • 26. GridViewUsingVBCode: Code Explanation-2 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;dat a source=" & Server.MapPath("~/App_Data/sale.mdb")) dbconn.Open() sql = "SELECT * FROM ItemMaster" dbcomm = New OleDbCommand(sql, dbconn) dbread = dbcomm.ExecuteReader() GridView1.DataSource = dbread GridView1.DataBind() dbconn.Close() End Sub This statement creates an object of OleDbConnection class. This is a path of Database. This statement initialize the SQl query in sql String type variable, which is used in next line Define the DataSource ID of GridView equal to DataReader This string type argument is passed, when we create a connection with MS Access This statement open the OleDbConnection This statement creates an object of OleDbConnection class. This need two arguments, 1- SQL Query, 2- OleDbConnection class object This statement execute the SQl query and store its result in OleDbDataReader class object This statement Close the OleDbConnection
  • 27. GridViewUsingVBCode: Modification Trials-1  In SELECT statement, replace '*' sign with 'ItemId, Item_name' Run and watch the effect Now you can observe that the result displays only the selected fields.  Add 'As Product_Name' just after the 'Item_name' SELECT statement. Syntax: <Field Name> As <New Name> You will see that Header of column will change when you will run the page.
  • 28. GridViewUsingVBCode: Modification Trials-2  Click on 'Auto Format…' link in 'GridView Task' and select the 'scheme' equal to 'Black & Blue 2' Run and watch the effect
  • 29. GridViewUsingVBCode: Error Trials  Remove the Namespace files which we have inserted in the beginning. You will get the error as given below BC30002: Type 'OleDbConnection' is not defined.  Instead of 'Dim dbconn As OleDbConnection' line type Dim dbconn and observe the error showing at ToolTip So it is necessary to Define the variable with As clause.  Remove the line dbconn.open() and see the result after running the page. The error will be shown that is given below. ExecuteReader requires an open and available Connection. The connection's current state is closed. To remove this error, connection should be open.
  • 30. GridViewUsingVBCode: Practice Exercise Create a website named Expenses Add the Database 'Monthly Expenses' Show column "CatId" and "Description" from table "Expense Category" and "Date" and "Detail" from table "Expenses" in a Grid view using wizard option. Show column "CatId" and "Description" from the "Expense Category" table and "Date" and "Detail" from the "Expenses" table in a Grid view using code.
  • 31. GridViewUsingVBCode: Learning Summary Review Use of GridView control Adding database to App_Data. Linking of MS Access database with Grid View through wizard. Linking of MS Access database with Grid View through Code. Use of OleDb classes.
  • 33. Ask and guide me at sunmitraeducation@gmail.com Share this information with as many people as possible. Keep visiting www.sunmitra.com for programme updates.