CRUD OPRATION ASP.NET
CRUD Operation in ASP.NET using
VB
·
Program Definition:
The below given program perform CRUD
operation in ASP.NET using VB.
·
Designing Page: database.aspx
·
Scripting page: database.aspx.vb
Imports System.Data.SqlClient
·
To insert the data into database
double click on “insert” button and write the
following code into that part
Protected Sub
btninsert_Click(ByVal sender As Object, ByVal e As EventArgs) Handles
btninsert.Click
Dim connect As
New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\GJ_SO_SMM\Documents\Vis ual Studio
2010\Projects\demo\demo\App_Data\Database1.mdf;Integrated Security=True;User Instance=True")
Dim
productId As Integer = txtproductid.Text Dim name As
String = txtname.Text
Dim
specification As String = txtspeci.Text Dim unit As String
= dropunit.SelectedValue
Dim color As String = radiocolor.SelectedValue Dim pdate
As DateTime = txtdate.Text
Dim quan As
String = txtquan.Text Dim status As String = ""
If chkregular.Checked = True Then
status = "Regular"
Else
status = "Irregular" End If
connect.Open()
Dim dcommand As New SqlCommand("insert into product
values('" & productId &
"','" & name & "','" & specification &
"','" & unit & "','" & color &
"','" & pdate & "','" &
quan & "','"
& status & "')", connect)
dcommand.ExecuteNonQuery()
MsgBox("Successfull
Data Inserted", MsgBoxStyle.Information, "Message") connect.Close()
ListPrroduct() End Sub
·
To show the data GridView is used on for that on
method namely “ListPrroduct ” created and also that method call in “page_load” event.
Private Sub ListPrroduct()
Dim connect As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\GJ_SO_SMM\Documents\Vis ual Studio
2010\Projects\demo\demo\App_Data\Database1.mdf;Integrated Security=True;User Instance=True")
connect.Open()
Dim command
As New SqlCommand("select * from product", connect) Dim sd As New
SqlDataAdapter(command)
Dim dt As
New DataTable sd.Fill(dt) GridView1.DataSource = dt GridView1.DataBind()
End Sub
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
ListPrroduct() End Sub
·
To update the data
write below code on “update”
button’s click event.
Protected Sub
Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles
Button1.Click
Dim connect As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\GJ_SO_SMM\Documents\Vis ual Studio
2010\Projects\demo\demo\App_Data\Database1.mdf;Integrated Security=True;User Instance=True")
Dim
productId As Integer = txtproductid.Text Dim name As
String = txtname.Text
Dim specification As String = txtspeci.Text Dim unit As String =
dropunit.SelectedValue Dim color As
String = radiocolor.SelectedValue Dim pdate As
DateTime = txtdate.Text
Dim quan As
String = txtquan.Text Dim status As String = ""
If chkregular.Checked = True Then
status = "Regular"
Else
status = "Irregular" End If
connect.Open()
Dim dcommand As New SqlCommand("Update product set name =
'" & name & "',specification
='" & specification & "',unit = '" & unit &
"',color = '" & color & "',date
= '" & pdate & "',quantity = '" & quan &
"',status = '" & status & "' where productId
= '" & productId & "'", connect)
dcommand.ExecuteNonQuery()
MsgBox("Successfull
Data Updated", MsgBoxStyle.Information, "Message") connect.Close()
ListPrroduct() End Sub
·
And the last one is for delete the
data. To perform this task write the below code on “delete” button’s
click event.
Protected Sub
Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles
Button2.Click
Dim connect As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\GJ_SO_SMM\Documents\Vis ual Studio
2010\Projects\demo\demo\App_Data\Database1.mdf;Integrated Security=True;User Instance=True")
Dim
productId As Integer = txtproductid.Text connect.Open()
Dim dcommand As New SqlCommand("Delete product where productId
= '" & productId & "'", connect)
dcommand.ExecuteNonQuery()
MsgBox("Successfull
Data Updated", MsgBoxStyle.Information, "Message") connect.Close()
ListPrroduct() End Sub


Comments
Post a Comment