Click here to Skip to main content
15,847,653 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello, i'm currently making a application to register using visual studio 2012 and SQL server 2012.

my windows form has four groupbox each with it's own textbox,combobox,and datetimepicker and one datagridview.

the function i want is this:

if i insert the data in different groupbox using save button the datagridview will display different table for each groupbox.

my qustion is:

how do i make my save button able to save different data from different table from one SQL database in one datagridview in one form?

this is my coding:

form1
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ClassKoneksi.namadatabase = "KPIRWAN"
        ClassSiswa.namadatabase = "KPIRWAN"
        ClassOrangtua.namadatabase = "KPIRWAN"
        ClassWali.namadatabase = "KPIRWAN"
        datagridview()
    End Sub
    Sub datagridview()
        Dim sql As String
        Dim dsdata As New DataSet
        sql = "select*from siswa"
        sql = "select*from orangtua"
        sql = "select*from wali"
        dsdata = ClassSiswa.displayData(ClassSiswa.opencon, sql, "siswa")
        dsdata = ClassOrangtua.displayData(ClassOrangtua.opencon, sql, "orangtua")
        dsdata = ClassWali.displayData(ClassWali.opencon, sql, "wali")
        DataGridView1.DataSource = dsdata
        DataGridView1.DataMember = "siswa"
        DataGridView1.DataMember = "orangtua"
        DataGridView1.DataMember = "wali"
        DataGridView1.ReadOnly = True
        ClassKoneksi.closecon()
