dimanche 17 février 2019

how to cannot proceed even only one error?

right now im using this code and it check the stock quantity but i got this 'treatmen' with have two 'obat' and whenever it check the quantity of the 'obat' from 'treatmen' and if there is only one 'obat' that out of quantity it still execute the else command of the 'if'

'treatmen' have 2 'obat' and one of the 'obat' that 'treatmen' use have minus quantity adn i dont want it to execute the insert part and update part i want it to stop whenever there is one quantity missmatch

Dim conn4 As New MySqlConnection("server=localhost;user id=root;password=admin;database=dni")
                Dim sql1 As String = "select tbl_treatmen.qty * '" & DataGridView1.Rows(a).Cells(2).Value & "' as 'qty1',dt_obat.quantity as 'qty2', dt_obat.nama_obat from dt_obat inner join tbl_treatmen on dt_obat.nama_obat = tbl_treatmen.nama_obat where nama_treatmen = '" & DataGridView1.Rows(a).Cells(0).Value & "'"
                Dim command2 As New MySqlCommand
                Dim reader2 As MySqlDataReader
                Dim aa As Double
                Dim aaa As Double
                command2.Connection = conn4
                command2.CommandText = sql1
                conn4.Open()
                reader2 = command2.ExecuteReader

            While reader2.Read()
                    aa = reader2("qty1")
                    aaa = reader2("qty2")


                    If aaa < aa Then
                            MessageBox.Show("Errow Stock Tidak Cukup! '" & aa & "' '" & aaa & "'")


                        Else
                            cetak = 1
                        '===============================================================================================================================================================================================================================================================================================================================================================================================================================================
                        'insert 
                        '===============================================================================================================================================================================================================================================================================================================================================================================================================================================

                        Try
                                'reader.Dispose()
                                Dim iReturn As Boolean
                                Dim conn5 As New MySqlConnection("server=localhost;user id=root;password=admin;database=dni")
                                Dim i As Integer
                                For i = 0 To DataGridView1.Rows.Count - 1


                                    Using sqlcommand As New MySqlCommand()
                                        With sqlcommand

                                            .CommandText = "INSERT INTO dni.dt_transaksi (`id_transaksi`, `nama_pasien`, `nama_barang`, `jenis`, `qty`, `harga`, `total`,`diskon`,`tanggal`,`jenispembayaran`,`nomor_kartu`,`nominal`,`nm_dokter`,`nm_pegawai`) values (@id,@nama,@nama_barang,@jenis,@qty,@harga,@total,@diskon,@tanggal,@jenispembayaran,@nomor_kartu,@nominal,@dokter,@pegawai)"

                                            .Connection = conn5
                                            .CommandType = CommandType.Text
                                            .Parameters.AddWithValue("@id", textbox_id.Text)
                                            .Parameters.AddWithValue("@nama", textbox_nama.Text)
                                            .Parameters.AddWithValue("@nama_barang", DataGridView1.Rows(i).Cells(0).Value)
                                            .Parameters.AddWithValue("@jenis", DataGridView1.Rows(i).Cells(1).Value)
                                            .Parameters.AddWithValue("@qty", DataGridView1.Rows(i).Cells(2).Value)
                                            .Parameters.AddWithValue("@harga", DataGridView1.Rows(i).Cells(3).Value)
                                            .Parameters.AddWithValue("@total", DataGridView1.Rows(i).Cells(5).Value)
                                            .Parameters.AddWithValue("@diskon", DataGridView1.Rows(i).Cells(4).Value)
                                            .Parameters.AddWithValue("@tanggal", Date.Now)
                                            .Parameters.AddWithValue("@jenispembayaran", ComboBox1.Text)
                                            .Parameters.AddWithValue("@nomor_kartu", kartu)
                                            .Parameters.AddWithValue("@nominal", cash)
                                            .Parameters.AddWithValue("@dokter", TextBox4.Text)
                                            .Parameters.AddWithValue("@pegawai", TextBox5.Text)


                                        End With

                                        Try
                                            conn5.Open()
                                            sqlcommand.ExecuteReader()
                                            iReturn = True
                                        Catch ex As Exception
                                            MsgBox(ex.Message.ToString)
                                            iReturn = False
                                        Finally
                                            conn5.Dispose()
                                            DataGridView1.DataSource = Nothing
                                        End Try

                                    End Using
                                Next
                                'MessageBox.Show("Transaksi Saved!", "Save Data", MessageBoxButtons.OK, MessageBoxIcon.Information)

                                Panel3.Visible = False
                                ComboBox1.Text = "Metode Pembayaran"
                                TextBox3.Text = ""
                            Catch ex As Exception
                                MsgBox(ex.Message.ToString)
                            Finally

                            End Try
                        '===============================================================================================================================================================================================================================================================================================================================================================================================================================================
                        'update 
                        '===============================================================================================================================================================================================================================================================================================================================================================================================================================================

                        Try
                                'reader.Dispose()
                                Dim iReturn As Boolean
                                Dim conn6 As New MySqlConnection("server=localhost;user id=root;password=admin;database=dni")
                                Dim i As Integer
                                For i = 0 To DataGridView1.Rows.Count - 1


                                    Using sqlcommand As New MySqlCommand()
                                        With sqlcommand

                                            .CommandText = "update dni.dt_obat inner join tbl_treatmen on dt_obat.nama_obat = tbl_treatmen.nama_obat inner join dt_transaksi on dt_transaksi.nama_barang = tbl_treatmen.nama_treatmen SET dt_obat.quantity = dt_obat.quantity - ('" & DataGridView1.Rows(i).Cells(2).Value & "' * tbl_treatmen.qty) where dt_transaksi.nama_barang = '" & DataGridView1.Rows(i).Cells(0).Value & "'"

                                            .Connection = conn6
                                            .CommandType = CommandType.Text


                                        End With

                                        Try
                                            conn6.Open()
                                            sqlcommand.ExecuteReader()
                                            iReturn = True
                                        Catch ex As Exception
                                            MsgBox(ex.Message.ToString)
                                            iReturn = False
                                        Finally
                                            conn6.Dispose()
                                            DataGridView1.DataSource = Nothing
                                        End Try

                                    End Using
                                Next
                                'MessageBox.Show("Transaksi updated!", "Save Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                Panel3.Visible = False
                                ComboBox1.Text = "Metode Pembayaran"
                                TextBox3.Text = ""





                            Catch ex As Exception
                                MsgBox(ex.Message.ToString)
                            Finally

                            End Try
                            '===============================================================================================================================================================================================================================================================================================================================================================================================================================================


                        End If


            End While

Aucun commentaire:

Enregistrer un commentaire