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

Maybe because you have four ? parameters but you set only 2 of them???

a.jpg

If this?

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.

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()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.