索引超出范围。必须为非负数,并且小于集合的大小。参数名称:索引

时间:2019-01-15 03:09:45

标签: vb.net

当我想从datagridview2更新我的sql中的数据时,出现了这个问题,该数据已经充满了datagridview1中的doubleclickcell

这是保存按钮代码

        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

这是我双击单元格时的代码

    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

这是我的代码,每当datagridview2更改时,我都想要它,然后按保存按钮,它将更新mysql数据库中的记录。 我使用id_treatmen和nama_obat作为主键

1 个答案:

答案 0 :(得分:1)

请突出显示程序崩溃的确切代码行...

我怀疑您是 DESTROYING ,而DataGrid2对象处于循环中间

因此,在循环的 FIRST 循环结束时,您正在销毁DataGrid2对象,因此INDEX超出范围。

查看行

DataGridView2.DataSource =无

在“尝试,抓住”部分的“最后”部分。