Solved

InvalidArguement=Value of '0' is not valid for 'index'.Parameter name index

Posted on 2014-07-31
8
190 Views
Last Modified: 2014-08-05
I decided to put all classes in the same project. so I got rid of errors but now when I click a button to load a listview control. I get
after button click I get this message box this here is code
DB Connection
Imports System.Data.SqlClient
Public Class DB
    Public Shared Function GetConnection() As SqlConnection
        Dim connectionString As String =
            "Persist Security Info=False;User ID=sa;Password=50Cent;Initial Catalog=TSN2;Server=tsnappdev01"
        Return New SqlConnection(connectionString)
    End Function
End Class

Open in new window


here is form code

Public Class Form1
    Public Sub FormatLVStatus()
        'lvHotfixes.Location = New System.Drawing.Point(13, 54)
        lvPlayers.Margin = New System.Windows.Forms.Padding(4)
        lvPlayers.MultiSelect = False
        lvPlayers.Name = "LVCONNECT1"

        'LVCONNECT.TabIndex = 7
        lvPlayers.UseCompatibleStateImageBehavior = True
        lvPlayers.View = System.Windows.Forms.View.Details
        lvPlayers.Columns.Add("", 25, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("PlayerID", 100, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Firstname", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Lastname", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Position", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Height", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Weight", 150, HorizontalAlignment.Left)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        FormatLVStatus()
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Dim playersList As List(Of Players)
        Try
            playersList = PlayersSQL.GetPlayers
            If playersList.Count > 0 Then
                Dim players As Players
                For i As Integer = 0 To playersList.Count - 1
                    players = playersList(i)
                    '  lvPlayers.Items.Add(players.playersNumber)
                    lvPlayers.Items(i).SubItems.Add(
                        CInt(players.PlayerID).ToString)
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Firstname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Lastname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Position))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Height))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Weight))
                Next
            Else
                MessageBox.Show("All playerss are paid in full.",
                    "No Balance Due")
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
            Me.Close()
        End Try
    End Sub
End Class

Open in new window


PlayersSQL

Imports System.Data.SqlClient
Public Class PlayersSQL
    Public Shared Function GetPlayers() As List(Of Players)
        Dim playersList As New List(Of Players)
        Dim connection As SqlConnection = DB.GetConnection
        Dim selectStatement As String =
            "SELECT PlayerID, Firstname, Lastname, " &
             "Position, Weight, Height " &
             "FROM NFLPlayers " &
             "ORDER BY Lastname"
        Dim selectCommand As New SqlCommand(selectStatement, connection)
        Try
            connection.Open()
            Dim reader As SqlDataReader =
                selectCommand.ExecuteReader()
            Dim players As Players
            Do While reader.Read
                players = New Players
                players.PlayerID = CInt(reader("playerID"))
                players.Firstname = CStr(reader("firstname"))
                players.Lastname = CStr(reader("lastname"))
                players.Position = CStr(reader("position"))
                players.Height = CStr(reader("height"))
                players.Weight = CStr(reader("weight"))
                playersList.Add(players)
            Loop
            reader.Close()
        Catch ex As SqlException
            Throw ex
        Finally
            connection.Close()
        End Try
        Return playersList
    End Function
End Class

Open in new window


FINALLY Players Class

Public Class Players
    Private m_PlayerID As Integer
    Private m_Firstname As String
    Private m_Lastname As String
    Private m_Position As String
    Private m_Height As String
    Private m_Weight As String

    Public Sub New()

    End Sub

    Public Property PlayerID() As Integer
        Get
            Return m_PlayerID
        End Get
        Set(ByVal value As Integer)
            m_PlayerID = value
        End Set
    End Property

    Public Property Firstname() As String
        Get
            Return m_Firstname
        End Get
        Set(ByVal value As String)
            m_Firstname = value
        End Set
    End Property

    Public Property Lastname() As String
        Get
            Return m_Lastname
        End Get
        Set(ByVal value As String)
            m_Lastname = value
        End Set
    End Property

    Public Property Position() As String
        Get
            Return m_Position
        End Get
        Set(ByVal value As String)
            m_Position = value
        End Set
    End Property

    Public Property Height() As String
        Get
            Return m_Height
        End Get
        Set(ByVal value As String)
            m_Height = value
        End Set
    End Property

    Public Property Weight() As String
        Get
            Return m_Weight
        End Get
        Set(ByVal value As String)
            m_Weight = value
        End Set
    End Property
End Class

Open in new window


Any suggestions pleas let me know or fixes thanks EE Never really had a problem doing this before but that was a few years ago. help EE Please
0
Comment
Question by:powerztom
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40232875
It looks like you commented out the code to add items to lvPlayers, and only left in code to add subitems to the item you didn't add?
0
 
LVL 40
ID: 40232882
Please, specify which line causes a problem when you ask a question. This makes our job a lot of helping you a lot easier.

I suspect that it is line 33 of the second piece of code. The line that adds a new item to the ListView is commented out, so there is no item defined when you try to add subitems.
0
 

Author Comment

by:powerztom
ID: 40234687
When you add a new project to your current Project in my case I add a Class Library. Now whatever I name that Class Library I want to reference that new project in current Project Like for example I create a form then I add new project called NFLRoster which is a class library then within I create my Classes for the solution now in the first project that has a Form I add IMPORTS NFLRoster in beginning however I get that error line under the IMPORTS NFLRoster well the NFLRoster word. Now this stops me from using Classes I created in The class library called NFLRoster. Is there a step I missed out on. I think this is my problem. Please help EE
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 40
ID: 40234699
Have you tried removing the comment on line 33 as suggested?
0
 

Author Comment

by:powerztom
ID: 40234733
James AI remmed that line out cause it had a error line after it so I unremmed it and error line came back
this is the line lvPlayers.Items.Add(players.playersNumber) But any suggestions or do you understand what I am asking in my lastest post?
0
 

Author Comment

by:powerztom
ID: 40234761
I just chaged lvPlayers.Items.Add(players.playersNumber) to lvPlayers.Items.Add(players.Index) and got the data I needed except I got a few records with PlayerID AND NOTHING MORE for Firstname , Lastname ect. James do you know of a way to avoid showing these records. A Null detector ?

Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Dim playersList As List(Of Players)
        Try
            playersList = PlayersSQL.GetPlayers
            If playersList.Count > 0 Then
                Dim players As Players
                For i As Integer = 0 To playersList.Count - 1
                    players = playersList(i)
                    lvPlayers.Items.Add(players.Index)
                    lvPlayers.Items(i).SubItems.Add(
                        CInt(players.PlayerID).ToString)
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Firstname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Lastname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Position))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Height))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Weight))
                Next
            Else
                MessageBox.Show("All playerss are paid in full.",
                    "No Balance Due")
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
            Me.Close()
        End Try
    End Sub

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40234785
Well, the ideal would be to filter them at the source, when you query your SQL database. But if for some reason you cannot do that, here is how you can do it in your code:

                    players = playersList(i)
If Cstr(players.FirstName).Length>0 AndAlso Cstr(players.LastName).Length>0 Then
                    lvPlayers.Items.Add(players.Index)
                    lvPlayers.Items(i).SubItems.Add(
                        CInt(players.PlayerID).ToString)
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Firstname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Lastname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Position))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Height))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Weight))
End If

Open in new window

0
 

Author Closing Comment

by:powerztom
ID: 40241082
Thanks Bro
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question