Click here to Skip to main content
15,886,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello again, this is my second question and it still regarding about the project i'm making in this link:

date time picker error[^]

so i figured out about the date time picker problem,the program is running there is still problem and this time i'ts about the save function:

after i input all the data in the form textbox and click the button save the message box that says "data berhasil di simpan" is displayed, But the data that was saved wasn't displayed on the datagridview or the sql server.

my question is:
how do i insert the data so it can be displayed on the data grid view?
should i even use sql server to create database for this program?
what value should i use on the Btnsimpan coding?
If there is something wrong in my coding or SQL can anybody help me to point out the mistake?



this is the sql script:
sql script:
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)
 
select*from siswa


and this is the other coding:
form:
VB
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ClassKoneksi.namadatabase = "KPIRWAN"
        ClassSiswa.namadatabase = "KPIRWAN"
        Dim dsdata As New DataSet
        Dim sql As String
        sql = "select*from datasiswa"
        dsdata = ClassSiswa.displayData(ClassSiswa.opencon, sql, "Datasiswa")
        DataGridView1.DataSource = dsdata
        DataGridView1.DataMember = "datasiswa"
        DataGridView1.ReadOnly = True
        ClassSiswa.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_tinggisiswa.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_tinggisiswa.Text
        ClassSiswa.RekamData(ClassSiswa.opencon)
        MsgBox("Data siswa Berhasil Disimpan")
    End Sub
    Private Sub DateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DTP_siswa.ValueChanged
        DTP_siswa.Format = DateTimePickerFormat.Custom
        DTP_siswa.ShowUpDown = False
    End Sub
End Class


class connection:
Public Class ClassKoneksi

    Private Shared CN As SqlClient.SqlConnection
    Private Shared _NamaDatabase As String


    Public Shared Property namadatabase As String
        Get
            Return _NamaDatabase

        End Get
        Set(value As String)

            _NamaDatabase = value

        End Set
    End Property

    Public Shared Function opencon() As SqlClient.SqlConnection
        CN = New SqlClient.SqlConnection
        CN.ConnectionString = "Data Source=RICKY-PC\LENOVOSQL;Initial Catalog=KPIRWAN;Integrated Security=True "
        CN.Open()
        Return CN
    End Function

    Public Shared Sub closecon()
        CN.Close()
        CN.ConnectionString = Nothing
    End Sub
End Class


class student:
Public Class ClassSiswa
    Inherits ClassKoneksi
    Private Shared _Nis, _Nisn, _Berat_Badan, _Tinggi_Badan As Integer
    Private Shared _Nama_Siswa, _Jenis_Kelamin, _Kota_Lahir, _Agama As String
    Private Shared _Tanggal_Lahir As Date
    Public Shared Property Nis() As Integer
        Get
            Return _Nis
        End Get
        Set(ByVal value As Integer)
            _Nis = value
        End Set
    End Property
    Public Shared Property Nisn() As Integer
        Get
            Return _Nisn
        End Get
        Set(ByVal value As Integer)
            _Nisn = value
        End Set
    End Property
    Public Shared Property Berat_Badan() As Integer
        Get
            Return _Berat_Badan
        End Get
        Set(ByVal value As Integer)
            _Berat_Badan = value
        End Set
    End Property
    Public Shared Property Tinggi_Badan() As Integer
        Get
            Return _Tinggi_Badan
        End Get
        Set(ByVal value As Integer)
            _Tinggi_Badan = value
        End Set
    End Property
    Public Shared Property Nama_Siswa() As String
        Get
            Return _Nama_Siswa
        End Get
        Set(ByVal value As String)
            _Nama_Siswa = value
        End Set
    End Property
    Public Shared Property Jenis_Kelamin() As String
        Get
            Return _Jenis_Kelamin
        End Get
        Set(ByVal value As String)
            _Jenis_Kelamin = value
        End Set
    End Property
    Public Shared Property Kota_Lahir() As String
        Get
            Return _Kota_Lahir
        End Get
        Set(ByVal value As String)
            _Kota_Lahir = value
        End Set
    End Property
    Public Shared Property Tanggal_Lahir() As DateTime
        Get
            Return _Tanggal_Lahir
        End Get
        Set(ByVal value As DateTime)
            _Tanggal_Lahir = value
        End Set
    End Property
    Public Shared Property Agama() As String
        Get
            Return _Agama
        End Get
        Set(ByVal value As String)
            _Agama = value
        End Set
    End Property
    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
End Class



i'm really sorry for this, i'm still a complete beginner at this even thought i tried to study about programming i still can't understand a thing.
Posted
Updated 30-Mar-15 5:57am
v6
Comments
[no name] 29-Mar-15 8:54am    
Please clarify your question, if you ask clear question it is easy to response.
Richard Deeming 30-Mar-15 12:55pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

Search about ado.net

Some steps

Imports system. data.sqlclient


Dim con as new sqlconnection(connect ion string)
Con.open

Just Google it u will get 100s of answer just modify SQL command query to suit or needs
 
Share this answer
 
Comments
asasql 29-Mar-15 9:56am    
where should i put the coding?
is it in class or the form ?
Mantu Singh 29-Mar-15 10:17am    
Dim I as integer=sql.ExecuteNonQuery()
Msgbox(I)
See value it must be >0 for successful insert
asasql 30-Mar-15 11:21am    
still not working.
whre should i put the:
Dim I as integer=sql.ExecuteNonQuery()
Msgbox(I)
in class or form coding?

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