Error system indexoutofrangeexception

0
Dim form As New Form5
    Dim sqlConString As String = "Server=localhost\TESTE;Database=tempTest;User Id=sa;Password=123"
    Form7.clientNIF = DataGridView1.CurrentRow.Cells(0).Value.ToString()
    Try
        Using con = New SqlConnection(sqlConString)
            con.Open()
            Using cmd = con.CreateCommand
                cmd.CommandText = "SELECT * FROM Computador Where NIFcliente = @NIF"
                cmd.Parameters.AddWithValue("@NIF", DataGridView1.CurrentRow.Cells(0).Value.ToString())
                Dim asrow As Boolean
                asrow = False
                Dim reader = cmd.ExecuteReader
                While (reader.Read())
                    asrow = True
                End While
                If asrow = True Then
                    Form7.ShowDialog()
                    Me.Close()
                Else

                    cmd.CommandText = "SELECT * FROM Clientes where NIF = @NIF"
                    cmd.Parameters.AddWithValue("@NIF", DataGridView1.CurrentRow.Cells(0).Value.ToString())
                    form.NIF1.Text = reader.GetString(reader.GetOrdinal("NIF"))
                    form.Nome.Text = reader.GetString(reader.GetOrdinal("Nome"))
                    form.Contato.Text = reader.GetString(reader.GetOrdinal("Contato"))
                    form.Morada.Text = reader.GetString(reader.GetOrdinal("Morada"))
                    form.CP.Text = reader.GetString(reader.GetOrdinal("CP"))
                    form.Cidade.Text = reader.GetString(reader.GetOrdinal("Cidade"))
                    form.Pais.Text = reader.GetString(reader.GetOrdinal("Pais"))
                    form.Email.Text = reader.GetString(reader.GetOrdinal("Email"))
                    form.dtderegisto.Text = reader.GetString(reader.GetOrdinal("dtderegisto"))
                    form.ShowDialog()
                    Me.Close()

                End If



            End Using
        End Using
    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try
    Me.Close()

Error in

form.NIF1.Text = reader.GetString(reader.GetOrdinal("NIF"))  system indexoutofrangeexception
    
asked by anonymous 11.05.2018 / 12:28

1 answer

0

You had to close the previous link with the reader and open a new one

Dim form As New Form5
    Dim sqlConString As String = "Server=localhost\TESTE;Database=tempTest;User Id=sa;Password=123"
    Form7.clientNIF = DataGridView1.CurrentRow.Cells(0).Value.ToString()
    Try
        Using con = New SqlConnection(sqlConString)
            con.Open()
            Dim asrow As Boolean
            asrow = False
            Using cmd = con.CreateCommand
                cmd.CommandText = "SELECT * FROM Computador Where NIFcliente = @NIF"
                cmd.Parameters.AddWithValue("@NIF", DataGridView1.CurrentRow.Cells(0).Value.ToString())

                Using reader = cmd.ExecuteReader
                    While (reader.Read())
                        asrow = True
                    End While
                End Using

            End Using
            Using cmd = con.CreateCommand
                If asrow = True Then
                    Form7.ShowDialog()
                    Me.Close()
                Else

                    cmd.CommandText = "SELECT * FROM Clientes where NIF = @NIF"
                    cmd.Parameters.AddWithValue("@NIF", DataGridView1.CurrentRow.Cells(0).Value.ToString())
                    Dim reader = cmd.ExecuteReader()
                    reader.Read()
                    form.NIF1.Text = reader.GetString(reader.GetOrdinal("NIF"))
                    form.Nome.Text = reader.GetString(reader.GetOrdinal("Nome"))
                    form.Contato.Text = reader.GetString(reader.GetOrdinal("Contato"))
                    form.Morada.Text = reader.GetString(reader.GetOrdinal("Morada"))
                    form.CP.Text = reader.GetString(reader.GetOrdinal("CP"))
                    form.Cidade.Text = reader.GetString(reader.GetOrdinal("Cidade"))
                    form.Pais.Text = reader.GetString(reader.GetOrdinal("Pais"))
                    form.Email.Text = reader.GetString(reader.GetOrdinal("Email"))
                    form.dtderegisto.Text = reader.GetString(reader.GetOrdinal("dtderegisto"))
                    form.ShowDialog()
                    Me.Close()

                End If
            End Using
        End Using
    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try
    Me.Close()
    
11.05.2018 / 13:22