我是应雇主的要求开始学习Visual Basic
的,但我在学习项目中遇到了令我沮丧的一点。
我正在尝试使用 SQL 数据库中特定表中的数据填充 ListView,但无论我如何尝试修复它,我都会收到相同的错误:
Argument Out of Range Exception - Invalid Argument=Value of '1' is not valid for 'index'.
这是生成异常的代码:
Private Sub MasterListSeries_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lvMasterListSeries.Columns.Add("Unique ID", 75, HorizontalAlignment.Center)
lvMasterListSeries.Columns.Add("Working Title", 100, HorizontalAlignment.Center)
lvMasterListSeries.Columns.Add("Book Count", 75, HorizontalAlignment.Center)
lvMasterListSeries.Columns.Add("Genre", 75, HorizontalAlignment.Center)
lvMasterListSeries.Columns.Add("Published Title", 100, HorizontalAlignment.Center)
lvMasterListSeries.Columns.Add("Date Started", 85, HorizontalAlignment.Center)
lvMasterListSeries.Columns.Add("Date Completed", 85, HorizontalAlignment.Center)
Using myConnection As New SqlConnection(dbConnection)
myConnection.Open()
Dim Count As Integer
Dim i As Integer = 0
Using CountRows As New SqlCommand("SELECT Count(uniqueID) FROM tblSeries", myConnection)
Count = Convert.ToDecimal(CountRows.ExecuteScalar)
End Using
Using querySeries As New SqlCommand("SELECT uniqueID, workingtitle, forecastedbookcount, genre, publishedtitle, datestarted, datecompleted FROM tblSeries", myConnection)
Dim qsResult As SqlDataReader = querySeries.ExecuteReader()
While qsResult.Read
i = Convert.ToDecimal(qsResult("uniqueID"))
lvMasterListSeries.Items.Add(New ListViewItem(Convert.ToString(qsResult("uniqueID"))))
lvMasterListSeries.Items(i).SubItems(1).Text = Convert.ToString(qsResult("workingtitle"))
lvMasterListSeries.Items(i).SubItems(2).Text = Convert.ToString(qsResult("forecastedbookcount"))
lvMasterListSeries.Items(i).SubItems(3).Text = Convert.ToString(qsResult("genre"))
lvMasterListSeries.Items(i).SubItems(4).Text = Convert.ToString(qsResult("publishedtitle"))
lvMasterListSeries.Items(i).SubItems(5).Text = Convert.ToString(qsResult("datestarted"))
lvMasterListSeries.Items(i).SubItems(6).Text = Convert.ToString(qsResult("datecompleted"))
End While
End Using
End Using
End Sub
异常发生在While qsResult.Read
序列的第三行。
我已经阅读了 Stack Overflow 上的两个线程、Dream_In_Code 上的两个线程和 Code Guru 上的另一个线程……都无济于事。
我在 MSDN 的社区论坛上发现了一个问题,但它似乎在使用数据集,而我正在尝试通过活动连接来学习 ADO.NET ......而且我担心我无法将它转换为可用的东西我。
这是 Visual Studio 调试窗口的屏幕截图:
(ListView
根据人口)应该类似于此处显示的 SQL 查询的结果:
感谢您的帮助,感谢您抽出宝贵时间阅读本文。