Perform Insert,Update,Delete Using Type Dataset

Public Class frmTypeDataset

    Dim dtableEmp As New DataSet1.empDataTable
    Dim adp As New DataSet1TableAdapters.empTableAdapter()

    Dim flgAdd As Boolean
    Dim rec As Int32
    Dim o_eno, o_salary As Int32
    Dim o_ename, o_skill, o_address As String

    Private Sub frmTypeDataset_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        adp.ClearBeforeFill = True
        adp.Fill(dtableEmp)

        DataGridView1.DataSource = dtableEmp

        ''fill()
        rec = 0
        display(rec)
        enableButton(True, False, False, True, True, True, True)
    End Sub
    Sub fill()
        adp.Fill(dtableEmp)
        DataGridView1.DataSource = dtableEmp
    End Sub
    Sub clearTextbox()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
    End Sub
    Sub display(ByVal r As Int32)
        TextBox1.Text = dtableEmp.Rows(r).Item("Eno")
        TextBox2.Text = dtableEmp.Rows(r).Item("ename")
        TextBox3.Text = dtableEmp.Rows(r).Item("skill")
        TextBox4.Text = dtableEmp.Rows(r).Item("salary")
        TextBox5.Text = dtableEmp.Rows(r).Item("address")
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        flgAdd = True
        enableButton(False, True, True, False, False, False, False)
        clearTextbox()
    End Sub
    Sub enableButton(ByVal badd As Boolean, ByVal bsave As Boolean, ByVal bcan As Boolean, ByVal bupdate As Boolean, ByVal bfind As Boolean, ByVal bnavigation As Boolean, ByVal bdel As Boolean)
        btnAdd.Enabled = badd
        btnCancel.Enabled = bcan
        btnUpdate.Enabled = bupdate
        btnFind.Enabled = bfind
        btnSave.Enabled = bsave
        btnfirst.Enabled = bnavigation
        btnlast.Enabled = bnavigation
        btnNext.Enabled = bnavigation
        btnprev.Enabled = bnavigation
        btndelete.Enabled = bdel
    End Sub
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        flgAdd = False
        enableButton(False, True, True, False, False, False, False)
        o_eno = CInt(TextBox1.Text)
        o_ename = TextBox2.Text
        o_skill = TextBox3.Text
        o_salary = CInt(TextBox4.Text)
        o_address = TextBox5.Text
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (flgAdd = True) Then
            If (MsgBox("sure u want to save ?", MsgBoxStyle.YesNo, "?") = MsgBoxResult.Yes) Then
                adp.Insert(CInt(TextBox1.Text), TextBox2.Text, TextBox3.Text, CInt(TextBox4.Text), TextBox5.Text)

            End If
        Else
            If (MsgBox("sure u want to update ?", MsgBoxStyle.YesNo, "?") = MsgBoxResult.Yes) Then
                adp.Update(CInt(TextBox1.Text), TextBox2.Text, TextBox3.Text, CInt(TextBox4.Text), TextBox5.Text, o_eno, o_ename, o_skill, o_salary, o_address)
            End If
        End If
        enableButton(True, False, False, True, True, True, True)
        fill()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        rec = 0
        display(rec)
        enableButton(True, False, False, True, True, True, True)
    End Sub

    Private Sub btnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfirst.Click
        rec = 0
        display(rec)
    End Sub

    Private Sub btnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlast.Click
        rec = dtableEmp.Rows.Count - 1
        display(rec)
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        rec = rec + 1
        If (rec >= dtableEmp.Rows.Count) Then
            rec = dtableEmp.Rows.Count - 1
        End If
        display(rec)
    End Sub

    Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click
        rec = rec - 1
        If (rec < 0) Then
            rec = 0
        End If
        display(rec)
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        If (MsgBox("sure u want to delete ?", MsgBoxStyle.YesNo, "?") = MsgBoxResult.Yes) Then
            adp.Delete(CInt(TextBox1.Text), TextBox2.Text, TextBox3.Text, CInt(TextBox4.Text), TextBox5.Text)
        End If
        fill()
        rec = 0
        display(rec)
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        rec = e.RowIndex
        display(rec)
    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim eno As String = InputBox("enter eno:")
        If (Not String.IsNullOrEmpty(eno)) Then
            Dim dre As DataSet1.empRow = dtableEmp.FindByENO(eno)
            If (dre Is Nothing) Then
                MsgBox("not found")
            Else
                TextBox1.Text = dre.ENO
                TextBox2.Text = dre.ename
                TextBox3.Text = dre.skill
                TextBox4.Text = dre.salary
                TextBox5.Text = dre.address
            End If
        End If
    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub
End Class


Average Rating (1)
by Firdosh Jahan   Memon  in VB.Net  on 3/4/2015 2:39:20 AM  


Post Your Comment
*


Nice coding.......
by   sharukh   on  3/16/2015 11:31:42 PM