End Sub
    Private Sub btnKeluar_Click(sender As Object, e As EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub

    Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.Click
        If Len(Trim(txt_nis.Text)) = 0 Or Len(Trim(txt_nisn.Text)) = 0 Or Len(Trim(txt_namasiswa.Text)) = 0 Or Len(Trim(cmb_kelaminsiswa.Text)) = 0 Or Len(Trim(txt_kotalahir.Text)) = 0 Or Len(Trim(DTP_siswa.Value)) = 0 Or Len(Trim(cmb_agamasiswa.Text)) = 0 Or Len(Trim(txt_beratsiswa.Text)) = 0 Or Len(Trim(txt_tinggi_badan.Text)) Then
            MsgBox("Data Belum Lengkap")
        ElseIf Len(Trim(cmb_ket_ayah.Text)) = 0 Or Len(Trim(txt_namaayah.Text)) = 0 Or Len(Trim(DTP_ayah.Text)) = 0 Or Len(Trim(txt_kotalahirayah.Text)) = 0 Or Len(Trim(cmb_agamaayah.Text)) = 0 Or Len(Trim(txt_ijazahayah.Text)) = 0 Or Len(Trim(txt_pekerjaanayah.Text)) = 0 Or Len(Trim(txt_pendapatanayah.Text)) = 0 Or Len(Trim(txt_alamatayah.Text)) = 0 Then
            MsgBox("Data Belum Lengkap")
        ElseIf Len(Trim(cmb_ket_ibu.Text)) = 0 Or Len(Trim(txt_namaibu.Text)) = 0 Or Len(Trim(DTP_ibu.Text)) = 0 Or Len(Trim(txt_kotalahiribu.Text)) = 0 Or Len(Trim(cmb_agamaibu.Text)) = 0 Or Len(Trim(txt_ijazahibu.Text)) = 0 Or Len(Trim(txt_pekerjaanibu.Text)) = 0 Or Len(Trim(txt_pendapatanibu.Text)) = 0 Or Len(Trim(txt_alamatibu.Text)) Then
            MsgBox("Data Belum Lengkap")
        ElseIf Len(Trim(cmb_ket_wali.Text)) = 0 Or Len(Trim(txt_namawali.Text)) = 0 Or Len(Trim(cmb_kelaminwali.Text)) = 0 Or Len(Trim(DTP_wali.Text)) = 0 Or Len(Trim(txt_kotalahirwali.Text)) = 0 Or Len(Trim(cmb_agamawali.Text)) = 0 Or Len(Trim(txt_ijazahwali.Text)) = 0 Or Len(Trim(txt_pekerjaanwali.Text)) = 0 Or Len(Trim(txt_pendapatanwali.Text)) = 0 Or Len(Trim(txt_alamatwali.Text)) = 0 Then
            MsgBox("Data Belum Lengkap")
            Exit Sub
        End If
        ClassSiswa.Nis = txt_nis.Text
        ClassSiswa.Nisn = txt_nisn.Text
        ClassSiswa.Nama_Siswa = txt_namasiswa.Text
        ClassSiswa.Jenis_Kelamin = cmb_kelaminsiswa.Text
        ClassSiswa.Kota_Lahir = txt_kotalahir.Text
        ClassSiswa.Tanggal_Lahir = DTP_siswa.Value
        ClassSiswa.Agama = cmb_agamasiswa.Text
        ClassSiswa.Berat_Badan = txt_beratsiswa.Text
        ClassSiswa.Tinggi_Badan = txt_tinggi_badan.Text
        ClassSiswa.RekamData(ClassSiswa.opencon)
        MsgBox("Data siswa Berhasil Disimpan")
        datagridview()
        Call kosongkan()

        ClassOrangtua.Ket_Ayah = cmb_ket_ayah.Text
        ClassOrangtua.Nama_Ayah = txt_namaayah.Text
        ClassOrangtua.Tanggal_Lahir_Ayah = DTP_ayah.Value
        ClassOrangtua.Kota_Ayah = txt_kotalahirayah.Text
        ClassOrangtua.Agama_Ayah = cmb_agamaayah.Text
        ClassOrangtua.Ijazah_Ayah = txt_ijazahayah.Text
        ClassOrangtua.Pekerjaan_Ayah = txt_pekerjaanayah.Text
        ClassOrangtua.Pendapatan_Ayah = txt_pendapatanayah.Text
        ClassOrangtua.Alamat_Ayah = txt_alamatayah.Text
        ClassOrangtua.RekamData(ClassOrangtua.opencon)
        MsgBox("Data ayah Berhasil Disimpan")
        datagridview()
        Call kosongkan()

        ClassOrangtua.Ket_Ibu = cmb_ket_ibu.Text
        ClassOrangtua.Nama_Ibu = txt_namaibu.Text
        ClassOrangtua.Tanggal_Lahir_Ibu = DTP_ibu.Value
        ClassOrangtua.Kota_Ibu = txt_kotalahiribu.Text
        ClassOrangtua.Agama_Ibu = cmb_agamaibu.Text
        ClassOrangtua.Ijazah_Ibu = txt_ijazahibu.Text
        ClassOrangtua.Pekerjaan_Ibu = txt_pekerjaanibu.Text
        ClassOrangtua.Pendapatan_Ibu
        ClassOrangtua.Alamat_Ibu = txt_alamatibu.Text
        ClassOrangtua.RekamData(ClassOrangtua.opencon)
        MsgBox("Data ibu Berhasil Disimpan")
        datagridview()
        Call kosongkan()

        ClassWali.Ket_Wali = cmb_ket_wali.Text
        ClassWali.Nama_Wali = txt_namawali.Text
        ClassWali.Kelamin_Wali = cmb_kelaminwali.Text
        ClassWali.Tanggal_Lahir_Wali = DTP_wali.Value
        ClassWali.Kota_Wali = txt_kotalahirwali.Text
        ClassWali.Agama_Wali = cmb_agamawali.Text
        ClassWali.Ijazah_Wali = txt_ijazahwali.Text
        ClassWali.Pekerjaan_Wali = txt_pekerjaanwali.Text
        ClassWali.Pendapatan_Wali = txt_pendapatanwali.Text
        ClassWali.Alamat_Wali = txt_alamatwali.Text
        ClassWali.RekamData(ClassWali.opencon)
        MsgBox("Data wali Berhasil Disimpan")
        datagridview()
        Call kosongkan()

    End Sub


class orangtua
Public Shared Sub RekamData(ByVal _Cn As SqlClient.SqlConnection)
        Dim sql As SqlClient.SqlCommand
        sql = New SqlClient.SqlCommand
        sql.Connection = _Cn
        sql.CommandType = CommandType.Text
        sql.CommandText = "Insert into orangtua values(','" & Ket_Ayah & "','" & Nama_Ayah & "','" & Tanggal_Lahir_Ayah & "','" & Kota_Ayah & "','" & Agama_Ayah & "','" & Ijazah_Ayah & "','" & Pekerjaan_Ayah & "','" & Pendapatan_Ayah & "','" & Alamat_Ayah & "','" & Ket_Ibu & "','" & Nama_Ibu & "','" & Tanggal_Lahir_Ibu & "','" & Kota_Ibu & "','" & Agama_Ibu & "','" & Ijazah_Ibu & "','" & Pekerjaan_Ibu & "','" & Pendapatan_Ibu & "','" & Alamat_Ibu & "')"
        sql.ExecuteNonQuery()
    End Sub
    Public Shared Function displayData(ByVal _cn As SqlClient.SqlConnection, ByVal _sql As String, ByVal _orangtua As String) As DataSet
        Dim _da As New SqlClient.SqlDataAdapter
        Dim _dsdata As New DataSet
        Dim sql As New SqlClient.SqlCommand
        _dsdata.Clear()
        sql.Connection = _cn
        sql.CommandType = CommandType.Text
        sql.CommandText = _sql
        _da.SelectCommand = sql
        _da.Fill(_dsdata, _orangtua)
        Return _dsdata
    End Function
End Class


classsiswa
Public Shared Sub RekamData(ByVal _Cn As SqlClient.SqlConnection)
        Dim sql As SqlClient.SqlCommand
        sql = New SqlClient.SqlCommand
        sql.Connection = _Cn
        sql.CommandType = CommandType.Text
        sql.CommandText = "Insert into siswa values('" & Nis & "','" & Nisn & "','" & Nama_Siswa & "','" & Jenis_Kelamin & "','" & Kota_Lahir & "','" & Tanggal_Lahir & "','" & Agama & "','" & Berat_Badan & "','" & Tinggi_Badan & "')"
        sql.ExecuteNonQuery()
    End Sub

    Public Shared Function displayData(ByVal _cn As SqlClient.SqlConnection, ByVal _sql As String, ByVal _siswa As String) As DataSet
        Dim _da As New SqlClient.SqlDataAdapter
        Dim _dsdata As New DataSet
        Dim sql As New SqlClient.SqlCommand
        _dsdata.Clear()
        sql.Connection = _cn
        sql.CommandType = CommandType.Text
        sql.CommandText = _sql
        _da.SelectCommand = sql
        _da.Fill(_dsdata, _siswa)
        Return _dsdata
    End Function


classwali
Public Shared Sub RekamData(ByVal _Cn As SqlClient.SqlConnection)
        Dim sql As SqlClient.SqlCommand
        sql = New SqlClient.SqlCommand
        sql.Connection = _Cn
        sql.CommandType = CommandType.Text
        sql.CommandText = "Insert into wali values('" & Ket_Wali & "','" & Nama_Wali & "','" & Kelamin_Wali & "','" & Tanggal_Lahir_Wali & "','" & Kota_Wali & "','" & Agama_Wali & "','" & Ijazah_Wali & "','" & Pekerjaan_Wali & "','" & Pendapatan_Wali & "','" & Alamat_Wali & "')"
        sql.ExecuteNonQuery()
    End Sub
    Public Shared Function displayData(ByVal _cn As SqlClient.SqlConnection, ByVal _sql As String, ByVal _wali As String) As DataSet
        Dim _da As New SqlClient.SqlDataAdapter
        Dim _dsdata As New DataSet
        Dim sql As New SqlClient.SqlCommand
        _dsdata.Clear()
        sql.Connection = _cn
        sql.CommandType = CommandType.Text
        sql.CommandText = _sql
        _da.SelectCommand = sql
        _da.Fill(_dsdata, _wali)
        Return _dsdata
    End Function
End Class


SQL query:
Create Database KPIRWAN
use KPIRWAN

Create Table siswa 
(
Nis int primary key,
Nisn int,
Nama_Siswa varchar(40),
Jenis_Kelamin varchar (10),
Kota_Lahir varchar (10),
Tanggal_Lahir date,
Agama varchar (10),
Berat_Badan int,
Tinggi_Badan int,
)

create table orangtua
(
Ket_Ayah varchar(40),
Nama_Ayah varchar(40),
Tanggal_Lahir_Ayah date,
Kota_Ayah varchar(40),
Agama_Ayah varchar(40),
Ijazah_Ayah varchar(40),
Pekerjaan_Ayah varchar(40),
Pendapatan_Ayah int,
Alamat_Ayah varchar(40),
Ket_Ibu varchar(40),
Nama_Ibu varchar(40),
Tanggal_Lahir_Ibu date,
Kota_Ibu varchar(40),
Agama_Ibu varchar(40),
Ijazah_Ibu varchar(40),
Pekerjaan_Ibu varchar(40),
Pendapatan_Ibu int,
Alamat_Ibu varchar(40),
)

create table wali
(
Ket_Wali varchar(40),
Nama_Wali varchar(40),
Kelamin_Wali varchar (10),
Tanggal_Lahir_Wali date,
Kota_Wali varchar(40),
Agama_Wali varchar(40),
Ijazah_Wali varchar(40),
Pekerjaan_Wali varchar(40),
Pendapatan_Wali int,
Alamat_Wali varchar(40),
)
Posted
Updated 10-May-15 6:07am
v4
Comments
Sergey Alexandrovich Kryukov 10-May-15 11:50am    
You need to explain it in more detail, but without all these code sample. You need to analyze what is similar in different calls and what do they have in common.
—SA
Maciej Los 10-May-15 11:51am    
Not clear what you're asking for...

1 solution

Sorry, but you mixed everything!

1. Do not use the same methods for the classes which differs only with names!
iswa, wali and ornagtua classes are using the same methods RekamData and DisplayData. It's not valid programming practice. Instead of that, use one base class and few derived classes which can inherit from base class.
See:
Inheritance in Visual Basic[^]
Inheritance from a Base Class in Microsoft .NET[^]
Inheritance-Based Polymorphism[^]
When to Use Inheritance[^]

Seems your problem is similar to situation when you want to use different color of pen: once you need blue color, another - the red one.
See:
VB
Public Class Pen
    Private sColor As String = String.Empty 
    
	'default constructor
	Public Sub New()
		sColor = "Blue" 'default color for Pen
	End Sub
	
	'
    Public Sub New(ByVal newColor As String)
        sColor = newColor
    End Sub

    Public Property Color As String
		Get
			Return sColor
		End Get
		Set (value As String)
			sColor = value
		End Set
	End Property
	
End Class


Usage:
VB
Console.WriteLine("Welcome! What color of pen do you want to use?")
Dim col As String
Do
    col = Console.ReadLine()
Loop While col Is Nothing

Dim mypen As Pen = New Pen(col)
Console.WriteLine("Pen's color is: {0}", mypen.Color)


Analogically you have to create your own class.
For further information, please see this:
9.6 Instance and Shared Variables[^]
Using Constructors and Destructors[^]

2. Use Data Access Layer when you work with databases
See:
Tutorial 1: Creating a Data Access Layer[^]
Walkthrough: Creating the Data Access and Business Logic Layers in ASP.NET[^]

3. Use Stored Procedures to avoid Sql Injection[^] attacks
See:
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]
How to: Use Stored Procedures that Take Parameters[^]
Walkthrough: Using Only Stored Procedures (Visual Basic)[^]

Try! Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900