VB.net Tutorials with Examples

VB.Net Projects

VB.Net Project 1

adplus-dvertising
Simple Select, Edit, Update and Delete in Asp.Net GridView
Previous Home Next

.ASPX CODE

<%@ Page 
Language=
"VB" AutoEventWireup=
"true"  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>Untitled Page</title>
</head>
<body>
<form id=
"form1" runat=
"server">
<div>
<asp:GridView
 ID="GridView1"
  runat=
"server" AllowPaging="True" 
AutoGenerateColumns=
"False" BackColor=
"#DEBA84" BorderColor="#DEBA84" 
BorderStyle=
"None" BorderWidth=
"1px" CellPadding="3" 
CellSpacing="2" 
DataKeyNames=
"item_id" onrowcancelingedit=
"GridView1_RowCancelingEdit" 
onrowcommand=
"GridView1_RowCommand" 
onrowdeleting="GridView1_RowDeleting" 
onrowediting=
"GridView1_RowEditing" 
onrowupdating="GridView1_RowUpdating" 
onselectedindexchanged=
"GridView1_SelectedIndexChanged">
<FooterStyle BackColor=
"#F7DFB5" ForeColor=
"#8C4510" />
<RowStyle 
BackColor="#FFF7E7"
 ForeColor=
"#8C4510" />
<Columns>
<asp:
BoundField DataField=
"item_id" HeaderText=
"ITEM" />
<asp:BoundField 
DataField="purchase1"
 HeaderText=
 "name" />
<asp:BoundField 
DataField="tax" 
HeaderText=
"tax" />
<asp:
CommandField ShowEditButton=
"True" />
<asp:
CommandField ShowSelectButton=
"True" />
<asp:
CommandField ShowDeleteButton=
"True" />
</Columns>
<PagerStyle ForeColor=
"#8C4510" HorizontalAlign=
"Center" />
<SelectedRowStyle BackColor=&
quot;#738A9C" Font-
Bold=
"True" ForeColor=
"White" />
<HeaderStyle BackColor=
"#A55129" Font
-Bold=
"True" ForeColor=
"White" />
<AlternatingRowStyle BorderColor=
"#FF99FF" />
</asp:GridView>
</div>
</form>
</body>
</html>

.VB CODE

Imports System.Data
Imports System.Data.OleDb
Partial Class _Default
Inherits System
.Web.UI
.Page
Public Sub bindgrid()
Dim con As OleDbConnection
Dim da As OleDbDataAdapter
con = New OleDbConnection(
"provider=microsoft.jet.oledb.4.0;Data source=D:\invent.mdb")
da = New OleDbDataAdapter(
"select item_id, purchase1,tax from purchase", con)
Dim ds As New DataSet()
da.Fill(ds, "purchase")
GridView1.DataSource = ds.Tables(
"purchase")
GridView1.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As 
Object, ByVal e As System
.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
bindgrid()
End If
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As 
Object, 
	ByVal e As System
	.Web
	.U
	I.WebControls
	.GridViewEditEventArgs
	) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
bindgrid()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As 
Object, 
	ByVal e As System
	.Web
	.UI.
	WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim UserID As String
 = GridView1.DataKeys(e.RowIndex).Value.ToString()
Dim l As String 
= DirectCast(GridView1.Rows(e.RowIndex).Cells(1).Controls
(0),
 TextBox).Text
Dim k As String
 = DirectCast(GridView1.Rows(e.RowIndex).Cells(2).Controls
  (0),
   TextBox
  ).Text
Dim can As New OleDbConnection(
"provider=microsoft.jet.oledb.4.0;Data source=D:\invent.mdb")
can.Open()
Dim com1 As New OleDbCommand(
"update purchase set purchase1='" 
	& l & 
	"',tax='" & k & 
	"' where item_id='" 
	& UserID & 
	"'", can)
com1.ExecuteNonQuery()
can.Close()
'to go back to the previous position
GridView1.EditIndex = -1
' // fetch and rebind the data.
bindgrid()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As 
Object, 
	ByVal e As System
	.Web
	.UI
	.WebControls
	.
	GridViewCancelEditEventArgs) 
	Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
bindgrid()
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As 
Object, 
	ByVal e As System
	.Web
	.UI.WebControls.GridViewDeleteEventArgs
	) Handles GridView1.RowDeleting
Dim UserID As String
 = GridView1.DataKeys(e.RowIndex).Value.ToString()
Dim can As New OleDbConnection(
"provider=microsoft.jet.oledb.4.0;Datasource=D:\invent.mdb")
can.Open()
Dim com1 As New OleDbCommand(
"delete from purchase where item_id ='" 
& UserID & "'", can)
com1.ExecuteNonQuery()
can.Close()
'to go back to the previous position
GridView1.EditIndex = -1
' // fetch and rebind the data.
bindgrid()
End Sub
End Class
Previous Home Next