0

我是应雇主的要求开始学习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 调试窗口的屏幕截图:

Visual_Studio_Debug_Screen

ListView根据人口)应该类似于此处显示的 SQL 查询的结果:

SQL_Query_Screen

感谢您的帮助,感谢您抽出宝贵时间阅读本文。

4

1 回答 1

0

您正在调用 Items(i) 而不是 Items.Item(i)。Items 只返回 ListView 中的项目集合。你需要调用 Items.Item(i).SubItems...

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.Item(i).SubItems(1).Text = Convert.ToString(qsResult("workingtitle"))
                 lvMasterListSeries.Items.Item(i).SubItems(2).Text = Convert.ToString(qsResult("forecastedbookcount"))
                 lvMasterListSeries.Items.Item(i).SubItems(3).Text = Convert.ToString(qsResult("genre"))
                 lvMasterListSeries.Items.Item(i).SubItems(4).Text = Convert.ToString(qsResult("publishedtitle"))
                 lvMasterListSeries.Items.Item(i).SubItems(5).Text = Convert.ToString(qsResult("datestarted"))
                 lvMasterListSeries.Items.Item(i).SubItems(6).Text = Convert.ToString(qsResult("datecompleted"))
             End While
         End Using
     End Using

 End Sub

不过,我建议您像下面那样进行操作,将一组值传递给您的 ListViewItem。不易出错。

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
                 lvMasterListSeries.Items.Add(
                    New ListViewItem({
                        Convert.ToString(qsResult("uniqueID")),
                        Convert.ToString(qsResult("workingtitle")),
                        Convert.ToString(qsResult("forecastedbookcount")),
                        Convert.ToString(qsResult("genre")),
                        Convert.ToString(qsResult("publishedtitle")),
                        Convert.ToString(qsResult("datestarted")),
                        Convert.ToString(qsResult("datecompleted")      
                    })
                 )
             End While
         End Using
     End Using

 End Sub
于 2017-02-07T15:38:41.747 回答