简体   繁体   中英

There is no row at position 0 in VB.NET

i have this 3 Forms in my project, on the second Form i have this edit button where i will edit the Listview item on the third Form , but when i select an item in the Listview and press edit, an error shows. It took me hours to find what's the problem, and i ended up here. Am i missing something?

this is my first form with listview in it.

Imports MySql.Data.MySqlClient
Public Class Form5

Public cd As Integer
Dim con As MySqlConnection
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim con As New MySqlConnection
    con.ConnectionString = "server=localhost;user id=root;database=db;password=root"
    con.Open()
    LoadPeople()
End Sub
Public Sub LoadPeople()
    Dim sConnection As New MySqlConnection
    sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
    sConnection.Open()
    Dim sqlQuery As String = "select * from candidate where cfname<>'Select a Candidate' AND candidacy='Filed'"
    Dim sqlAdapter As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim TABLE As New DataTable
    Dim i As Integer


    With sqlCommand
        .CommandText = sqlQuery
        .Connection = sConnection
    End With

    With sqlAdapter
        .SelectCommand = sqlCommand
        .Fill(TABLE)
    End With

    LvPeople.Items.Clear()

    For i = 0 To TABLE.Rows.Count - 1
        With LvPeople
            .Items.Add(TABLE.Rows(i)("idn"))
            With .Items(.Items.Count - 1).SubItems
                .Add(AddFieldValue(TABLE.Rows(i), ("cpos")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("clname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cparty")))
            End With
        End With
    Next

End Sub

Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
    If Not DBNull.Value.Equals(row.Item(fieldName)) Then
        Return CStr(row.Item(fieldName))
    Else
        Return Nothing
    End If
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Form17.Show()
End Sub
End Class

my 2nd Form

Imports MySql.Data.MySqlClient
Public Class Form17

Public cd As Integer
Public sConnection As New MySqlConnection
Private Sub Form17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
        sConnection.Open()
    End If
    LoadPeople3()
End Sub
Public Sub LoadPeople3()
    Dim sqlQuery As String = "select * from candidate where cfname<>'Select a Candidate'"
    Dim sqlAdapter As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim TABLE As New DataTable
    Dim i As Integer

    With sqlCommand
        .CommandText = sqlQuery
        .Connection = sConnection
    End With

    With sqlAdapter
        .SelectCommand = sqlCommand
        .Fill(TABLE)
    End With

    lvPeople3.Items.Clear()

    For i = 0 To TABLE.Rows.Count - 1
        With lvPeople3
            .Items.Add(TABLE.Rows(i)("idn"))
            With .Items(.Items.Count - 1).SubItems
                .Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("clname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cyr")))
            End With
        End With
    Next
End Sub
Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
    If Not DBNull.Value.Equals(row.Item(fieldName)) Then
        Return CStr(row.Item(fieldName))
    Else
        Return Nothing
    End If
End Function
Private Sub lvPeople3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvPeople3.MouseClick
    cd = lvPeople3.SelectedItems(0).Selected
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If cd = Nothing Then
        MsgBox("Please choose a record to edit.", MsgBoxStyle.Exclamation)
    Else
        Dim sqlQuery As String = "SELECT * FROM candidate WHERE cid = '" & lvPeople3.SelectedItems(0).Text & "'"
        Dim sqlAdapter As New MySqlDataAdapter
        Dim sqlCommand As New MySqlCommand
        Dim sqlTabble As New DataTable

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = sConnection
            .ExecuteNonQuery()
        End With

        With sqlAdapter
            .SelectCommand = sqlCommand
            .Fill(sqlTabble)
        End With
        Form23.cd = lvPeople3.SelectedItems(0).Text
        Form23.cfname = sqlTabble.Rows(0)("cfname")
        Form23.cfname = sqlTabble.Rows(0)("cmname")
        Form23.cfname = sqlTabble.Rows(0)("clname")
        Form23.ShowDialog()

        cd = Nothing
    End If
End Sub
 End Class

and my third Form

Imports MySql.Data.MySqlClient
Public Class Form23

Friend cd As Integer
Friend cfname As String
Friend clname As String
Friend cmname As String

Public sConnection As New MySqlConnection

Private Sub Form23_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
        sConnection.Open()
    End If

    TextBox2.Text = cfname
    TextBox3.Text = clname
    TextBox4.Text = cmname

End Sub

Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
    Dim conn As New MySqlConnection
    Dim cmd As New MySqlCommand

    conn.ConnectionString = "server = localhost; user id = root; database = db; password = root"
    cmd.Connection = conn
    conn.Open()

    If TextBox1.Text = "" Then
        MessageBox.Show("Please complete the required fields..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        Dim sqlQuery As String = "UPDATE candidate SET cpos='" & ComboBox1.Text & "', cparty='" & TextBox1.Text & "', candidacy='Filed' WHERE cid='" & cd & "'"
        Dim sqlCommand As New MySqlCommand

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = sConnection
            .ExecuteNonQuery()
        End With
        MsgBox("Record Updated")
        Dispose()
        Form5.Show()
        Form17.Hide()
    End If

    Form5.LoadPeople()
    Form17.LoadPeople3()

    Me.Close()
End Sub
End Class

In Button1_Click you are accessing the first DataRow of a DataTable without checking if there is one:

Form23.cfname = sqlTabble.Rows(0)("cfname")
Form23.cfname = sqlTabble.Rows(0)("cmname")
Form23.cfname = sqlTabble.Rows(0)("clname")

You can check if there is one row by using the DataTable.Rows property:

If sqlTabble.Rows.Count > 0 Then
    Form23.cfname = sqlTabble.Rows(0)("cfname")
    Form23.cfname = sqlTabble.Rows(0)("cmname")
    Form23.cfname = sqlTabble.Rows(0)("clname")
End If

Since you are trying to get all records according to the user's selection, i guess that you're using the wrong field. You are using the display-field but you are filtering by the ID-field:

"SELECT * FROM candidate WHERE cid = '" & lvPeople3.SelectedItems(0).Text & "'"

Maybe this is what you actually want:

"SELECT * FROM candidate WHERE cfname = '" & lvPeople3.SelectedItems(0).Text & "'"

Note that you should not use string concatenation but sql-parameters to prevent sql injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM