2017-02-07 89 views
0

我應僱主的要求開始學習​​3210,我在我的學習項目中達到了一個令我感到沮喪的地步。Visual Basic 2015:從SQL連接填充ListView

我試圖填充使用從我的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

我很感激任何幫助,感謝您花時間閱讀本文。

+0

您正在嘗試使用'SubItems(1)'在那個點不存在。這會導致錯誤。我不熟悉這些控件,但是在構建一個合適的'someNewSubItem'對象(它可能已經包含Text屬性)後,您可能需要執行'.SubItems.Add(someNewSubItem)'。 –

回答

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 
+0

此解決方案工作正常!謝謝! – Kirloth

+0

@Kirloth - 很高興幫助! – Phil