0% found this document useful (0 votes)
61 views

Insert - Update - Delete in Gridview

The document discusses how to insert, update, delete data in a GridView control in ASP.NET using SQLConnection and SQLDataAdapter. It involves designing a form with a GridView, connecting the GridView to a SQL database, enabling editing functions on the GridView, and writing code to handle inserting, updating, deleting records by executing SQL queries and rebinding the GridView. The code sample provided shows methods for filling the GridView, handling GridView events for editing, updating, deleting, and clearing controls.

Uploaded by

Jemi Moradiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Insert - Update - Delete in Gridview

The document discusses how to insert, update, delete data in a GridView control in ASP.NET using SQLConnection and SQLDataAdapter. It involves designing a form with a GridView, connecting the GridView to a SQL database, enabling editing functions on the GridView, and writing code to handle inserting, updating, deleting records by executing SQL queries and rebinding the GridView. The code sample provided shows methods for filling the GridView, handling GridView events for editing, updating, deleting, and clearing controls.

Uploaded by

Jemi Moradiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Insert Update Delete data in Gridview asp.

net using SqlDataAdapter


method:
we will learn insert edit update and delete data in gridview control
in asp.net using SQLConnection and SQLDataAdapter method. For
understand edit update delete gridview example, First we insert
records in to database and select records from database and bind it to
gridview control, then update and delete record in gridview using
sqlDataAdapter method.

Design the form given below:

Now, finishing design make connection between sqldatabase and asp.net


web application. In this asp.net gridview example we use
sqlconnection and SqlDataAdapter method for connectivity. In this
method we write sql query in our code behind page .

Faculty Name: Nayna N Mistry (M.C.A, NET, GSET)


Sutex bank college of computer applications and science Page 1
Now, set gridview property shows like below.
AutoGenerateDeleteButton=”True”
AutoGenerateEditButton=”True”
DataKeyNames=”ID”
AllowPaging="True"

Enable Gridview events for Edit update delete records in gridview.


GridView1_RowCancelingEdit
GridView1_RowDeleting
GridView1_RowEditing
GridView1_RowUpdating
GridView1_PageIndexChanging

Faculty Name: Nayna N Mistry (M.C.A, NET, GSET)


Sutex bank college of computer applications and science Page 2
Coding:
Imports System.Data
Imports System.Data.SqlClient
Partial Class frminsert_update_delete
Inherits System.Web.UI.Page
Dim constring As String =
ConfigurationManager.ConnectionStrings("constring").ToString()
Dim cnn As New SqlConnection(constring)
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim dt As DataTable
Dim strquery As String = ""

Sub fillgrid()
strquery = "select * from tblusermst"
da = New SqlDataAdapter(strquery, cnn)
dt = New DataTable
da.Fill(dt)
grvuser.DataSource = dt
grvuser.DataBind()

End Sub
Sub clearcontrol()
txtusername.Text = ""
txtsurname.Text = ""
txtcity.Text = ""
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If IsPostBack = False Then
fillgrid()
End If
End Sub

Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As


System.EventArgs) Handles btnsave.Click
Try
strquery = "insert into tblusermst values ('" &
txtusername.Text & "','" & txtsurname.Text & "','" & txtcity.Text &
"')"
da = New SqlDataAdapter(strquery, cnn)
dt = New DataTable
da.Fill(dt)
Faculty Name: Nayna N Mistry (M.C.A, NET, GSET)
Sutex bank college of computer applications and science Page 3
fillgrid()
Catch ex As Exception
lblmsg.Text = ex.Message
End Try
clearcontrol()
End Sub

Protected Sub grvuser_PageIndexChanging(ByVal sender As Object,


ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles
grvuser.PageIndexChanging
grvuser.PageIndex = e.NewPageIndex
fillgrid()
End Sub

Protected Sub grvuser_RowCancelingEdit(ByVal sender As Object,


ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs)
Handles grvuser.RowCancelingEdit
grvuser.EditIndex = -1
fillgrid()
End Sub

Protected Sub grvuser_RowDeleting(ByVal sender As Object, ByVal e


As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles
grvuser.RowDeleting
Try
Dim vid As Integer = grvuser.DataKeys(e.RowIndex).Value
strquery = "delete from tblusermst where id= " & vid
da = New SqlDataAdapter(strquery, cnn)
dt = New DataTable
da.Fill(dt)
fillgrid()
Catch ex As Exception
lblmsg.Text = ex.Message
End Try
cnn.Close()
End Sub

Protected Sub grvuser_RowEditing(ByVal sender As Object, ByVal e


As System.Web.UI.WebControls.GridViewEditEventArgs) Handles
grvuser.RowEditing
grvuser.EditIndex = e.NewEditIndex
fillgrid()
End Sub

Faculty Name: Nayna N Mistry (M.C.A, NET, GSET)


Sutex bank college of computer applications and science Page 4
Protected Sub grvuser_RowUpdating(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
grvuser.RowUpdating
Try
Dim vtxtusername, vtxtsurname, vtxtcity As New TextBox
Dim vid As Integer = grvuser.DataKeys(e.RowIndex).Value
vtxtusername =
grvuser.Rows(e.RowIndex).Cells(2).Controls(0)
vtxtsurname =
grvuser.Rows(e.RowIndex).Cells(3).Controls(0)
vtxtcity = grvuser.Rows(e.RowIndex).Cells(4).Controls(0)
strquery = "update tblusermst set name='" &
vtxtusername.Text & "',surname='" & vtxtsurname.Text & "',city='" &
vtxtcity.Text & "' where id= " & vid
da = New SqlDataAdapter(strquery, cnn)
dt = New DataTable
da.Fill(dt)
fillgrid()
Catch ex As Exception
lblmsg.Text = ex.Message
End Try

End Sub
End Class

Faculty Name: Nayna N Mistry (M.C.A, NET, GSET)


Sutex bank college of computer applications and science Page 5

You might also like