Edit Database through Data Grid (ASP.NET PROGRAM)


          Data Grid retrieves data from database and displays it. We can update the database through data grid using controls which are embedded with data grid.
Imports System.Data.OleDb


Namespace DataGrid_Repeater
Partial Class DataGrid
    Inherits System.Web.UI.Page
    Dim ad As oledbDataAdapter
    Dim d As DataSet
    Dim con As oledbConnection
#Region " Web Form Designer Generated Code "
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub
#End Region
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            con = New OleDbConnection("Provider=microsoft.jet.oledb.4.0; data source=C:\Inetpub\wwwroot\Sample-Raghavan\DataGrid-Repeater\bin\datas.mdb")
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Private Sub ButShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButShow.Click
        Try
            Call Dgview()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub DgEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        Call Dgview()
    End Sub
    Sub DgCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = -1
        Call Dgview()
    End Sub
    Sub DgUpdate(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Try
            Dim s1 As TableCell
            Dim s2, s3 As TextBox
            s1 = e.Item.Cells(0)
            s2 = e.Item.Cells(1).Controls(0)
            s3 = e.Item.Cells(2).Controls(0)
            con.Open()
            Dim cmd As New OleDbCommand
            cmd = New OleDbCommand("Update Vote set age=" & Val(s3.Text) & ", sex='" & Trim(s2.Text) & "' where ename='" & s1.Text & "'", con)
            cmd.executenonquery()
            con.Close()
            DataGrid1.EditItemIndex = -1
            Call Dgview()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Sub DgDelete(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Try
            Dim s1 As TableCell
            'Dim s2, s3 As TextBox
            s1 = e.Item.Cells(0)
            's2 = e.Item.Cells(1).Controls(0)
            's3 = e.Item.Cells(2).Controls(0)
            con.Open()
            Dim cmd As New OleDbCommand
            cmd = New OleDbCommand("delete from  Vote  where ename='" & s1.Text & "'", con)
            cmd.ExecuteNonQuery()
            con.Close()
            DataGrid1.EditItemIndex = -1
            Call Dgview()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Sub Dgview()
        ad = New OleDbDataAdapter("Select * from Vote", con)
        d = New DataSet
        ad.Fill(d)
        DataGrid1.DataSource = d
        DataGrid1.DataBind()
    End Sub
    Sub DoPaging(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        Call Dgview()
    End Sub
    Private Sub ButInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButInsert.Click
        Try
            Dim cmd As OleDbCommand
            cmd = New OleDbCommand("Insert into vote values('" & TextBox1.Text & "'," & Val(TextBox2.Text) & ",'" & Trim(TextBox3.Text) & "')", con)
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
            Call Dgview()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
End Class
End Namespace

OUTPUT 
Share:

Total Pageviews

CONTACT US

Prof. Roshan P. Helonde
Mobile: +917276355704
WhatsApp: +917276355704
Email: roshanphelonde@rediffmail.com

Enter Project Title

Popular Projects

All Archive

Contact Form

Name

Email *

Message *