Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ComboBox1.Text = "" Or Label5.Text = "" Then
            MsgBox("Lengkapi Transaksi")

        Else
            TglSql = Format(Today, "yyyy-MM-dd")
            Call Koneksi()
            simpan = "INSERT INTO tbbeli(nofak,tanggal,kodesup,totalitem,totalbayar) VALUES (@p1,@p2,@p3,@p4,@p5)"
            simpan1 = "INSERT INTO tbdetailbeli(nofak,kodebarang,jumlah) VALUES (@p6,@p7,@p8) "
            ubah = "UPDATE tbbarang SET stok=stok+@p9 WHERE kodebarang = @p10"
            CMD = Conn.CreateCommand
            With CMD
                .CommandText = simpan
                .Connection = Conn
                .Parameters.Add("p1", OdbcType.VarChar, 20).Value = Label12.Text
                .Parameters.Add("p2", OdbcType.DateTime).Value = TglSql
                .Parameters.Add("p3", OdbcType.VarChar, 6).Value = ComboBox1.Text
                .Parameters.Add("p4", OdbcType.Int, 11).Value = Label8.Text
                .Parameters.Add("p5", OdbcType.Int, 11).Value = Label10.Text
                .ExecuteNonQuery()
            End With
            For i As Integer = 0 To DataGridView1.Rows.Count - 2
                CMD = Conn.CreateCommand
                With CMD
                    .CommandText = simpan1
                    .Connection = Conn
                    .Parameters.Add("p6", OdbcType.VarChar, 20).Value = Label12.Text
                    .Parameters.Add("p7", OdbcType.VarChar, 8).Value = DataGridView1.Rows(i).Cells(0).Value
                    .Parameters.Add("p8", OdbcType.Int).Value = DataGridView1.Rows(i).Cells(3).Value
                    .ExecuteNonQuery()
                End With
                CMD = Conn.CreateCommand
                With CMD

                    .CommandText = ubah
                    .Connection = Conn
                    .Parameters.Add("p9", OdbcType.Int).Value = DataGridView1.Rows(i).Cells(3).Value
                    .Parameters.Add("p10", OdbcType.VarChar).Value = DataGridView1.Rows(i).Cells(0).Value
                    .ExecuteNonQuery()
                End With
            Next
            MsgBox("Barang Berhasil disimpan")
            Conn.Close()
            CMD.Dispose()
            Call KondisiAwal()

        End If
    End Sub


What I have tried:

help me ............. I've tried but there is no way out
Posted
Updated 22-Nov-20 22:49pm
v2
Comments
Richard MacCutchan 22-Nov-20 4:50am    
Somewhere in all that code you are passing a null value as a parameter. Use your debugger to find out where and why.

1 solution

If this is the code that is throwing that exception then it would appear that Label12.Text is empty.

How I came to this conclusion ..

The error message clearly states that
Quote:
Column 'nofak' cannot be null
So I searched for nofak and found it in these two lines
VB
simpan = "INSERT INTO tbbeli(nofak,tanggal,kodesup,totalitem,totalbayar) VALUES (@p1,@p2,@p3,@p4,@p5)"
simpan1 = "INSERT INTO tbdetailbeli(nofak,kodebarang,jumlah) VALUES (@p6,@p7,@p8) "
Because you have correctly used parameterised queries it is easy to spot that parameter @p1 or parameter @p6 must be at fault (if this is the code that is throwing the exception).
So I searched for where @p1 or @p2 are being initialised
VB
.Parameters.Add("p1", OdbcType.VarChar, 20).Value = Label12.Text
and
VB
.Parameters.Add("p6", OdbcType.VarChar, 20).Value = Label12.Text

Of course if it is CommandText ubah that is throwing the exception it means that you have a record on your table that was inserted before the constraint on nofak was added - i.e. you have corrupt data. You will either have to delete that row or correct it manually.

To ensure you get better answers in future, always let us know which line of code is throwing the exception.

If you are not sure how to use the debugging tools in Visual Studio here is a starting point Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
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