0

Untitled.jpg

Why can not save to the database?

my script

Imports MySql.Data.MySqlClient
Imports System.Data.Odbc

Public Class Arsip
    Const DSN = "DSN=arsip"
Dim conn As OdbcConnection
Dim cmd As OdbcCommand
Dim da As OdbcDataAdapter
Dim dr As OdbcDataReader
Dim ds As DataSet
Dim table As DataTable

Sub koneksi()
    conn = New OdbcConnection(DSN)
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
End Sub

Sub tampilkan_data()
    koneksi()
    da = New OdbcDataAdapter("select * from arsip", conn)
    ds = New DataSet
    ds.Clear()
    da.Fill(ds, 0)
    table = ds.Tables(0)
    DGV.DataSource = table
    conn.Close()
End Sub

Private Sub Arsip_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    tampilkan_data()
End Sub

Private Sub btnsimpan_Click(sender As Object, e As EventArgs) Handles btnsimpan.Click
    Try
        koneksi()
        cmd = New OdbcCommand("insert into arsip values (?, ?, ?, ?)", conn)
        With cmd
            .Parameters.AddWithValue("?", TextNO.Text)
            .Parameters.AddWithValue("?", TextJenis.Text)
            .ExecuteNonQuery()
        End With
        conn.Close()

        tampilkan_data()

        TextNO.Text = ""
        TextJenis.Text = ""
        TextNO.Focus()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub TextJenis_TextChanged(sender As Object, e As EventArgs) Handles TextJenis.TextChanged

End Sub

Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged

End Sub
End Class
4
Contributors
4
Replies
24
Views
1 Week
Discussion Span
Last Post by Mr.M
2

A word of advice - you seem to like starting threads with the title

(ask)

That is not the best way to get an answer. A thread with a meaningful title is far more likely to get a timely, and useful response.

Edited by Reverend Jim

1

You problem is here

        cmd = New OdbcCommand("insert into arsip values (?, ?, ?, ?)", conn)
        With cmd
            .Parameters.AddWithValue("?", TextNO.Text)
            .Parameters.AddWithValue("?", TextJenis.Text)
            .ExecuteNonQuery()
        End With
        conn.Close()

you have 4 values that need to be used but you only used 2.

If your database doen't have 4 fields then your code should be like this:

        cmd = New OdbcCommand("insert into arsip values (?, ?)", conn)
        With cmd
            .Parameters.AddWithValue("?", TextNO.Text)
            .Parameters.AddWithValue("?", TextJenis.Text)
            .ExecuteNonQuery()
        End With
        conn.Close()

Edited by Mr.M: Correcting

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.