1 Jun 2014

Penggunaan Dataset dengan VB.Net

11:33 AM

Akhirnya Semester ini selese juga, Bener-bener butuh perjuangan keras. ..tiap hari tugas, tugas dan tugas. . .di buat tidur hanya 3 jam per hari, bahkan bisa melekan hingga 2 hari. . Gila !! ohya. .aku ada tugas VB.NET lagi ne, tentang dataset yang virtual dan yang dengan database. virtual itu maksudnya tidak menggunakan database jadi semua data-data yang kita inputkan pada saat program di running akan tersimpan pada memori komputer, dan jika di close tidak akan ada data yang tersimpan. tentu saja.!! Orang gak ada databasenya koc! 
             Sedangkan penggunaan dataset yang ada databasenya pada saat program dijalankan dataadapter akan mengambil struktur database kemudian akan disimpan di Dataset yang telah kita sediakan(Apikasi terputus). Sehingga semua perubahan yang dilakukan akan disimpan di dataset, kemudian saat aplikasi akan di close kita bisa menentukan apakah perubahan-perubahan/ manipulasi data akan disimpan ke database atau tidak.
1. Virtual
Public Class Form2
    Dim xDs As New DataSet
    Dim xDt As New DataTable("mhs")
    Dim update1 As Boolean = False

    Private Sub Tnim_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tnim.TextChanged
        Dim txt1 As String = tnim.Text
        If IsNumeric(Microsoft.VisualBasic.Right(txt1, 1)) Then
        Else
            If tnim.Text.Length = 0 Then
            Else
                tnim.Text = Microsoft.VisualBasic.Left(txt1, txt1.Length - 1)
            End If
        End If
        tnim.SelectionStart = tnim.Text.Length
    End Sub

    Sub isicombo()
        With ComboBox1.Items
            .Add("S1")
            .Add("D3")
            .Add("D4")
        End With

        With ComboBox3.Items
            .Add("Baru")
            .Add("Transfer")
        End With

        With ComboBox4.Items
            .Add("Aktif")
            .Add("Cuti")
            .Add("Lulus")
        End With
        btambah.Enabled = False
        bedit.Enabled = False
        bhapus.Enabled = False
    End Sub

    Private Sub ComboBox1_LostFocus(ByVal sender As ObjectByVal e As System.EventArgs) Handles ComboBox1.LostFocus

        ComboBox2.Items.Clear()
        If ComboBox1.Text = "S1" Then
            With ComboBox2.Items
                .Add("Sistem Informasi")
                .Add("Sistem Komputer")
                .Add("Desain Komunikasi Visual")
            End With
        ElseIf ComboBox1.Text = "D3" Then
            With ComboBox2.Items
                .Add("Manajemen Informatika")
                .Add("Sekretaris")
                .Add("Multi Media")
            End With
        ElseIf ComboBox1.Text = "D4" Then
            With ComboBox2.Items
                .Add("Multimedia")
            End With
        End If
    End Sub
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        isicombo()
        xDs.Tables.Add(xDt)

        xDt.Columns.Add("Nim")
        xDt.Columns.Add("Nama")
        xDt.Columns.Add("Alamat")
        xDt.Columns.Add("Telp")
        xDt.Columns.Add("Tmp_lhr")
        xDt.Columns.Add("Tgl_lhr")
        xDt.Columns.Add("Jurusan")
        xDt.Columns.Add("Prodi")
        xDt.Columns.Add("Stt_msk")
        xDt.Columns.Add("Stt_mhs")

        xDt.PrimaryKey = New DataColumn() {xDt.Columns("Nim")}

        DataGridView1.DataSource = xDs.Tables("mhs")
    End Sub

    Sub kunci()
        Dim xRow As DataRow = xDs.Tables("mhs").Rows.Find(DataGridView1.CurrentRow.Cells(0).Value)

        If Not xRow Is Nothing Then
            DataGridView1.Enabled = True
            btambah.Enabled = True
            bsimpan.Enabled = False
            bbatal.Enabled = False
            btutup.Enabled = True
            bedit.Enabled = False
            bhapus.Enabled = False
            tnim.Enabled = False
            TextBox2.Enabled = False
            TextBox3.Enabled = False
            TextBox4.Enabled = False
            TextBox5.Enabled = False
            DateTimePicker1.Enabled = False
            ComboBox1.Enabled = False
            ComboBox2.Enabled = False
            ComboBox3.Enabled = False
            ComboBox4.Enabled = False
        Else
            btambah.Enabled = False
            bsimpan.Enabled = True
            bbatal.Enabled = True
            btutup.Enabled = True
            bedit.Enabled = False
            bhapus.Enabled = False
            'GroupBox3.Enabled = False
            tnim.Enabled = True
            TextBox2.Enabled = True
            TextBox3.Enabled = True
            TextBox4.Enabled = True
            TextBox5.Enabled = True
            DateTimePicker1.Enabled = True
            ComboBox1.Enabled = True
            ComboBox2.Enabled = True
            ComboBox3.Enabled = True
            ComboBox4.Enabled = True
        End If
    End Sub

    Private Sub bsimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsimpan.Click
        If update1 = False Then
            Dim xRow As DataRow = xDs.Tables("mhs").NewRow

            xRow(0) = tnim.Text
            xRow(1) = TextBox2.Text
            xRow(2) = TextBox3.Text
            xRow(3) = TextBox4.Text
            xRow(4) = TextBox5.Text
            xRow(5) = DateTimePicker1.Value
            xRow(6) = ComboBox1.Text
            xRow(7) = ComboBox2.Text
            xRow(8) = ComboBox3.Text
            xRow(9) = ComboBox4.Text

            xDs.Tables("mhs").Rows.Add(xRow)

            bbatal_Click(sender, e)

            DataGridView1.DataSource = xDs.Tables("mhs")
            kunci()
            MsgBox("Data Tersimpan")

        Else

            Dim xRow As DataRow = xDs.Tables("mhs").Rows.Find(tnim.Text)

            If Not xRow Is Nothing Then
                xRow.BeginEdit()
                xRow(1) = TextBox2.Text
                xRow(2) = TextBox3.Text
                xRow(3) = TextBox4.Text
                xRow(4) = TextBox5.Text
                xRow(5) = DateTimePicker1.Value
                xRow(6) = ComboBox1.Text
                xRow(7) = ComboBox2.Text
                xRow(8) = ComboBox3.Text
                xRow(9) = ComboBox4.Text

                xRow.EndEdit()

                bbatal_Click(sender, e)
                kunci()
                tnim.Enabled = True

                DataGridView1.DataSource = xDs.Tables("mhs")

                MsgBox("Data Terupdate")
            End If
        End If

    End Sub

    Private Sub bedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bedit.Click
        update1 = True
        tnim.Text = DataGridView1.CurrentRow.Cells(0).Value
        TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value
        TextBox3.Text = DataGridView1.CurrentRow.Cells(2).Value
        TextBox4.Text = DataGridView1.CurrentRow.Cells(3).Value
        TextBox5.Text = DataGridView1.CurrentRow.Cells(4).Value
        DateTimePicker1.Text = DataGridView1.CurrentRow.Cells(5).Value
        ComboBox1.Text = DataGridView1.CurrentRow.Cells(6).Value
        ComboBox2.Text = DataGridView1.CurrentRow.Cells(7).Value
        ComboBox3.Text = DataGridView1.CurrentRow.Cells(8).Value
        ComboBox4.Text = DataGridView1.CurrentRow.Cells(9).Value
        btambah_Click(sender, e)
        tnim.Enabled = False
    End Sub

    Private Sub bhapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bhapus.Click
        Dim xRow As DataRow = xDs.Tables("mhs").Rows.Find(DataGridView1.CurrentRow.Cells(0).Value)

        If Not xRow Is Nothing Then
            xRow.Delete()

            DataGridView1.DataSource = xDs.Tables("mhs")

            MsgBox("Data Terhapus")
        End If
    End Sub

    Private Sub btambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btambah.Click
        btambah.Enabled = False
        bsimpan.Enabled = True
        bbatal.Enabled = True
        btutup.Enabled = True
        tnim.Enabled = True
        TextBox2.Enabled = True
        TextBox3.Enabled = True
        TextBox4.Enabled = True
        TextBox5.Enabled = True
        DateTimePicker1.Enabled = True
        ComboBox1.Enabled = True
        ComboBox2.Enabled = True
        ComboBox3.Enabled = True
        ComboBox4.Enabled = True
        DataGridView1.Enabled = False
    End Sub

    Private Sub bbatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bbatal.Click
        tnim.Text = ""
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        ComboBox3.Text = ""
        ComboBox4.Text = ""
        kunci()
    End Sub

    Private Sub DataGridView1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles DataGridView1.Click
        bedit.Enabled = True
        bhapus.Enabled = True
    End Sub

    Private Sub btutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btutup.Click
        Me.Close()
    End Sub
End Class
2. Dengan Database
Imports System.Data.SqlClient
Public Class Form3
    Dim xDs As New DataSet
    Dim xDt As New DataTable("mhs")
    Dim xCn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\t07.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
    Dim SqlCommandBuil As New SqlCommandBuilder
    Dim xDa As New SqlDataAdapter
    Dim update1 As Boolean = False

    Private Sub Tnim_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tnim.TextChanged
        Dim txt1 As String = tnim.Text
        If IsNumeric(Microsoft.VisualBasic.Right(txt1, 1)) Then
        Else
            If tnim.Text.Length = 0 Then
            Else
                tnim.Text = Microsoft.VisualBasic.Left(txt1, txt1.Length - 1)
            End If
        End If
        tnim.SelectionStart = tnim.Text.Length
    End Sub
    Sub isicombo()
        With ComboBox1.Items
            .Add("S1")
            .Add("D3")
            .Add("D4")
        End With

        With ComboBox3.Items
            .Add("Baru")
            .Add("Transfer")
        End With

        With ComboBox4.Items
            .Add("Aktif")
            .Add("Cuti")
            .Add("Lulus")
        End With
        btambah.Enabled = False
        bedit.Enabled = False
        bhapus.Enabled = False
    End Sub


    Private Sub ComboBox1_LostFocus(ByVal sender As ObjectByVal e As System.EventArgs) Handles ComboBox1.LostFocus

        ComboBox2.Items.Clear()
        If ComboBox1.Text = "S1" Then
            With ComboBox2.Items
                .Add("Sistem Informasi")
                .Add("Sistem Komputer")
                .Add("Desain Komunikasi Visual")
            End With
        ElseIf ComboBox1.Text = "D3" Then
            With ComboBox2.Items
                .Add("Manajemen Informatika")
                .Add("Sekretaris")
                .Add("Multi Media")
            End With
        ElseIf ComboBox1.Text = "D4" Then
            With ComboBox2.Items
                .Add("Multimedia")
            End With
        End If
    End Sub

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

        isicombo()

        xDa = New SqlDataAdapter("SELECT * FROM mhs", xCn)
        xDt.PrimaryKey = New DataColumn() {xDt.Columns("nim")}
        xDa.MissingSchemaAction = MissingSchemaAction.AddWithKey
        xDa.Fill(xDs, "mhs")
        DataGridView1.DataSource = xDs.Tables("mhs")

    End Sub

    Sub kunci()

        Dim xRow As DataRow = xDs.Tables("mhs").NewRow
        If Not xRow Is Nothing Then
            DataGridView1.Enabled = True
            btambah.Enabled = True
            bsimpan.Enabled = False
            bbatal.Enabled = False
            btutup.Enabled = True
            bedit.Enabled = False
            bhapus.Enabled = False
            tnim.Enabled = False
            TextBox2.Enabled = False
            TextBox3.Enabled = False
            TextBox4.Enabled = False
            TextBox5.Enabled = False
            DateTimePicker1.Enabled = False
            ComboBox1.Enabled = False
            ComboBox2.Enabled = False
            ComboBox3.Enabled = False
            ComboBox4.Enabled = False
        Else
            btambah.Enabled = False
            bsimpan.Enabled = True
            bbatal.Enabled = True
            btutup.Enabled = True
            bedit.Enabled = False
            bhapus.Enabled = False
            'GroupBox3.Enabled = False
            tnim.Enabled = True
            TextBox2.Enabled = True
            TextBox3.Enabled = True
            TextBox4.Enabled = True
            TextBox5.Enabled = True
            DateTimePicker1.Enabled = True
            ComboBox1.Enabled = True
            ComboBox2.Enabled = True
            ComboBox3.Enabled = True
            ComboBox4.Enabled = True
        End If
    End Sub

    Private Sub bsimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsimpan.Click
        If update1 = False Then
            Dim xRow As DataRow = xDs.Tables("mhs").NewRow

            xRow(0) = tnim.Text
            xRow(1) = TextBox2.Text
            xRow(2) = TextBox3.Text
            xRow(3) = TextBox4.Text
            xRow(4) = TextBox5.Text
            xRow(5) = DateTimePicker1.Value
            xRow(6) = ComboBox1.Text
            xRow(7) = ComboBox2.Text
            xRow(8) = ComboBox3.Text
            xRow(9) = ComboBox4.Text

            xDs.Tables("mhs").Rows.Add(xRow)

            bbatal_Click(sender, e)

            DataGridView1.DataSource = xDs.Tables("mhs")
            kunci()
            MsgBox("Data Tersimpan")

        Else

            Dim xRow As DataRow = xDs.Tables("mhs").Rows.Find(tnim.Text)

            If Not xRow Is Nothing Then
                xRow.BeginEdit()
                xRow(1) = TextBox2.Text
                xRow(2) = TextBox3.Text
                xRow(3) = TextBox4.Text
                xRow(4) = TextBox5.Text
                xRow(5) = DateTimePicker1.Value
                xRow(6) = ComboBox1.Text
                xRow(7) = ComboBox2.Text
                xRow(8) = ComboBox3.Text
                xRow(9) = ComboBox4.Text
                xRow.EndEdit()
                bbatal_Click(sender, e)
                kunci()
                tnim.Enabled = True

                DataGridView1.DataSource = xDs.Tables("mhs")

                MsgBox("Data Terupdate")
            End If
        End If

    End Sub

    Private Sub bedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bedit.Click
        update1 = True
        tnim.Text = DataGridView1.CurrentRow.Cells(0).Value
        TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value
        TextBox3.Text = DataGridView1.CurrentRow.Cells(2).Value
        TextBox4.Text = DataGridView1.CurrentRow.Cells(3).Value
        TextBox5.Text = DataGridView1.CurrentRow.Cells(4).Value
        DateTimePicker1.Text = DataGridView1.CurrentRow.Cells(5).Value
        ComboBox1.Text = DataGridView1.CurrentRow.Cells(6).Value
        ComboBox2.Text = DataGridView1.CurrentRow.Cells(7).Value
        ComboBox3.Text = DataGridView1.CurrentRow.Cells(8).Value
        ComboBox4.Text = DataGridView1.CurrentRow.Cells(9).Value
        btambah_Click(sender, e)
        tnim.Enabled = False
    End Sub

    Private Sub bhapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bhapus.Click
        Dim xRow As DataRow = xDs.Tables("mhs").Rows.Find(DataGridView1.CurrentRow.Cells(0).Value)

        If Not xRow Is Nothing Then
            xRow.Delete()

            DataGridView1.DataSource = xDs.Tables("mhs")

            MsgBox("Data Terhapus")
        End If
    End Sub

    Private Sub btambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btambah.Click
        btambah.Enabled = False
        bsimpan.Enabled = True
        bbatal.Enabled = True
        btutup.Enabled = True
        tnim.Enabled = True
        TextBox2.Enabled = True
        TextBox3.Enabled = True
        TextBox4.Enabled = True
        TextBox5.Enabled = True
        DateTimePicker1.Enabled = True
        ComboBox1.Enabled = True
        ComboBox2.Enabled = True
        ComboBox3.Enabled = True
        ComboBox4.Enabled = True
        DataGridView1.Enabled = False
    End Sub

    Private Sub bbatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bbatal.Click
        tnim.Text = ""
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        ComboBox3.Text = ""
        ComboBox4.Text = ""
        kunci()
    End Sub

    Private Sub DataGridView1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles DataGridView1.Click
        bedit.Enabled = True
        bhapus.Enabled = True
    End Sub

    Private Sub btutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btutup.Click

        MessageBox.Show("Apakah Perubahan Data Akan disimpan " & vbCrLf & "Ke Database ?""Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
        If MsgBoxResult.Yes Then
            'Dim SqlCommandBuil As New SqlCommandBuilder(xDa)
            xDa.Fill(xDs, "mhs")
            SqlCommandBuil = New SqlCommandBuilder(xDa)
            xDa.Update(xDs, "mhs")
            xDa.Update(xDs.Tables("mhs").Select(NothingNothing, DataViewRowState.Deleted))
            xDa.Update(xDs.Tables("mhs").Select(NothingNothing, DataViewRowState.ModifiedCurrent))
            xDa.Update(xDs.Tables("mhs").Select(NothingNothing, DataViewRowState.Added))

            'MsgBox("Data Tersimpan ke Database!")
            Me.Close()
        Else
            Me.Close()
        End If
    End Sub
End Class
Project lengkannya dapat di download di bawah ini. . semoga bermanfaat...! -_-'  

Written by

Bukan seorang pakar ataupun praktisi profesional, hanya ingin berbagai atas esensi realita perjalanan dan mimpi yang terlihat tak bertepi.

0 komentar:

Posting Komentar

 

© 2014 Dipzt. All rights resevered. Designed by Templateism

Back To Top