Cara Insert Update Delete file PDF dengan VB.Net

 

  1. Membuat database
  2. Membuat tabel
  3. Membuat project
  4. Membuat module koneksi

 

Imports System.Data.SqlClient

 

Module Module1

 

    Public conn As SqlConnection

    Public da As SqlDataAdapter

    Public ds As DataSet

    Public cmd As SqlCommand

    Public dr As SqlDataReader

 

    Public Sub Koneksi()

        conn = New SqlConnection("data source = .\sqlexpress;initial catalog=mydb;integrated security=true")

        conn.Open()

    End Sub

End Module

 

  1. Membuat form

 

 

  1. Tambahkan komponen adobe pdf reader

 

 

  1. Tambahkan komponen openfiledialog

 

 

 

Coding :

 

 

Imports System.Data.SqlClient

 

Public Class OlahPdf

 

    Sub Kosongkan()

        On Error Resume Next

        TextBox1.Clear()

        TextBox2.Clear()

        TextBox3.Clear()

        TextBox5.Clear()

        TextBox1.Focus()

        PDF.src = (TextBox3.Text)

    End Sub

 

    Sub databaru()

        On Error Resume Next

        TextBox2.Clear()

        TextBox3.Clear()

        TextBox5.Clear()

        TextBox2.Focus()

        PDF.src = (TextBox3.Text)

    End Sub

 

    Sub Ketemu()

        On Error Resume Next

        TextBox2.Text = dr(1)

        TextBox3.Text = dr(2)

        TextBox2.Focus()

        PDF.src = (TextBox3.Text)

    End Sub

 

    Sub Carikode()

        Call Koneksi()

        cmd = New SqlCommand("select * from TBLPDF where id='" & TextBox1.Text & "'", conn)

        dr = cmd.ExecuteReader

        dr.Read()

    End Sub

 

    Sub Tampilgrid()

        Call Koneksi()

        da = New SqlDataAdapter("select * from TBLPDF", conn)

        ds = New DataSet

        da.Fill(ds)

        DGV.DataSource = ds.Tables(0)

        DGV.ReadOnly = True

      

        DGV.RowsDefaultCellStyle.BackColor = Color.AliceBlue

        DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.AntiqueWhite

    End Sub

 

    Sub awal()

        Call Kosongkan()

        Call Tampilgrid()

    End Sub

 

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

        Me.CenterToScreen()

        Call awal()

    End Sub

 

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        TextBox1.MaxLength = 3 'batas max digit

        If e.KeyCode = Keys.Enter Then

            Call Carikode()

            If dr.HasRows Then

                Call Ketemu()

            Else

                Call databaru()

            End If

        End If

    End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then

            MsgBox("data belum lengkap")

            Exit Sub

        End If

 

        Try

            Call Carikode()

            If Not dr.HasRows Then

                Call Koneksi()

                Dim simpan As String = "insert into TBLPDF values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')"

                cmd = New SqlCommand(simpan, conn)

                cmd.ExecuteNonQuery()

            Else

                Call Koneksi()

                Dim edit As String = "update TBLPDF set Nama='" & TextBox2.Text & "',lokasi='" & TextBox3.Text & "' where id='" & TextBox1.Text & "'"

                cmd = New SqlCommand(edit, conn)

                cmd.ExecuteNonQuery()

            End If

            Call awal()

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If MessageBox.Show("yakin akan dihapus...?", "Perhatian", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            Call Koneksi()

            Dim hapus As String = "delete from TBLPDF  where id='" & TextBox1.Text & "'"

            cmd = New SqlCommand(hapus, conn)

            cmd.ExecuteNonQuery()

            Call awal()

        Else

            Call Kosongkan()

        End If

    End Sub

 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Call Kosongkan()

    End Sub

 

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Me.Close() ' tutup form

        'End  = tutup project

    End Sub

 

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged

        Call Koneksi()

        da = New SqlDataAdapter("select * from TBLPDF where Nama like '%" & TextBox5.Text & "%'", conn)

        ds = New DataSet

        da.Fill(ds)

        DGV.DataSource = ds.Tables(0)

        DGV.ReadOnly = True

    End Sub

 

    Private Sub DGV_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV.CellMouseClick

        On Error Resume Next

        TextBox1.Text = DGV.Rows(e.RowIndex).Cells(0).Value

        Call Carikode()

        If dr.HasRows Then

            Call Ketemu()

        End If

    End Sub

 

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        On Error Resume Next

        OpenFileDialog1.ShowDialog()

        TextBox3.Text = OpenFileDialog1.FileName

        PDF.src = (TextBox3.Text)

    End Sub

End Class