更改ListBox索引显示SQLite数据库中的错误值

时间:2017-04-12 15:03:33

标签: vb.net sqlite listbox

我正在编写一个VB.NET程序,它使用SQLite作为数据库。

我的问题是,当表单加载时,它会显示从数据库到ListBox的特定列(我的情况下的问题)中的所有值。然后,如果用户单击任何项​​目(ListBox索引更改),则数据库(答案)中的相应值将显示在RichTextBox中。这可以按预期工作。

当用户在TextBox中输入内容并点击搜索Button时,应根据TextBox.Text将数据库中的问题显示为数据库中的标记列相同的ListBox。这也很好。

ListBox索引立即更改时,问题就开始了。如果搜索结果只是ListBox中的一个项目,则索引将为零,RichTextBox显示数据库的第一个项目,其中包含ListBox项目的相应值。“ / p>

我的代码在这里:

Sub showData()'''this shows questions when form loads
    connect()
    Dim da As New SQLiteDataAdapter("select * from elect", connection)
    'Dim dt As New DataTable
    Dim ds As New DataSet
    da.Fill(ds, "elect")
    Dim mySelectQuery As String = "select * from elect"
    Dim sqConnection As New SQLiteConnection(connection)
    Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
    'sqConnection.Open()
    Try
        Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()


        ' Always call Read before accessing data.
        Do While sqReader.Read()
            Dim sName = sqReader.Item("question")

            ListBox1.Items.Add(sName)
        Loop

        ' always call Close when done reading.
        sqReader.Close()

        ' Close the connection when done with it.
    Finally
        connection.Close()
    End Try


End Sub


Public Sub NavigateRecords()
    page1.Clear()''page is rich text box


    connect()
    Dim da As New SQLiteDataAdapter("select * from elect", connection)
    'Dim dt As New DataTable
    Dim ds As New DataSet
    da.Fill(ds, "elect")

    Dim mySelectQuery As String

    mySelectQuery = "select * from elect"

    Dim sqConnection As New SQLiteConnection(connection)
    Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
    Dim num As Integer = Me.inc = Conversions.ToInteger(ListBox1.SelectedIndices.ToString)
    MsgBox(num)
    Me.inc = Conversions.ToInteger(ListBox1.SelectedIndex.ToString)
    If (Me.inc > -1) Then

            Dim ans As String = Conversions.ToString(ds.Tables.Item("elect").Rows.Item(Me.inc).Item(2))

            page1.Text = (ans)
End If
End Sub

Private Sub search()
    connect()
    Dim da As New SQLiteDataAdapter("select * from elect", connection)
    'Dim dt As New DataTable
    Dim ds As New DataSet
    da.Fill(ds, "elect")
    Dim mySelectQuery As String = ("select * from elect WHERE tag like'%" & txtSearch.Text & "%' ")
    Dim sqConnection As New SQLiteConnection(connection)
    Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
    Try
        Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()

        ' Always call Read before accessing data.
        Do While sqReader.Read()
            Dim sName = sqReader.Item("question")

            ListBox1.Items.Add(sName)
        Loop
        If ListBox1.Items.Count = 0 Then
            MsgBox("Nothing Found ")

            showData()





        End If
        ' always call Close when done reading.
        sqReader.Close()

        ' Close the connection when done with it.
    Finally
        connection.Close()
    End Try
End Sub


Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    page1.Visible = True

    ListBox1.Visible = False

    Label1.Visible = False

        NavigateRecords()

End Sub

0 个答案:

没有答案