VB.NET中的0位没有行

时间:2014-10-13 07:23:19

标签: mysql vb.net listview

我的项目中有3个Forms,第二个Form我有这个编辑按钮,我将编辑第三个Listview上的Form项,但是当我在Listview中选择一个项目并按编辑时,会显示错误。我花了几个小时才发现问题所在,我最终到了这里。我错过了什么吗?

这是我的第一个带有listview的表单。

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

我的第二个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

和我的第三个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

1 个答案:

答案 0 :(得分:3)

Button1_Click中,您正在访问DataRow的第一个DataTable,而不检查是否有一个:{/ p>

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

您可以使用DataTable.Rows属性检查是否有一行:

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

由于您尝试根据用户的选择获取所有记录,我猜您使用了错误的字段。您正在使用显示字段,但您正在按ID字段过滤:

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

也许这就是你真正想要的东西:

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

请注意,不应使用字符串连接,而应使用sql-parameters来阻止sql注入。