我应雇主的要求开始学习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查询的结果:
感谢您的帮助,感谢您花时间阅读本文。
答案 0 :(得分: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