-3

i got this problem when I want to update data in my sql from datagridview2 that already fill with doubleclickcell from datagridview1

this is the save button code

        Try
            'reader.Dispose()
            Dim iReturn As Boolean
            Dim conn As New MySqlConnection("server=localhost;user id=root;password=admin;database=dni")
            For i As Integer = 0 To DataGridView2.Rows.Count - 1 Step +1
                Using command As New MySqlCommand()
                    With command

                        .CommandText = "INSERT INTO dni.tbl_treatmen (`id_treatmen`, `nama_treatmen`, `harga_treatmen`, `Nama_obat`, `qty`, `satuan`) values (@id,@nama,@harga,@namaobat1,@qtyobat1,@satuanobat1) ON DUPLICATE KEY UPDATE
                                            `id_treatmen` = @id,
                                            `nama_treatmen`= @nama,
                                            `harga_treatmen` = @harga,
                                            `Nama_obat`= @namaobat1, 
                                            `qty`= @qtyobat1,
                                            `satuan`= @satuanobat1"

                        .Connection = conn
                        .CommandType = CommandType.Text
                        .Parameters.AddWithValue("@id", textbox_id.Text)
                        .Parameters.AddWithValue("@nama", textbox_nama.Text)
                        .Parameters.AddWithValue("@harga", textbox_hargajual.Text)




                        .Parameters.AddWithValue("@namaobat1", DataGridView2.Rows(i).Cells(0).Value.ToString)
                        .Parameters.AddWithValue("@qtyobat1", DataGridView2.Rows(i).Cells(1).Value.ToString)
                        .Parameters.AddWithValue("@satuanobat1", DataGridView2.Rows(i).Cells(2).Value.ToString)



                    End With







                    Try
                        conn.Open()
                        command.ExecuteNonQuery()
                        iReturn = True
                    Catch ex As MySqlException
                        MsgBox(ex.Message.ToString)
                        iReturn = False
                    Finally
                        conn.Dispose()

                        DataGridView2.DataSource = Nothing
                    End Try
                End Using
            Next




            MessageBox.Show("Data Saved", "Save Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
            textbox_id.Text = "ID Treatmen"
            textbox_nama.Text = "Nama Treatmen"

            textbox_hargajual.Text = "Harga Jual"

            textbox_id.Enabled = False
            textbox_nama.Enabled = False

            textbox_hargajual.Enabled = False

            DataGridView1.Enabled = False
            Label2.Visible = False
            ShowData()

            Button1.Enabled = True
            Button2.Enabled = True
            Button3.Enabled = False
            Button4.Enabled = True
            Button5.Enabled = False
            Button6.Enabled = False

            If aa = 1 Then
                DataGridView2.Rows.Clear()
                DataGridView2.Columns.Clear()
                GroupBox1.Visible = False
            ElseIf aa = 2 Then
                DataGridView2.DataSource = Nothing
                Button9.Visible = False


            End If




        Catch ex As Exception
            MsgBox(ex.Message)
        End Try



    End If
End Sub

this is the code when i double click cell

    Button9.Visible = True
    Dim conn = New MySqlConnection
    conn.ConnectionString = "server=localhost;userid=root;password=admin;database=dni"
    Dim SDA As New MySqlDataAdapter
    Dim dbDataSet As New DataTable
    Dim bSource As New BindingSource

    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)

        textbox_id.Text = row.Cells("ID Treatmen").Value.ToString
        textbox_nama.Text = row.Cells("Nama Treatmen").Value.ToString

        textbox_hargajual.Text = row.Cells("Harga Treatmen").Value.ToString



        Try
            conn.Open()
            Dim query As String
            query = "select nama_obat as 'Nama Obat' , qty as 'Quantity' ,Satuan as 'Satuan' from dni.tbl_treatmen where id_treatmen = @idtreat"
            Dim command As New MySqlCommand(query, conn)
            SDA.SelectCommand = command
            command.Parameters.AddWithValue("@idtreat", textbox_id.Text)
            SDA.Fill(dbDataSet)
            bSource.DataSource = dbDataSet
            DataGridView2.DataSource = dbDataSet
            SDA.Update(dbDataSet)
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Dispose()
        End Try





        textbox_id.Enabled = False
        textbox_nama.Enabled = True

        textbox_hargajual.Enabled = True


        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
    End If

this is my code, i want it whenever the datagridview2 is change and i press the save button it will update the record in mysql database. i use id_treatmen and nama_obat as primary key

Akira
  • 5
  • 3
  • The message tells you what the problem is - you have an index that is either negative or larger than the collection. Use the debugger to step through the code and find out why that's happening. We can't do that for you, because we don't have your data or the rest of your code to use to do so. – Ken White Jan 15 '19 at 03:18
  • 2
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Ken White Jan 15 '19 at 03:20
  • what is this index mean i dont understand :( and wahat collection? – Akira Jan 15 '19 at 04:09
  • It's your code. How can you not know what collection you're trying to get an item from by index? The very first thing you should be doing (have already done) is determine exactly what line is throwing the exception and then examine everything about that line, both in the code editor and the debugger while it's executing. – jmcilhinney Jan 15 '19 at 04:23
  • First get rid the Try...Catch....End Try in your code so we can at least find out what line is causing the error. You are working with all sorts of collections. Rows is a collection , Cells is a collection, Parameters is a collection. Enough? – Mary Jan 15 '19 at 05:20
  • @Akira If you don't know what the meaning of 'index' is you should repeat learning the basics of programming. – Horitsu Jan 15 '19 at 06:51
  • If you use the debugger as I suggested, you'll know exactly what index and what collection. If you also remove the empty `try..catch..end`, you'll know exactly when the exception occurs as well. – Ken White Jan 15 '19 at 13:18

1 Answers1

1

Please highlight the exact line of your code where your program is crashing on...

I suspect that you are DESTROYING the DataGrid2 object whilst it is in the middle of the loop

So at the end of the FIRST iteration of your loop you are destroying the DataGrid2 Object and therefore the INDEX is out range.

See the line

DataGridView2.DataSource = Nothing

Which is inside your Try, Catch in the FINALLY Section.

Zeddy
  • 2,079
  • 1
  • 15
  • 23