Source Code Test Program Dengan VB Net Jastis Bago
[email protected] [email protected] http://niascommunity.web.id
Lisensi Dokumen:
Copyright © 2003-2006 IlmuKomputer.Com Seluruh dokumen di IlmuKomputer.Com dapat digunakan, dimodifikasi dan disebarkan secara bebas untuk tujuan bukan komersial (nonprofit), dengan syarat tidak menghapus atau merubah atribut penulis dan pernyataan copyright yang disertakan dalam setiap dokumen. Tidak diperbolehkan melakukan penulisan ulang, kecuali mendapatkan ijin terlebih dahulu dari IlmuKomputer.Com.
Pendahuluan Step by step → Buatlah database dan sebuah tabel (silahkan diganti nama database dan tabelnya) seperti seperti berikut :
→ Silahkan isi tabel (sesuaikan) atau boleh juga nanti ditambahkan.
1. Setelah itu buat satu form seperti berikut :
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
1
→ Sekarang kita buat sebuah koneksi ke database. Tambahkan sebuah Module (Module koneksi) pada project dan tuliskan script seperti berikut :
Imports System.Data Imports System.Data.SqlClient Module Module1 Public DA As SqlDataAdapter Public CMD As SqlCommand Public RD As SqlDataReader Public STR As String Public DS As DataSet Public DT As DataTable Public Function koneksi() As SqlConnection Dim supernothing As New SqlConnection supernothing = New SqlConnection("server=YCL-1; database=tutorial; trusted_Connection=true") '/ Koneksi di komputer saya '/ supernothing.Open() Return (supernothing) End Function End Module
Perhatikan tulisan supernothing = New SqlConnection("server=YCL-1; database=tutorial; trusted_Connection=true") silahkan diganti sesuai nama database dan server anda. → Tuliskan atau copy paste script berikut 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
Imports System.Data Imports System.Data.SqlClient Public Class propinsi Private Sub propinsi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox1.Items.Add("ALL") ComboBox2.Items.Add("ALL") ComboBox3.Items.Add("ALL") ComboBox3.Items.Add("Kelurahan") ComboBox3.Items.Add("Kecamatan") TextBox6.Enabled = False data() End Sub Sub data() CMD = New SqlCommand("select distinct kecamatan from propinsi_jastis", Module1.koneksi) RD = CMD.ExecuteReader Do While RD.Read ComboBox1.Items.Add(RD.GetString(0)) Loop
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
2
22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95.
CMD = New SqlCommand("select distinct kotamadya from propinsi_jastis", Module1.koneksi) RD = CMD.ExecuteReader Do While RD.Read ComboBox2.Items.Add(RD.GetString(0)) Loop End Sub Sub clear() TextBox1.Clear() TextBox2.Clear() TextBox3.Clear() TextBox4.Clear() TextBox5.Clear() End Sub Sub tampil() DA = New SqlDataAdapter("select * from propinsi_jastis", Module1.koneksi) DS = New DataSet DS.Clear() DA.Fill(DS, "propinsi_jastis") DGV.DataSource = (DS.Tables("propinsi_jastis")) DGV.ReadOnly = True Call tabel() End Sub Sub tabel() Try DGV.Columns(0).HeaderText = "Kode Pos" DGV.Columns(1).HeaderText = "Kelurahan" DGV.Columns(2).HeaderText = "Kecamatan" DGV.Columns(3).HeaderText = "Kotamadya" DGV.Columns(4).HeaderText = "Jumlah Penduduk" Catch ex As Exception MsgBox(ex.Message) End Try 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 = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Then MsgBox("data Belum Lengkap") Else DA = New SqlDataAdapter("select * from propinsi_jastis where kode_pos='" & TextBox1.Text & "'", Module1.koneksi) DT = New DataTable("propinsi_jastis") DA.Fill(DT) If DT.Rows.Count <> 1 Then STR = "insert into propinsi_jastis (kode_pos,kelurahan,kecamatan,kotamadya,jlh_penduduk) values " & _ "('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')" CMD = New SqlCommand(STR, Module1.koneksi) CMD.ExecuteNonQuery() MsgBox("Data sukses ") clear() tampil() ComboBox1.ResetText() ComboBox2.ResetText() Button4.Text = "Kembali" Else MsgBox("Kode Pos : " & TextBox1.Text & " Sudah ada, input data lain") DGV.DataSource = DT Call tabel() End If End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Select Case Button4.Text Case "&Tampilkan" Button4.Text = "&Kembali" tampil() Case "&Kembali" DGV.DataSource = Nothing Button4.Text = "&Tampilkan" ComboBox1.ResetText() ComboBox2.ResetText() ComboBox3.ResetText() TextBox6.Clear() TextBox6.Enabled = False
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
3
96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160.
End Select End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If TextBox1.Text = "" Then MsgBox("silahkan piliha data yang mau diupdate dari tabel") Else STR = "update propinsi_jastis set kelurahan='" & TextBox2.Text & "',kecamatan='" & TextBox3.Text & "',kotamadya='" & TextBox4.Text & "',jlh_penduduk='" & TextBox5.Text & "' where kode_pos='" & TextBox1.Text & "'" CMD = New SqlCommand(STR, Module1.koneksi) CMD.ExecuteNonQuery() MsgBox("data sudah diupdate") tampil() TextBox1.Enabled = True clear() End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If TextBox1.Text = "" Then MsgBox("silahkan piliha data yang mau dihapus dari tabel") Else If MessageBox.Show("Yakin dihapus ?", "Konfirmasi", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then CMD = New SqlCommand("delete from propinsi_jastis where kode_pos='" & TextBox1.Text & "'", Module1.koneksi) CMD.ExecuteNonQuery() MsgBox("Data sudah dihapus") tampil() clear() ComboBox1.ResetText() ComboBox2.ResetText() ComboBox3.ResetText() TextBox1.Enabled = True End If End If End Sub Private Sub DGV_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick Dim i As Integer i = DGV.CurrentRow.Index() TextBox1.Text = DGV.Item(0, i).Value TextBox2.Text = DGV.Item(1, i).Value TextBox3.Text = DGV.Item(2, i).Value TextBox4.Text = DGV.Item(3, i).Value TextBox5.Text = DGV.Item(4, i).Value TextBox1.Enabled = False End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged DA = New SqlDataAdapter("select * from propinsi_jastis where kecamatan='" & ComboBox1.Text & "'", Module1.koneksi) DT = New DataTable("propinsi_jastis") DA.Fill(DT) DGV.DataSource = DT tabel() Button4.Text = "&Kembali" End Sub Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged DA = New SqlDataAdapter("select * from propinsi_jastis where kotamadya='" & ComboBox2.Text & "'", Module1.koneksi) DT = New DataTable("propinsi_jastis") DA.Fill(DT) DGV.DataSource = DT tabel() Button4.Text = "&Kembali" End Sub
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged 161. TextBox6.Enabled = True 162. TextBox6.Clear()
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
4
163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185.
TextBox6.Focus() End Sub Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged If TextBox6.Text = "" Then DGV.DataSource = Nothing Else If ComboBox3.Text = "Kelurahan" Then DA = New SqlDataAdapter("select * from propinsi_jastis where kelurahan like'%" & TextBox6.Text & "%'", Module1.koneksi) DT = New DataTable("propinsi_jastis") DA.Fill(DT) DGV.DataSource = DT tabel() Button4.Text = "&Kembali" ElseIf ComboBox3.Text = "Kecamatan" Then DA = New SqlDataAdapter("select * from propinsi_jastis where kecamatan like'%" & TextBox6.Text & "%'", Module1.koneksi) DT = New DataTable("propinsi_jastis") DA.Fill(DT) DGV.DataSource = DT tabel() Button4.Text = "&Kembali" Else DA = New SqlDataAdapter("select * from propinsi_jastis where kode_pos like'%" & TextBox6.Text & "%' or kelurahan like'%" & TextBox6.Text & "%' or kecamatan like'%" & TextBox6.Text & "%' or kotamadya like'%" & TextBox6.Text & "%' or jlh_penduduk like'%" & TextBox6.Text & "%'", Module1.koneksi) DT = New DataTable("propinsi_jastis") DA.Fill(DT) DGV.DataSource = DT tabel() Button4.Text = "&Kembali" End If End If End Sub
186. 187. 188. 189. 190. 191. 192. 193. 194. 195. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click 196. report.Show() 197. End Sub 198. End Class
Jika koding diatas sudah sesuai maka hasilnya seperti berikut :
Note : Silahkan diganti sesuai kebutuhan. Sekarang kita akan membuat laporan yaitu dengan menggunakan crystal report. → Silahkan klik Add New Item, sehingga muncul dialog seperti berikut :
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
5
Pilih Reporting dan klik Crystal Report akan keluar kota dialog berikut :
Pilih Using the Report Wizard (terserah sih) lalu Ok kan.
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
6
Klik OLE DB(ADO) untuk membuat koneksi baru.
Pada dialog ini pilih Microsoft OLE DB Provider fro SQL Server lalu Next kan.
Isikan nama Server dan Database anda.
Pilih tabel yang ingin ditampilkan pada crystal report.
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
7
Pilih filed ataupun semuanya untuk di tampilkan lalu klik finis sehingga keluar tampilan seperti berikut. Karena Kode Pos tadi tipe datanya adalah smallint maka kita akan ganti formatnya disini agar waktu ditampilkan menjadi number. Klik kanan pada text Kode_pos :
Pilih Format Object untuk mengganti format text kode_pos ke number.
Silahkan pilih seperti yang terlihat digambar atas lalu klik OK. Untuk tampilan laporan ini silahkan di buat sendiri sesuai selera anda. :D Setelah crystal report selesai sekarang kita buat satu form lagi untuk menampung crystal report yang telah kita buat tadi......berikut tampilan formnya.
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
8
Masukkan CrystalReportViewer dari ToolBox ke Form lalu pada propertiesnya rename CrystalReportViewer menjadi CRV (terserah sih). Pada properties di bagian datasource silahkan pilih report yang telah kita design tadi, seperti berikut :
Lalu save dan jalankan programmnya.............. SELAMAT MENCOBA !
Penutup
Tutorial ini masih sangat jauh dari kesempurnaan, mungkin saja masih ada coding yang salah tapi setidaknya teman-teman dapat melakukan exploitasi dengan menggunakan imajinasi yang ada untuk membuat coding ini menjadi lebih sempurna. Untuk contoh program dapat didownload di http:niascommunity.web.id dibagian download area.
Biografi Penulis
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
9
Nama Jastis Bago. Lahir Di Nias 27 Juli 1988. Menjadi Administrator Forum Nias Community dan Indonesian BlackHat Team dengan alamat http://niascommunity.web.id , http://indonesianblackhat.web.id serta menjadi TOS (Team Of Smadav) salah satu Anti Viurs Lokal di Indonesia. Saat ini sedang menyelesaikan pendidikan S1 jurusan Sistem Informasi di STMIK Potensi Utama – Medan. I’m Supernothing Aku bukan siapa-siapa seperti yang anda kenal. Aku hanya belajar dari apa yang saya tidak tahu agar saya menjadi tahu. “Learn what i do not know, sharing i knowledge and doing what i think is right.” Read More About Me......http://jastisbago.wordpress.com/
Komunitas eLearning IlmuKomputer.Com Copyright © 2003-2007 IlmuKomputer.Com
10