我试图通过允许用户选择列名并使用文本框搜索数据来过滤列表视图中的数据。
这是我的代码
Private Sub Search()
lviStock.Items.Clear()
Dim item As New ListViewItem()
Dim _isFound As Boolean = False
Dim colName() As String = {"MedicineID", "Medicine_Name"}
Dim strSqlSearch As String = "SELECT MedicineID, Medicine_Name " &
"FROM medicine " &
"WHERE " & colName(cboColumns.SelectedIndex) & "' LIKE '" & txtSearchCriteria.Text & "%'"
Using myConn As New MySqlConnection("server=localhost; userid = root; password = root; database= database")
Using myComm As New MySqlCommand()
With myComm
.Connection = myConn
.CommandType = CommandType.Text
.CommandText = strSqlSearch
.Parameters.AddWithValue("@valueName", txtSearchCriteria.Text)
End With
Try
myConn.Open()
Dim myReader As MySqlDataReader = myComm.ExecuteReader()
While myReader.Read()
_isFound = True
item = lviClientlist.Items.Add(myReader("MedicineID").ToString)
item.SubItems.Add(myReader("Medicine_Name").ToString)
End While
If Not _isFound Then
MsgBox("No results found.", MsgBoxStyle.OkOnly, "Stock System")
End If
Catch ex As MySqlException
MsgBox(ex.Message.ToString(), MsgBoxStyle.OkOnly, "Stock System")
End Try
End Using
End Using
End Sub
现在我不断收到一条错误消息,指出“索引超出了数组的范围。”
突出显示部分
Dim strSqlSearch As String = "SELECT MedicineID, Medicine_Name " &
"FROM medicine " &
"WHERE " & colName(cboColumns.SelectedIndex) & "' LIKE '" & txtSearchCriteria.Text & "%'"
我该如何解决这个问题?